zoukankan      html  css  js  c++  java
  • 实战fortran77基础语法2

    由于大量的接触fortran77,先学习fortran77吧

    1. 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
    fortran77.for
     
  • 相关阅读:
    固定textview大小,根据文字多少调整字体自适应textview大小
    这可能是最全的禁用win10自动更新了
    Android Studio 3.0正式版填坑之路
    android studio 3.0之后版本自定义文件名生成apk文件
    Android Studio从2.3升级到3.1注意事项
    Android studio2.3.3升级3.1.2坑
    iterm2 快捷键大全
    php实现的短网址算法分享
    mysql远程连接命令
    Mac下安装与配置Go语言开发环境
  • 原文地址:https://www.cnblogs.com/zhubinglong/p/8666412.html
Copyright © 2011-2022 走看看