program main implicitnone type student character(len=20)::department character(len=10)::class character(len=15)::name integer::number endtype student type(student)::person person=student("computer","92_2","lilin",21) write(*,*) person%class,person%name end
program main implicitnone real::A=1.5,B=2.0,C=1.2,D=7.5,X=3.0,Y=5.0 logical::L1=.true. print *,A>2.8*B.and.X>=Y.or.L1.and..not.(3.6-C)*2>=D/2.5 end ! F
program main implicitnone real,parameter::pi=3.14 integer,parameter::m=50 integer::a,b real::r,ans read *,a,b,r ans=m*(a*b-4*pi*r*r) print *,ans endprogram main
! 5 3 0.5 ! 593.000000
子程序
函数子程序、子例子程序
函数子程序function
[recursive] function f(x1,x2) result(y) implicit none end [function [f]]
function factor(N) result(fac_result) implicitnone integer::N,fac_result,i fac_result=1 do i=1,N fac_result=fac_result*i enddo end
program cal_factor implicitnone integer::factor,s=0,i do i=4,8 s=s+factor(i) enddo print *,'S=',s end
输出s=46224
练习 编写子程序,求
function item(n,x) result(item_result) implicitnone integer::n,i real::x,item_result,T T=0 do i=1,n T=T+i enddo item_result=x**n/T end program main implicitnone integer::n,i real::item,x,s read *,n,x s=0 do i=1,n s=s+item(i,x) enddo print *,s end
子例子程序subroutine
[recursive] subroutine f[(x1,x2)] implicit none end [subroutine [f]]
call f(x1,x2)
给虚元加上intent(in)或者intent(out)属性,限制输入变量或者输出变量(不必须)
integer,intent(in)::n integer,intent(out)::s
练习 使用子例子程序求
subroutine fact(n,value) implicitnone integer,intent(in)::n integer::i integer,intent(out)::value value=1 do i=1,n value=value*i enddo end program main implicitnone integer::s=0,i,value do i=1,5 call fact(i,value) s=s+value enddo print *,s end
练习 使用子程序求,要求的通项小于1e-6时停止
subroutine fact(n,value) implicitnone integer,intent(in)::n integer::i real(kind=selected_real_kind(16,100)),intent(out)::value value=1 do i=1,n value=value*i enddo end program main implicitnone real(selected_real_kind(16,100))::s=0,value,exp_=0,x,sinh_ integer::i=2 read *,x s=1+x do call fact(i,value) exp_=x**i/value s=s+exp_ if (exp_<=1e-6) then exit endif i=i+1 enddo sinh_=(s-(1/s))/2 print *,sinh_ end
! 5 ! 74.2032105178825331163
接口块interface
类型声明之前,implicit none语句之后
interface function func(x) result(f) real::x,f end function end interface
if 条件 then 语句块 else if 条件 then 语句块 else 语句块 end if
例 输入三角形三边,输出边长或不能构成三角形。
例 排序:给定输入的abcd,按升序输出。
program main implicitnone real::a,b,c,d,t read *,a,b,c,d if (a>b) then t=a a=b b=t endif if (c<=a) then t=c c=b b=a a=t elseif (a<=c .and. c<=b) then t=b b=c c=t endif if (d<=a) then print *,d,a,b,c elseif (a<=d .and. d<=b) then print *,a,d,b,c elseif (b<=d .and. d<=c) then print *,a,b,d,c else print *,a,b,c,d endif end
case结构
情况表达式为数值型(只能整型):
program main implicitnone integer::s read *,s selectcase(s) case(90:100) print *,'A' case(80:89) print *,'B' casedefault print *,'N' endselect end
情况表达式为字符型(或者字符数组):
program main implicitnone character::s read *,s selectcase(s) case('A') print *,'90:100' case('B') print *,'80:90' casedefault print *,'None' endselect end
情况表达式为逻辑型:
program main implicitnone integer::s read *,s selectcase(s>=90) case(.true.) print *,'A' case(.false.) print *,'B' endselect end
练习 输入一个数,判断能否被3整除。
! if program main implicitnone integer::x read *,x if (mod(x,3)==0) then print *,'Yes' else print *,'No' endif end
! case logical program main implicitnone integer::x read *,x selectcase (mod(x,3)==0) case(.true.) print *,'Yes' case(.false.) print *,'No' endselect end
! case integer program main implicitnone integer::x read *,x selectcase (mod(x,3)) case(0) print *,'Yes' casedefault print *,'No' endselect end
练习 求一元二次方程的根
program main real::a,b,c,delta read *,a,b,c delta=b**2-4*a*c if (a==0) then if (b==0) then if (c==0) then print *,"Any" else print *,"None" endif else print *,-(c/b) endif else if (delta>0) then print *,(-b+sqrt(delta))/2*a,(-b-sqrt(delta))/2*a elseif (delta==0) then print *,-(b/2*a) else print *,cmplx(-(b/2*a),sqrt(-delta)/2*a),cmplx(-(b/2*a),-sqrt(-delta)/2*a) endif endif end
program main implicitnone integer,dimension(:),allocatable::A !声明动态数组 integer::max,k,i,n read *,n allocate(A(n)) !分配存储空间 read *,A max=A(1) k=1 do i=2,n if (A(i)>max) then max=A(i) k=i endif enddo print *,max,k deallocate(A) !释放空间 end
subroutine sort(A) implicitnone integer,dimension(9)::A integer::i,j,t,flag do j=1,8 flag=0 do i=1,8-j+1 if (A(i)>A(i+1)) then t=A(i);A(i)=A(i+1);A(i+1)=t ! print '(9I2)',A flag=1 endif enddo if (flag==0) then exit endif enddo end program main integer,dimension(8,9)::D integer,dimension(9)::S=0 integer::i,s_l interface subroutine sort(A) implicitnone integer,dimension(9)::A integer::i,j,t,flag end endinterface read *,(D(i,:),i=1,8) do i=1,9 s_l=0 do j=1,8 s_l=s_l+D(j,i) enddo S(i)=s_l enddo print'(9I3)',(S) call sort(S) print'(9I3)',(S) end
选择排序
subroutine sort(A) implicitnone integer,dimension(9)::A integer::i,j,t do j=1,8 do i=j+1,9 if (A(j)>A(i)) then t=A(j);A(j)=A(i);A(i)=t endif enddo enddo end
recursivefunction gcd(a,b) result(g) implicitnone integer::a,b,g if (mod(a,b)==0) then g=b else g=gcd(b,mod(a,b)) endif end recursivefunction lcm(a,b,c) result(y) implicitnone integer::a,b,y,gcd,t0 integer,optional::c if (present(c)) then t0=lcm(a,b) y=t0*c/gcd(t0,c) else y=a*b/gcd(a,b) endif end
接下来便可求解。首先需要将t1,t2,t3排序。
if (t1>t2) then t=t1;t1=t2;t2=t endif if (t3<t1) then t=t3;t3=t2;t2=t1;t1=t elseif (t3<t2) then t=t2;t2=t3;t3=t endif
recursivefunction gcd(a,b) result(g) implicitnone integer::a,b,g if (mod(a,b)==0) then g=b else g=gcd(b,mod(a,b)) endif end recursivefunction lcm(a,b,c) result(y) implicitnone integer::a,b,y,gcd,t0 integer,optional::c if (present(c)) then t0=lcm(a,b) y=t0*c/gcd(t0,c) else y=a*b/gcd(a,b) endif end program main implicitnone interface recursivefunction lcm(a,b,c) result(y) integer::a,b,y,gcd,t0 integer,optional::c endfunction endinterface integer::t1,t2,t3,n,t integer::t1t,t2t,t3t,t12,t13,t23,t123,n1,n2,n3 read *,t1,t2,t3,n if (t1>t2) then t=t1;t1=t2;t2=t endif if (t3<t1) then t=t3;t3=t2;t2=t1;t1=t elseif (t3<t2) then t=t2;t2=t3;t3=t endif t1t=t1*(n-1) t2t=t2*(n-1) t3t=t3*(n-1) t12=lcm(t1,t2) t13=lcm(t1,t3) t23=lcm(t2,t3) t123=lcm(t1,t2,t3) n1=n+(t1t/t2)+(t1t/t3)-t1t/t12-t1t/t23-t1t/t13+t1t/t123 n2=(n-t1t/t2-1)+(t2t/t3-t1t/t3)-max(((t2t-t1t+mod(t1t,t23))/t23),0) n3=n-t2t/t3-1 print *,n1+n2+n3 end
使用动态数组进行模拟方法如下:
program main implicitnone integer::t1,t2,t3,n,t,i,s=0 integer,dimension(:),allocatable::A read *,t1,t2,t3,n t=max(t1,t2,t3)*(n-1)+1 allocate(A(t)) A=0 A(1)=1 A(1:t1*n:t1)=1 A(1:t2*n:t2)=1 A(1:t3*n:t3)=1 do i=1,t s=A(i)+s enddo deallocate(A) print *,s end