Fortran学习记录3(选择语句)

流程控制语句

if的基本用法

if(逻辑表达式)then
...
...       ! 当逻辑表达式为真时执行then代码块,否则执行end if之后的内容
...
end if

以上then代码块中如果只有一行程序代码,可以改写成下面的形式:

if(逻辑表达式) 一行表达式---------如:
if(a>3) a=a+1

if-else语句块

if(逻辑表达式)then
...
else
...
end if

下面利用该语句块写一个分段函数:

 program main
  C
    implicit real*8(a-z)
    real*8::x,f
  C  
    if(x<-1)then
    f=x**2+sin(x)
    else
    f=x**3-cos(x)
    end if
  C  
    print*,"please input a real number"
    read(*,*)x
    write(*,*)"the value of function is: ",f
  end program main

多重判断if-elseif语句

if可以配合else-if来做多重判断,多重判断可以一次列出多个条件以及多个程序模块,但其中最多只有一个成立。也就是说每次最多只有一个程序块被执行。

 if(条件1)then
  ...
  elseif(条件2)then
  ...
  elseif(条件3)then
  ...
  ...
  else   ! else这个条件模块可以省略,当前面都不成立时,才执行这个。
  ...
  ...
  end if

面写一个简单的程序,要求给出一个人的工资,计算他应该纳多少税。

 program main
  !------------------------------program comment
  ! Description   :这个小程序计算个人收入应上交税金
  !---------------------------------------
  ! Version      :V1.0
  !  Date         :20160731
  !  Coding by    :xiaodai
  !-----------------------------------------
  ! Input parameter  :wage,thr_ensure,house_pay,s_num
  ! Output parameter :tax,aftax
  !-----------------------------------------
  !
  implicit real*8(a-z)
  real*8::wage,temp,tax,aftax
  ! wage月工资,tax应纳税金,aftax税后工资
  !假设这个城市的三险一金标准是养老保险8%、医疗保险2%、失业保险1%、住房公积金8%
  !
  print*,"请输入您的月工资"
  read(*,*)wage
  temp=wage*(1-0.08-0.02-0.01-0.08)-3500
  !
  if(temp<=0)then
  tax=0
  elseif(temp<=1500)then
  tax=temp*0.03
  elseif(temp<=4500)then
  tax=1500*0.03+(temp-1500)*0.1+105
  elseif(temp<=9000)then
  tax=1500*0.03+(4500-1500)*0.1+(temp-4500)*0.2+555
  elseif(temp<=35000)then
  tax=1500*0.03+(4500-1500)*0.1+(9000-4500)*0.2+(temp-9000)*0.25+1005
  elseif(temp<=55000)then
  tax=1500*0.03+(4500-1500)*0.1+(9000-4500)*0.2+(35000-9000)*0.25+(temp-35000)*0.3+2755
  elseif(temp<=80000)then
  tax=1500*0.03+(4500-1500)*0.1+(9000-4500)*0.2+(35000-9000)*0.25+(55000-35000)*0.3+(temp-55000)*0.35+5505
  else
  tax=1500*0.03+(4500-1500)*0.1+(9000-4500)*0.2+(35000-9000)*0.25+(55000-35000)*0.3+(80000-55000)*0.35+(temp-80000)*0.45+13505
  end if
  !
  aftax=wage-tax
  write(*,*)"您需要上交的税金是:",tax
  write(*,*)"您的税后工资是:",aftax
  write(*,*)"您上交的税金占您的工资",tax/wage*100,"%"
  end program main

if语句嵌套

 if(...)then
      if(...)then
          if(...)then
              else if(...)then
                  else
                      ...
          end if
      end if
  end if

Select case语句

select case 是进行“多重判断”的有效而简明的语句,语法如下:

select case(变量)
case(数值1)
...
case(数值2)
...
case(数值3)
...
case(数值4)
...
...
case(数值n)
...
case deflaut
...
end select

下面给出一个有趣的例子:

program main
! --------------------------------------------------------program comment
! 描述:本程序要求用户给出任意一个成绩,返回给用户该成绩所对应的等级
! --------------------------------------------------------
implicit none
integer::score
print*,"please input your score"
read(*,*)score
select case(score)
case(90:100)
    write(*,*),"your grade is A !"
    case(80:89)
         write(*,*),"your grade is B !"
        case(60:79)
             write(*,*),"your grade is C !"
            case(0:59)
                 write(*,*),"your grade is D !"
                 case default
                     write(*,*),"There are something error !"
end select
end program

使用select case要注意的地方有:

  1. select case后面的参数只能使用整数(integer)、字符(character)、以及逻辑变量(logical),不能使用浮点数和复数。如果要使用浮点数来判断,则只能使用if-else-if语句。

  2. 每个case所使用的数值必须是常量,不能是变量。

  3. case后面可以使具体的数值,也可以是一个变化范围,也可以是几个独立的数字等,如下:

    case(1)        ! 变量等于1时,执行这个case
    case(1:)       ! 变量大于1时,执行这个case
    case(1:5)      ! 变量在1-5之间时,执行这个case
    case(1,3,5)    ! 变量等于1,3,5其中任何一个时,执行这个case
    case(:5)       ! 变量小于5时,执行这个case

下面是一个颇为有趣的小程序,摘自彭国伦的《Fortran95程序设计》一书的例子。

program main
    implicit none
    real*8:: a,b,ans
    character c

    write(*,*),"请按如下格式输入:数字,运算符,数字:"
    read(*,*),a
    read(*,"(a1)")c
    read(*,*)b

    select case (c)
    case('+')
        ans=a+b
        case('-')
            ans=a-b
            case('/')
                ans=a/b
                case('*')
                    ans=a*b
                    case default
                        write(*,"('Unknown operator',a1)") c
                        stop
    end select

    write(*,"(F16.2,a1,F16.2,'=',F16.2)")a,c,b,ans
    stop
end program

Goto语句

顾名思义,goto语句就是将程序跳转到要去的地方,这个语句的优点是可以让程序控制变得更灵活,但是缺点也很明显,就是它是的程序支离破碎,没有一定功力的人很难得驾驭。建议初学者尽量不要使用。

Fortran中,任何一行程序代码都可以有自己的标签(行标号),goto可以控制程序在不同行之间跳跃。

下面的程序用来计算个人的BMI指标。

program main
    ! ------------------------------------------------------program comment
    ! 改程序用来检测一个人的体重是否超标
    ! 中华人名共和国卫生行业标准成人体重判定
    ! ------------------------------------------------------
    implicit none
    real::height,weight,BMI

    write(*,*),"Please input your height(m) and weight (kg) : "
    read(*,*)height,weight
    BMI=weight/height**2

    if(BMI>=28) goto 100
    if(BMI>=24.AND.BMI<28) goto 101
    if(BMI>=18.5.AND.BMI<24)goto 102
    if(BMI<18.5)goto 103
 100   write(*,*),"You are too fat,guys . Please have a hleathy diet."
 stop
 101   write(*,*),"You are too weight,guys . "
 stop
 102   write(*,*),"You have a hleathy body."
 stop
 103   write(*,*),"You should take care of your weight."
end program

赶紧来试试你的健康状况吧。(上面的程序把stop去掉会是什么情况呢?请试试吧。)

goto还可以用来写循环。如下的程序计算一个数的阶乘。

program main
    ! -----------------------------------program comment
    ! 改程序用来计算一个数的阶乘(狭义阶乘)
    ! --------------------------------------
          real*8::num=1
          integer*4::a,i
          print*,"Please input a number: "
          read(*,*)i
          a=i
100    num=num*a
          a=a-1
         if(a>1) goto 100
         write(*,*) i,"'s factorial is ",num
end program

上面的小程序只能计算狭义的阶乘,要求输入的数字是正整数。

下面的程序中,goto可以提供多个跳转点共选择,更具有灵活性。该程序改编自彭国伦《Fortran95程序设计》一书,原书程序表示没有看懂,欢迎大家讨论、指导。

program main
    implicit none
    integer i,n
   print*,"请输入两个整数"
   read(*,*)i,n
! 当I/N=1时,goto 10,当I/N=2时,goto 20,当I/N=3时,goto 30,当I/N<1I/N>3时,不跳转,执行下一行
    goto(10,20,30) i/n
10    write(*,*) 'I/N=1'
     goto 100
20    write(*,*) 'I/N=2'
     goto 100
30    write(*,*) 'I/N=3'

100  stop
end program

同样的程序,改编自彭国伦《Fortran95程序设计》一书

program main
    implicit none
    real::a,b,c
    write(*,*),"Please input two number"
    read(*,*)a,b
    c=a-b
    ! c<0就goto 10,c=0就goto 20,c>0就goto 30
    if(c) 10,20,30
10     write(*,*)a,'<',b
         goto 40
20     write(*,*)a,'=',b
         goto 40
30     write(*,*)a,'>',b
         goto 40

40   stop
end program

PAUSE CONTINUE STOP

pause 程序执行到pause时会停止执行,直到用户按下”Enter” 键才会继续。

continue 就是继续往下执行代码的命令。

stop 用来结束程序

原文地址:https://www.cnblogs.com/baowee/p/9556831.html