由於大量的接觸fortran77,先學習fortran77吧
- 1.fortran不區分大小寫,fortran77一般全部大寫,感覺沒必要,不好讀,還是用大小寫混合着寫吧。后綴 .for 為固定格式的代碼,比如fortran77的代碼語法就是。另一種以 .f90 為后綴,是自由格式。
Program ex1 print *,'Hello world!' end
- 最好在程序開頭寫上implicit none語句,作用是不按照默認的數據類型。要是默認的則I和N之間所有字母開頭的變量都代表這個變量是整數類型。
1 Program ex1 2 implicit none 3 real x,y,z,aver 4 x=90.5 5 y=87.0 6 z=60.5 7 aver=(x+y+z)/3 8 print *,'the three number are:',x,y,z 9 print *,'average=',aver 10 end
但是注意所有變量要在所有執行語句之前聲明。
- 調用子函數:
在子函數中聲明與子函數同名的變量,記得要返回的值賦值給這個與子函數同名的變量。
1 c Main program,to calculate the area of the circle 2 Program ex2 3 implicit none 4 real x,y,z,areaofcircle 5 x=90.5 6 y=87.0 7 z=60.5 8 print *,'r=',x,'area of circle=',areaofcircle(x) 9 print *,'r=',y,'area of circle=',areaofcircle(y) 10 print *,'r=',z,'area of circle=',areaofcircle(z) 11 end 12 13 function areaofcircle(r) 14 implicit none 15 real r ,areaofcircle 16 areaofcircle=3.1415926*r**2 17 return 18 end
從上面可以看出,子函數更像數學里一個公式,其實areaofcircle可以用一個更簡單的f代替,就更好理解啦。
- 子例行函數
1 c Main program,to calculate the area of the circle 2 Program ex2 3 implicit none 4 real x,y,z,xarea,yarea,zarea 5 x=90.5 6 y=87.0 7 z=60.5 8 c 必須有call 9 call areaofcircle(x,xarea) 10 call areaofcircle(y,yarea) 11 call areaofcircle(z,zarea) 12 print *,'r=',x,'area of circle=',xarea 13 print *,'r=',y,'area of circle=',yarea 14 print *,'r=',z,'area of circle=',zarea 15 end 16 c 通過傳址方式回傳數值,利用了area的地址 17 subroutine areaofcircle(r,area) 18 implicit none 19 c 含有implicit none語句,那么本子程序所有變量都要顯示聲明 20 real r,area 21 area=3.1415926*r**2 22 end
2. 算法
- if:
1 program readGrade 2 implicit none 3 integer num,grade,i,j 4 i=3 5 j=1 6 print *,"請輸入",i,"個人的學號和成績:" 7 10 read *,num,grade 8 if(grade .GT. 80) then 9 print *,num,grade 10 end if 11 j=j+1 12 if (j .LE. i) goto 10 13 end
盡量不要用goto這種跳轉,多了邏輯會很亂。可以用do 代替。
由於visual studio 2013安裝的inter 的fortran編譯器兼容性比較好,在.for中寫fortran90的語法也不報錯,所以,大於號也可以直接寫 >,比如: if(grade > 80) then
判斷是否是閏年:
1 C 判斷是否是閏年,這種c開頭的注釋是fortran77語法 2 Program isYear 3 implicit none 4 integer year!year是整數類型; 以 ! 開頭的語句或!之后的部分是fortran90語法的注釋 5 !下面是執行語句 6 year=0 7 !下面是默認的讀入方式 8 print *,'請輸入年份,為整數:' 9 read *,year 10 if ( MOD(year,4) .EQ. 0) then 11 if (MOD(year,100) .NE. 0) then 12 print *,year,'年是閏年,1' 13 else 14 print *,year,'年不是閏年,1' 15 endif 16 else 17 if (MOD(year,400) .EQ. 0) then 18 print *,year,"年是閏年,2" 19 else 20 print *,year,'年不是閏年,2' 21 endif 22 endif 23 24 end
- 計算1-1/2+1/3-...-1/100:
1 program computeSum 2 implicit none 3 real sum 4 integer i 5 sum=0.0 6 do 10 i=1, 100 7 if (mod(i,2) .EQ. 0) then 8 sum=sum-1.0/i 9 else 10 sum=sum+1.0/i 11 10 end if 12 print *,sum 13 14 end

1 program computeSum 2 implicit none 3 real sum 4 integer i 5 sum=0.0 6 do 10, i=1, 100,1!10后面的逗號要不要都行 7 if (mod(i,2) .EQ. 0) then 8 sum=sum-1.0/i 9 else 10 sum=sum+1.0/i 11 10 end if!fortran77中不讓這樣,但也可以執行,兼容了 12 print *,sum 13 sum=0 14 do 20, i=1, 100,1!10后面的逗號要不要都行 15 if (mod(i,2) .EQ. 0) then 16 sum=sum-1.0/i 17 else 18 sum=sum+1.0/i 19 end if 20 20 continue!最好這樣 21 print *,sum 22 23 end
--------------
1 program mymath 2 !double 比 real 精度要高 3 double precision a,b!fortran77語法 4 a=11111.1 5 b=1111.11 6 print *,a*b 7 print *,sin(1.0) 8 print *,asin(1.0) 9 print *,log(3.0) 10 print *,int(8.6) 11 print *,mod(8,3) 12 print *,sign(8.5,-2.0)!將后一個數的符號給前一個數 13 print *,max(1.0,3.0,9.0,11.0,22.0,100.0)!求一系列數的最大值,但類型必須相同,都是實數 14 print *,max(1,2**10,9,11,22,100)!類型必須相同,都是實數,2**10代表2的10次方 15 print *,min(1,3,9,11,22,100) 16 !real 只能表示7位數 17 print *,0.001+123456.0-123455.0 18 print *,123456.0-123455.0+0.001 19 print *,1.0/3.0*3.0 20 print *,1,2 21 print *,sqrt(4.0),sqrt(9.0)!必須為實型數據 22 print *,sqrt(a),sqrt(b) 23 write(*,*) '123456789012345678901234567890'!整數占11列,兩個整數占23列,說明中間有個空格 24 25 end
--------------------------
1 program arrayStudy 2 double precision a(1:100000000)!一維數組,包含10個整形數組 3 double precision b(1:3,4:6)!二維數組,3行3列 4 !dimension b(1:3,4:6)!和上面聲明效果一樣,不過是多寫了一行 5 !double precision b 6 DATA C,D,E,F/-1.0,2.0,3.0,4.0/ 7 DATA G/2.0/,H/3.0/ 8 9 integer i 10 do 10 ,i=1,100000000 11 a(i)=i**2 12 !print *,a(i) 13 10 continue 14 print *,a(100000000) 15 end

1 Program ex1 2 print *,'Hello world!' 3 end