Fortran 90

Download Fortran 90

Post on 23-Oct-2014

201 views

Category:

Documents

8 download

Embed Size (px)

TRANSCRIPT

<p>90</p> <p>Fortran 90 Programming Examples Including Fortran 90 CD</p> <p>Omid Alizadeh CCGroup 1/1/2009</p> <p> 59 09 77 . 09 77 . COMPAQ FORTRAN 59 09 77 MICROSOFT VISUAL MICROSOFT . COMPAQ VISUAL FORTRAN .</p> <p> Variable Declaration Execution Statement . END . . ) ( . . . :</p> <p> : 1- 0 9 2- ) ( 3- _ 4- :5</p> <p>Character blank = + * / ( ) , .'</p> <p>Name )Blank (space Equal sign Plus sign Minus sign Asterisk Slash Left parenthesis Right parenthesis Comma Period (decimal )point Apostrophe</p> <p>Character : ! " % &amp; ; ? $</p> <p>Name Colon Exclamation point Quotation mark Percent sign Ampersand Semicolon Less than Greater than Question mark Dollar sign (currency )symbol</p> <p> . 5 ) (Label )+( 7 27 . . + &amp; . ! C . . : ** </p> <p>6</p> <p>* / + -</p> <p> 26+2 . 41 61 . . . :</p> <p>)(** / * +// &gt; &lt; =&lt; =&gt; = = =/ ..NOT ..AND ..OR ..EQV ..NEQV</p> <p> . </p> <p> . :7</p> <p>2/6 - 2 * 3 + 4 ^ ^ ^ ^ 2 1 3 4 2/6 - 2 * )3 + 4( ^ ^ ^ ^ 1 2 3 4 2/)6 - 2 * 3 + 4( ^ ^ ^ ^ 2 1 3 4</p> <p>7 =</p> <p>11 =</p> <p>2 =</p> <p>2/)6 - 2 * )3 + 4(( ^ ^ ^ ^ 1 2 3 4</p> <p>4 =</p> <p> )4+3(/5 .5 4 +3</p> <p> . ) NOT ( T T F F T F T F ..NOT F T F T ..AND T F F F ..OR T T T F ..EQV T F F T ..NEQV F T T F</p> <p> // '</p> <p> ' ABCDEF .'('ABC'//'DE')//'F )''ABC'//('DE'//'F ''ABC'//'DE'//'F</p> <p> :</p> <p>&lt; .LT. or</p> <p>8</p> <p>=&lt; .LE. or = = .EQ. or =/ .NE. or &gt; .GT. or =&gt; .GE. or</p> <p> . .TRUE . .FALSE . . . 2+2 4 .4 . 2/1 0 1 2 5.0 . . . 2/ .1 5.0 .</p> <p>9</p> <p> . . . . . 1 . 2 . 3 . a A z 0 Z 9 _ ) underscore ( . ) 7up 1 ( ) Sin 3 ( . Sinx Flor A-B . Sinx . ) Sin(x Sin Sinx . Flor ) Floor ( . A-B ) (dash . . a z 62 . . StudentName Name . NAME name . StudentName Student_Name StudentName .</p> <p>11</p> <p> . :</p> <p> 2 3 ... 1.2 0.3 )32 21(</p> <p>Integer Real</p> <p>Complex</p> <p>Character Logical</p> <p> Integer 0 9 . Real ) . ( 2.0 2. .2 . Complex . . : Complex CN )2,1(=CN Print *,CN End CN )00000.2,00000.1( Real . Real . Double Precision . 21</p> <p> . A B REAL A DOUBLE PRECISION B ).1(A=4*ATAN ).1(B=4*ATAN PRINT *,A,B END : 75210147295141.3 395141.3 . . BASIC : INTEGER*1 A A 8 -821 721 . 1 2 4 8 4 8 8* REAL DOUBLE PRESICION 4 . : INTEGER(1) A ) ( Integer , Real , Complex . . B . 2 ."1"B 10001B 100111B</p> <p> 2 . 8 O</p> <p> '2110'B</p> <p> 61 Z . :31</p> <p> 61</p> <p> 2</p> <p> 8</p> <p> 01</p> <p>0 1 2 3 4 5 6 7 8 9 A B C D E F</p> <p>0000 1000 0100 1100 0010 1010 0110 1110 0001 1001 0101 1101 0011 1011 0111 1111</p> <p>00 10 20 30 40 50 60 70 01 11 21 31 41 51 61 71</p> <p>0 1 2 3 4 5 6 7 8 9 01 11 21 31 41 51</p> <p> text . Ali :Hello . Ali :Hello . ... . ... .</p> <p>41</p> <p> Logical . .True . .False . . . .True . .False . . Type . . : ... 2 1 1 text : 1Character text . : INTEGER A,B,C I N . IMPLICIT NONE IMPLICIT . : ... 2 1 Implicit . C T : IMPLICIT CHARACTER T,C :</p> <p>51</p> <p>)Implicit integer ( I-N )Implicit Real (A-H) , ( O-Z I N A H O Z . x1 x2 xn n x n : ... ) ( 2 ) ( 1 . 001 x : )001(Integer x . x 32 : 32=)5(X . ... )... , 2 , 1 ( 001 ) 452 ( )4,52(Real A 52 4 : : )4,:(Real A A 4 1 4 . : ... ) ... , 1 : 1( : )2:1-,:(Real A61</p> <p> 4 1- . ) : ( Allocate . : )3,:,:(Real Allocatable :: A ))3,4,21:11(Allocate (A Allocate 2 11 4 1 ) ( . ALLOCATE . ALLOCATE : ):(INTEGER*4, ALLOCATABLE:: A ))1(ALLOCATE(A ))2(ALLOCATE(A END ALLOCATE . ALLOCATED . . .TRUE . .FALSE . . : Allocatable Allocatable Allocate . 5.6 Visual Fortran Allocate Allocate . - Dimension</p> <p>71</p> <p> A B C )2,2( . : Real ,Dimension (2,2) ::A,B,C : )2,2(Real A(2,2) , B(2,2) , C Parameter . 295141.3 p : 295141.3 =Double Precision , Parameter :: P Read *,R 2**Print *,Area = , p*R Print *, S = , 2*p*R End . entity oriented attribute oriented entity oriented : ... 2 1 :: ... 2 1 Real Allocatable , Dimension (:) :: A,B attribute oriented : ... 2 1 ... 2 1 1 ... 2 1 2 ... Real A,B Dimension (:) A,B81</p> <p>Allocatable A,B . . Kind . : Integer (Selected_INT_Kind(3)) A A 3 01- 301 . . : Real (Selected_Real_Kind(3,4))B B 3 4- 01 401 . Selected_INT_Kind Selected_Real_Kind : Integer (3) A Real (3,4) B : ... 1 )Character (Len=n ... 1 )Character (n ... 1 Character * n n . 1 . n m m&gt;n n . Space . 1Character *4 C91</p> <p>C1=Hello Hello Hell . 1Character *6 C C1=Hello Hello . )(Space .</p> <p>02</p> <p> . ) ( . )... 2 1( . : )ABS (x )CABS (x )DABS (x )IABS (x )ACOS (x )DACOS (x )AINT (x )DINT (x )ASIN (x )DSIN (x )ATAN (x )DTAN (x )ATAN2 (x )DTAN2 (x )CHAR (x )COS (x )CCOS (x )DCOS (x )CONJ (x )COSH (x )DCOSH (x22</p> <p> Real Complex Integer Real Real Real Real Real Integer Real Complex Complex Real </p> <p> Ascii </p> <p>)DIM (x,y )IDIM (x,y )DPROD (x,y )EXP (x )CEXP (x )DEXP (x )ICHAR (x )INDEX (String,Substring )INT (x )IFIX (x )IDINT (x ) LEN( String )LOG (x )ALOG (x )CLOG (x )DLOG (x )LOG10 (x )ALOG10 (x )DLOG10 (x ),MAX (x,y ),MAX0 (x,y ),AMAX1 (x,y ),DMAX1 (x,y ),MAX1 (x,y ),AMAX0 (x,y ),MIN (x,y ),MIN0 (x,y ),AMIN1 (x,y ),DMIN1 (x,y32</p> <p> Real Integer Real Real Complex Real Real Real Real Complex Real Real Real Integer Real Real Integer Real Integer Real </p> <p> e e e </p> <p>),MIN1 (x,y ),AMIN0 (x,y )MOD (x,y )AMOD (x,y )DMOD (x,y )REAL (x )FLOAT (x )SNGL (x )SIGN (x,y )DSIGN (x,y )ISIGN (x,y )SIN (x )CSIN (x )DSIN (x )SINH (x )DSINH (x )SQRT (x )CSQRT (x )DSQRT (x )TAN (x )DTAN (x )TANH (x )DTANH (x ) ADJUSTL ( String ) ADJUSTR (String ) TRIM ( String ) LEN_TRIM ( String42</p> <p> Real Integer Integer Real Integer Integer Real Integer Real Complex Real Real Complex Real Real </p> <p> x y x y x y </p> <p> . ) ( . : ) ( IF . . : ) Then 1 ( IF )Then 2 ( Else IF . . . Else End IF Else IF Else ELSE IF Then . End IF . . Else Else Else . .</p> <p>62</p> <p>: Read *,I If ( I&gt;=0 .AND. I= b must be true, they c = a ! are swapped if a &lt; b a = b b = c END IF DO c = MOD(a, b) IF (c == 0) EXIT a = b b = c END DO ! now we have a Input) EXIT DO IF (MOD(Input,Divisor) /= 0 .OR. Input == 1) EXIT Count = Count + 1 WRITE(*,*) 'Factor # ', Count, ': ', Divisor Input = Input / Divisor END DO Divisor = Divisor + 2 END DO END PROGRAM Factorize</p> <p>10 4 : 01 PROGRAM UpperTriangularMatrix IMPLICIT NONE INTEGER, PARAMETER INTEGER, DIMENSION(1:SIZE,1:SIZE) INTEGER INTEGER INTEGER CHARACTER(LEN=100)</p> <p>:: :: :: :: :: ::</p> <p>SIZE = 10 Matrix Number Position i, j Format</p> <p>READ(*,"(I5)") Number DO i = 1, Number READ(*,"(10I5)") (Matrix(i,j), j = 1, Number) END DO WRITE(*,"(1X,A)") "Input Matrix:" DO i = 1, Number WRITE(*,"(1X,10I5)") (Matrix(i,j), j = 1, Number) END DO WRITE(*,"(/1X,A)") "Upper Triangular Part:" Position = 2 DO i = 1, Number WRITE(Format,"(A,I2.2,A)") "(T", Position, ", 10I5)" WRITE(*,Format) (Matrix(i,j), j = i, Number) Position = Position + 5 END DO END PROGRAM UpperTriangularMatrix</p> <p> 5 : 60</p> <p>PROGRAM Multiplication_Table IMPLICIT NONE INTEGER, PARAMETER :: MAX = 9 INTEGER :: i, j CHARACTER(LEN=80) :: FORMAT FORMAT = "(9(2X, I1, A, I1, A, I2))" DO i = 1, MAX WRITE(*,FORMAT) (i, '*', j, '=', i*j, j = 1, MAX) END DO END PROGRAM Multiplication_Table</p> <p> 6 : PROGRAM Sorting IMPLICIT NONE INTEGER, PARAMETER :: MAX_SIZE INTEGER, DIMENSION(1:MAX_SIZE) INTEGER INTEGER</p> <p>= 100 :: InputData :: ActualSize :: i</p> <p>READ(*,*) ActualSize, (InputData(i), i = 1, ActualSize) WRITE(*,*) "Input Array:" WRITE(*,*) (InputData(i), i = 1, ActualSize) CALL Sort(InputData, ActualSize)</p> <p>WRITE(*,*) WRITE(*,*) "Sorted Array:" WRITE(*,*) (InputData(i), i = 1, ActualSize) CONTAINS INTEGER FUNCTION FindMinimum(x, Start, End) IMPLICIT NONE INTEGER, DIMENSION(1:), INTENT(IN) :: x INTEGER, INTENT(IN) :: Start, End INTEGER :: Minimum INTEGER :: Location INTEGER :: i Minimum = x(Start) Location = Start DO i = Start+1, End IF (x(i) &lt; Minimum) THEN Minimum = x(i) Location = i END IF END DO FindMinimum = Location END FUNCTION FindMinimum SUBROUTINE Swap(a, b) IMPLICIT NONE INTEGER, INTENT(INOUT) :: a, b INTEGER :: Temp Temp = a</p> <p>61</p> <p>a = b b = Temp END SUBROUTINE</p> <p>Swap</p> <p>SUBROUTINE Sort(x, Size) IMPLICIT NONE INTEGER, DIMENSION(1:), INTENT(INOUT) :: x INTEGER, INTENT(IN) :: Size INTEGER :: i INTEGER :: Location DO i = 1, Size-1 Location = FindMinimum(x, i, Size) CALL Swap(x(i), x(Location)) END DO END SUBROUTINE Sort END PROGRAM Sorting</p> <p>!</p> <p> 7 : PROGRAM ArmstrongNumber IMPLICIT NONE INTEGER :: a, b, c INTEGER :: abc, a3b3c3 INTEGER :: Count Count = 0 DO a = 0, 9 DO b = 0, 9 DO c = 0, 9 abc = a*100 + b*10 + c a3b3c3 = a**3 + b**3 + c**3 IF (abc == a3b3c3) THEN Count = Count + 1 WRITE(*,*) 'Armstrong number ', Count, ': ', abc END IF END DO END DO END DO END PROGRAM ArmstrongNumber</p> <p> 8 : 0001 integer o open(2,file="c:\1000.txt",status="replace") write(2,*) "1000 Rls 500 Rls 200 Rls 100 Rls 50 Rls 20 Rls 10 Rls" write(2,*) "-------- ------- ------- ------- ------ ------ ------" do 1 i=0,1 do 1 j=0,2 do 1 k=0,5 do 1 l=0,10 do 1 m=0,20 do 1 n=0,50</p> <p>!1000 rls !500 rls !200 rls !100 rls !50 rls !20 rls</p> <p>62</p> <p>do 1 o=0,100 if (i*1000+j*500+k*200+l*100+m*50+n*20+o*10==1000) then end if 1 continue 30 format (3x,I1,10x,I1,9x,I1,9x,I2,7x,I2,7x,I2,6x,I3) end</p> <p>!10 rls &amp; write(2,30) i,j,k,l,m,n,o</p> <p>. 9 : Read *,A Nmax=A ; Nmin=A Do Read *,A Nmax=Max(A,Nmax) ; Nmin=Min(A,Nmin) IF (A==0.) Goto 11 End do 11 print *,"Max=",Nmax,"Min=",Nmin End</p> <p>( 01 : )real ,allocatable :: f(:,:) real ,allocatable :: p(:) real A integer N character stat read *,N,A allocate(P(n+2)) allocate(f(-1:N+2,N+3)) do i= 1 , n+2 print *,"Please enter P",i,":" read *,P(i) pi=pi+p(i) end do Do j= 2,n+2 mp=mp+p(j)*(j-1)*A/2 end do t=(n-1)/2 g=floor(T) c2=mp/(g+1) c1=-c2+pi f(1,2)=c1*(1/sin(3.141592/3)) f(1,3)=-f(1,2)*cos(3.141592/3) f(0,2)=0 f(n+1,n+3)=0 f(n+2,n+1)=0 f(n+2,n+2)=0 do j=2, n+1 f(j,j+1)=-f(j-1,j)+p(j) f(j,j+2)=f(j-2,j)+f(j-1,j)*cos(3.141592/3)f(j,j+1)*cos(3.141592/3) end do print *,"-----------------------------------------------" print print print print *,"Calculation process completed" *,"c1=",C1 *,"c2=",C2 *,"=",50/SIN(3.141592/3)</p> <p>63</p> <p>print *,"Now is the time to Show th information" 1 read *,Nj print *,"-----------------------------------------------" if (F(nj,nj+2) ' Left, Right ! read in Left and Right</p> <p>fLeft = Funct(Left) ! compute their function values fRight = Funct(Right) WRITE(*,*) WRITE(*,*) 'Left = ', Left, ' f(Left) = ', fLeft</p> <p>74</p> <p>WRITE(*,*) 'Right = ', Right, ' f(Right) = ', fRight WRITE(*,*) IF (fLeft*fRight &gt; 0.0) THEN WRITE(*,*) '*** ERROR: f(Left)*f(Right) must be negative ***' ELSE Root = Solve(Left, Right, Tolerance) WRITE(*,*) 'A root is ', Root END IF CONTAINS ! ! ! ! ! ! ! ------------------------------------------------------------------REAL FUNCTION Funct() This is for function f(x). It takes a REAL formal argument and returns the value of f() at x. The following is sample function with a root in the range of -10.0 and 0.0. You can change the expression with your own function. ------------------------------------------------------------------REAL FUNCTION Funct(x) IMPLICIT NONE REAL, INTENT(IN) :: x REAL, PARAMETER :: PI = 3.1415926 REAL, PARAMETER :: a = 0.8475 Funct = SQRT(PI/2.0)*EXP(a*x) + x/(a*a + x*x) END FUNCTION ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! Funct</p> <p>------------------------------------------------------------------REAL FUNCTION Solve() This function takes Left - the left end, Right - the right end, and Tolerance - a tolerance value such that f(Left)*f(Right) &lt; 0 and find a root in the range of Left and Right. This function works as follows....</p>