C This program read six real values which represent the values of C two 3D vectors. The values are either the Cartesian or spherical C representations of the vectors -- the assumed coordinate system C can be toggled by generating an error on the input. The program C prints the values of both vectors in Cartesian and spherical C coordinates. Additionally, the dot and cross products of the C vectors are given (the cross product is given in Cartesian C coordinates). C..... program shell implicit none real a,b,c, e,f,g, x1,y1,z1, x2,y2,z2, r1,t1,p1, r2,t2,p2, pi real dot integer state, numsts, num parameter (pi=3.141592) C..... C a,b,c = values associates with the first vector C e,f,g = values associates with the second vector C x1,y1,z1 = Cartesian coordinates of first vector C x2,y2,z2 = Cartesian coordinates of second vector C r1,p1,t1 = spherical coordinates of first vector C r2,p2,t2 = spherical coordinates of second vector C state = current "state" of the program, 0=Cartesian, 1=Spherical C numsts = the total number of possible states (just two) C num = status of the read operation C..... state = 0 numsts = 2 print *,'Enter two vectors. Cartesian coordinates are assumed.' print *,'Enter a non-numeric character for spherical input.' 10 read(5,*,iostat=num) a,b,c, e,f,g C....... C If end of file, i.e., num < 0, stop. If error occurred, i.e., C num > 0, toggle state and go back to read statement. C....... if (num .lt. 0) then stop elseif (num .gt. 0) then state = mod(state+1,numsts) if (state .eq. 0) then print *,'Cartesian coordinates are assumed.' print *,'Enter a non-numeric character for spherical', & ' input.' else print *,'Spherical coordinates are assumed', & ' (angles in degrees).' print *,'Enter a non-numeric character for Cartesian', & ' input.' endif goto 10 endif C....... C If we got here, input was okay. If state is zero, input C already in Cartesian form and need to calculate spherical C values. C....... if (state .eq. 0) then x1=a y1=b z1=c x2=e y2=f z2=g call crtsph(x1,y1,z1,r1,p1,t1) call crtsph(x2,y2,z2,r2,p2,t2) C....... C If state is not zero (i.e., it is one), then input was in C spherical coordinates. Convert degrees to radians and calculate C Cartesian values. C....... else r1 = a p1 = b*pi/180.0 t1 = c*pi/180.0 r2 = e p2 = f*pi/180.0 t2 = g*pi/180.0 call sphcrt(r1,p1,t1,x1,y1,z1) call sphcrt(r2,p2,t2,x2,y2,z2) endif C.......Print output. print *,'Cartesian coordinates (x,y,z):' print *,'Vec1: (',x1,',',y1,',',z1,')' print *,'Vec2: (',x2,',',y2,',',z2,')' print *,'Spherical coordinates (r,phi,theta):' print *,'Vec1: (',r1,',',p1*180.0/pi,',',t1*180.0/pi,')' print *,'Vec2: (',r2,',',p2*180.0/pi,',',t2*180.0/pi,')' print *,'Dot product: ',dot(x1,y1,z1,x2,y2,z2) call cross(x1,y1,z1,x2,y2,z2,a,b,c) print *,'Cross product: (',a,',',b,',',c,')' print * goto 10 stop end C..... C Subroutine to calculate Cartesian coordinates given spherical C coordinates. C..... subroutine sphcrt(r,p,t,x,y,z) implicit none real r,p,t, x,y,z x = r*cos(p)*sin(t) y = r*sin(p)*sin(t) z = r*cos(t) return end C..... C Subroutine to calculate spherical coordinates given Cartesian C coordinates. C..... subroutine crtsph(x,y,z,r,p,t) implicit none real x,y,z, r,p,t r = sqrt(x*x + y*y + z*z) p = atan2(y,x) t = atan2(sqrt(x*x+y*y),z) return end C..... C Function to calculate the dot product of two vectors (which are C assumed to be in Cartesian coordinates. C..... real function dot(x1,y1,z1, x2,y2,z2) implicit none real x1,y1,z1, x2,y2,z2 dot = x1*x2 + y1*y2 + z1*z2 return end C..... C Subroutine to calculate the cross product of two vectors. Input C vectors are assumed to be in Cartesian coordinates and result is C in Cartesian coordinates. C..... subroutine cross(x1,y1,z1, x2,y2,z2, a,b,c) implicit none real x1,y1,z1, x2,y2,z2, a,b,c a = y1*z2-y2*z1 b = x2*z1-x1*z2 c = x1*y2-x2*y1 return end