fortran习题课

合集下载

Fortran练习经典题目

Fortran练习经典题目

Fortran练习经典题目a, 输入一个年、月、日并计算它是本年度的第几天。

b.输入任意一年份,给出该年出生人的属相,如1945年出生的人的属相为“鸡”。

c.显示输出2000~2099年的任何一年的某一月的月历,所要显示的年月有键盘输入。

如2002年5月的月历形式如下;5月2002年日一二三四五六1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31d.显示输出2000~2099年的任何一年的年历,并写入一文本文件中。

e.以上功能都应从菜单中选择执行。

提示:a中若y为年份,d是该年的某月某日从1月1日开始经过的天数,则s=(y-1)*1.2425+d。

若s除以7后所得的余数取整后为0,则为周日,为1则周一。

program firstquestionimplicit noneinteger::q,i,ldayinteger::year,month,day,days,mday,dscharacter(len=2),dimension(1:37)::tem! 这个程序可以同时在屏幕和文本中输出计算结果。

为了计算不同的问题,引入变量q,当q=1时,计算某年月日是此年的第几天;! 当q=2时,计算某年的属相;当q=3时,计算月历;当q=4时,计算年历;当q=0时,退出此程序。

此外,此程序还设置了对输入! 年、月、日的判断,判断其是否合法。

print *,'when q=1,this program can resolve the question thatwhich day some day is in some year! this is the question one.' print *,'when q=2,this program can obtain the shengxiao of some year! this is the question two.'print *,'when q=3,this program can obtain the calendar of some month of some year(2000-2099)! this is the question three.' print *,'when q=4,this program can obtain the Almanac of some year(2000-2099)! this is the question four.'print *,'If you want to exit from this program,please input q=0.'doprint *,'input the number of q:'read *,qif(q==0) exitif (q==1) then2000 print *,'input the year, the month and the day:'read *,year,month,dayif (month>12.or.month<0) print *,'you input wrong month.' if (month>12.or.month<0) goto 2000call leapday(year,month,day,lday)if (lday==0) print *,'you input wrong day.'if (lday==0) goto 2000call numday(year,month,day,days)print '(a14,i3,a18)','The day is the',days,' day of this year.'open(25,file='days.dat',status='unknown')write(25,'(a14,i3,a18)') 'The day is the',days,' day of this year.' close(25)else if (q==2) thenprint *,'input the year:'read *,yearcall shengxiao(year)else if (q==3) then2001 print *,'input the year(2000-2099) and the month:'read *,year,monthif (year>2099.or.year<2000) print *,'you input wrong year,please input the year between 2000 and 2099.'if (year>2099.or.year<2000) goto 2001if (month>12.or.month<0) print *,'you input wrong month.' if (month>12.or.month<0) goto 2001open(27,file='yueli.dat',status='unknown')call nyli(year,month,mday,ds,tem)write(27, "(26x,i2,'月',3x,i4,'年')")month,yearwrite(27, '(18x,7a4)')'日','一','二','三','四','五','六'write(27, '(18x,7a4)')(tem(i),i=1,mday+ds)write(27,*)close(27)else if (q==4) then2002 print *,'input the year(2000-2099):'read *,yearif (year>2099.or.year<2000) print *,'you input wrong year,pleade input the year between 2000 and 2099.' if (year>2099.or.year<2000) goto 2002open(28,file='nianli.dat',status='unknown')do month=1,12call nyli(year,month,mday,ds,tem)write(28, "(26x,i2,'月',3x,i4,'年')")month,yearwrite(28, '(18x,7a4)')'日','一','二','三','四','五','六'write(28, '(18x,7a4)')(tem(i),i=1,mday+ds)write(28,*)end doclose(28)elseprint *,'you input the wrong number of q.' end ifend doend program firstquestionsubroutine shengxiao(year)implicit noneinteger::year,m,nm=year-1945if (m>=12.or.m<=-12) thenn=mod(m,12)elsen=mend ifopen(26,file='shengxiao.dat',status='unknown') if (n==0) thenprint *,'The shengxiao of this year is:鸡'write(26,*)'The shengxiao of this year is:鸡' else if(n==1.or.n==-11) thenprint *,'The shengxiao of this year is:狗'write(26,*)'The shengxiao of this year is:狗' else if(n==2.or.n==-10) thenprint *,'The shengxiao of this year is:猪'write(26,*)'The shengxiao of this year is:猪' else if(n==3.or.n==-9) thenprint *,'The shengxiao of this year is:鼠'write(26,*)'The shengxiao of this year is:鼠' else if(n==4.or.n==-8) thenprint *,'The shengxiao of this year is:牛'write(26,*)'The shengxiao of this year is:牛'else if(n==5.or.n==-7) thenprint *,'The shengxiao of this year is:虎'write(26,*)'The shengxiao of this year is:虎' else if(n==6.or.n==-6) thenprint *,'The shengxiao of this year is:兔'write(26,*)'The shengxiao of this year is:兔' else if(n==7.or.n==-5) thenprint *,'The shengxiao of this year is:尨'write(26,*)'The shengxiao of this year is:尨' else if(n==8.or.n==-4) thenprint *,'The shengxiao of this year is:蛇'write(26,*)'The shengxiao of this year is:蛇' else if(n==9.or.n==-3) thenprint *,'The shengxiao of this year is:马'write(26,*)'The shengxiao of this year is:马' else if(n==10.or.n==-2) thenprint *,'The shengxiao of this year is:羊'write(26,*)'The shengxiao of this year is:羊' else if(n==11.or.n==-1) thenprint *,'The shengxiao of this year is:猴'write(26,*)'The shengxiao of this year is:猴' end ifclose(26)end subroutine shengxiaosubroutine nyli(year,month,mday,ds,tem) implicit noneinteger::year,month,days,mday integer::i,m,ds,dts,qreal::scharacter(len=2),dimension(1:37)::temcall numday(year,month,1,days)call monday(year,month,mday)s=(year-1)*1.2425+daysds=int(s-int(s/7)*7)do i=1,37tem(ds+1)='1';tem(ds+11)='11';tem(ds+12)='12';tem(ds+2)='2';tem(ds+13)='13';tem(ds+14)='14';tem(ds+3)='3';tem(ds+15)='15';tem(ds+16)='16';tem(ds+4)='4';tem(ds+17)='17';tem(ds+18)='18';tem(ds+5)='5';tem(ds+19)='19';tem(ds+20)='20';tem(ds+6)='6';tem(ds+21)='21';tem(ds+22)='22';tem(ds+7)='7';tem(ds+23)='23';tem(ds+24)='24';tem(ds+8)='8';tem(ds+25)='25';tem(ds+26)='26';tem(ds+9)='9';tem(ds+27)='27';tem(ds+28)='28';tem(ds+10)='10';tem(ds+29)='29';tem(ds+30)='30';tem(ds+ 31)='31'; print "(26x,i2,'月',3x,i4,'年')",month,yearprint '(18x,7a4)','日','一','二','三','四','五','六'print '(18x,7a4)',(tem(i),i=1,mday+ds)print *end subroutine nylisubroutine numday(year,month,day,days)implicit noneinteger::year,month,day,days,nlogical::leapcall leapyear(year,leap)n=int(month/2)if (month<=8) thenif (mod(month,2)/=0) thendays=30*n+31*n+dayelsedays=30*(n-1)+31*n+dayend ifelseif (mod(month,2)/=0) thendays=30*(n-1)+31*(n+1)+dayelsedays=30*(n-1)+31*n+dayif(month>2) thenif (leap) thendays=days-1elsedays=days-2end ifend ifend subroutine numdaysubroutine monday(year,month,mday) implicit none integer::year,month,mday logical::leapcall leapyear(year,leap)select case (month)case (4,6,9,11)mday=30case (1,3,5,7,8,10,12)mday=31case (2)for_feb:select case (leap)case (.true.)mday=29case (.false.)mday=28end select for_febcase defaultend selectend subroutine monday subroutine leapyear(year,leap)implicit noneinteger::yearlogical::leapif (mod(year,4)/=0) thenleap=.false.else if (mod(year,100)/=0) thenleap=.true.else if (mod(year,400)/=0) thenleap=.true.elseleap=.false.end ifend subroutine leapyearsubroutine leapday(year,month,day,lday) implicit none integer::year,month,day,lday logical::leapcall leapyear(year,leap)select case (month)case (4,6,9,11)if (day>30.or.day<1) thenlday=0elselday=1end ifcase (1,3,5,7,8,10,12)if (day>31.or.day<1) thenlday=0elselday=1end ifcase (2)for_feb:select case (leap)case (.true.)if (day>29.or.day<1) thenlday=0elselday=1end ifcase (.false.)if (day>28.or.day<1) thenlday=0elselday=1end ifend select for_febcase defaultend selectend subroutine leapday素数是只能被1与其本身整除的数,下面给出一种寻找素数的方法:a生存一个数组,将其所有的元素初始化为1b 从数组下标为2的元素开始,每次寻找数值为1 的数组元素。

fortran例题6

fortran例题6

工程分析程序设计上机作业(六)上机目的:练习派生类型的定义、构造、初始化,成员的调用和操作,操作符的重载,指针1、构造一个由年、月、日组成的派生类型,并编写程序,实现从键盘上输入年、月、日,由程序计算出是当年度的第几天。

例如:输入:2011, 4, 8输出:4月8日是2011年的第98天。

注意:需考虑闰年的情形。

program maintype day !day 是年、月、日组成的派生类型integer yearinteger monthinteger dateend typetype(day)::dinteger i,j,k,n,m ! n 为天数n=0print*,"please imput the year month date"read*,d%year,d%month,d%date!判断是否为闰年if(mod(d%year,4)==0.and.mod(d%year,100)/=0.or.mod(d%year,400)==0) thendo i=1,d%month-1 !31天的月1 3 5 7 8 10 12,30天4 6 9 11 ,29天2if(i==1.or.i==3.or.i==5.or.i==7.or.i==8.or.i==10.or.i==12) thenn=n+31else if(i==2) thenn=n+29elsen=n+30end ifend doelse !不是闰年do i=1,d%month-1 !31天的月1 3 5 7 8 10 12,30天4 6 9 11 ,28天2if(i==1.or.i==3.or.i==5.or.i==7.or.i==8.or.i==10.or.i==12) thenn=n+31else if(i==2) thenn=n+28elsen=n+30end ifend doend ifn=n+d%datewrite(*,"(I5,'年',I3,'月',I3,'日是第',I3,'天')",advance='no') d%year,d%month,d%date,nend2、设计一个“-”操作符的重载,把集合A中那些同时又出现在集合B中的元素去掉。

fortran课本习题答案

fortran课本习题答案

fortran课本习题答案
Fortran课本习题答案
在学习Fortran编程语言的过程中,课本习题答案是非常重要的。

它们不仅可以帮助我们更好地理解和掌握知识,还可以帮助我们提高编程能力和解决问题的
能力。

首先,课本习题答案可以帮助我们检验自己的学习成果。

通过完成课本习题并
对比答案,我们可以了解自己在学习中的掌握程度,发现自己的不足之处,从
而有针对性地进行学习和提高。

其次,课本习题答案还可以帮助我们更好地理解知识点。

在完成习题的过程中,我们可能会遇到一些困难和疑惑,而课本习题答案可以为我们提供参考和解答,帮助我们更好地理解和掌握知识点。

此外,课本习题答案还可以帮助我们提高编程能力。

通过不断地完成习题并对
比答案,我们可以积累更多的编程经验,提高自己的编程能力,同时也可以学
习到一些常见的编程技巧和方法。

总之,课本习题答案对于我们学习Fortran编程语言是非常重要的。

它们不仅可以帮助我们检验学习成果,更可以帮助我们更好地理解知识点,提高编程能力,是我们学习的重要辅助工具。

希望大家能够充分利用课本习题答案,不断提高
自己的编程水平,取得更好的学习成绩。

fortran课后习题第五章

fortran课后习题第五章

第五章第一题program wd51implicit nonereal :: areal :: bwrite(*,*) "请输入一位上班族的月收入a="read(*,*) aif (a<=1000) thenb=a*0.03else if(a>=1000 .and. a<=5000) thenb=a*0.1elseb=a*0.15end ifwrite(*,*)"这位上班族所应缴纳的税金b=",bstopend第二题program wd52implicit noneinteger :: weekwrite(*,*)"请输入星期来查询当天晚上的节目week=" read(*,*) weekif(week==1 .or. week==4) thenwrite(*,*) "新闻"else if(week==2 .or. week==5) thenwrite(*,*) "电视剧"else if(week==3 .or. week==6) thenwrite(*,*) "卡通片"elsewrite(*,*) "电影"end ifend第三题program wd53implicit noneinteger age,moneyreal taxwrite(*,*) "请输入一位上班族的年龄="read (*,*) agewrite(*,*) "输入他的年收入="read (*,*) moneyif (age<50) thenif(money<=1000) thentax=money*0.03else if(money>1000 .and. money<5000) thentax=money*0.1elsetax=money*0.15end ifelse if (age>=50) thenif(money<=1000) thentax=money*0.05else if(money>1000 .and. money<5000) thentax=money*0.07elsetax=money*0.1end ifend ifwrite(*,*) "这位上班族所缴纳的税金是=",tax stopend第四题program wd54implicit noneinteger year,daylogical :: a,b,cwrite(*,*) "请输入一个公元年份="read (*,*) yeara=(MOD(year,4)==0)b=(MOD(year,100)==0)c=(MOD(year,400)==0)if((a.NEQV.b) .or. c) thenday=366elseday=365end ifwrite(*,*)"一年当中有",day,"天" stopend。

fortran课后习题答案

fortran课后习题答案

fortran课后习题答案Fortran课后习题答案Fortran是一种编程语言,广泛应用于科学计算和工程领域。

学习Fortran的过程中,课后习题是非常重要的一部分,通过解答这些习题,可以巩固所学的知识,并且提升自己的编程能力。

在本文中,我将为大家提供一些Fortran课后习题的答案,希望对大家的学习有所帮助。

1. 编写一个Fortran程序,计算并输出1到100之间所有偶数的和。

```fortranprogram sum_even_numbersimplicit noneinteger :: i, sumsum = 0do i = 1, 100if (mod(i, 2) == 0) thensum = sum + iend ifend doprint *, "The sum of even numbers from 1 to 100 is", sumend program sum_even_numbers```2. 编写一个Fortran程序,计算并输出用户输入的两个数的乘积。

```fortranprogram multiply_numbersimplicit nonereal :: num1, num2, productprint *, "Enter the first number:"read *, num1print *, "Enter the second number:"read *, num2product = num1 * num2print *, "The product of", num1, "and", num2, "is", productend program multiply_numbers```3. 编写一个Fortran程序,计算并输出用户输入的一个数的平方根。

fortran课后习题

fortran课后习题

5.3 program main implicit none integer nian,shouru real suijin read(*,*) nian read(*,*) shouru if (nian<50.and.nian>0) then if (shouru>=1000 .and. shouru<=5000) then suijin=shouru*0.1 end if if (shouru>5000) then suijin=shouru*0.15 end if if (shouru<1000) then suijin=shouru*0.03 end if else if (nian>50) then if (shouru>=1000 .and. shouru<=5000) then suijin=shouru*0.07 end if if (shouru>5000) then suijin=shouru*0.1 end if if (shouru<1000) then suijin=shouru*0.05 end if end if write(*,*)"应缴纳的税金金额为:" write(*,*) suijin end 5.4 program main implicit none integer day write(*,*) "请输入一个公元的年份:" read(*,*) day if (mod(day,4)==0) then
write(*,*)"一年有366天" else if(mod(day,100)==0.and.mod(day,400)==0) then write(*,*)"一年有366天" else write(*,*)"一年有365天" end if End 6.1 method one: program main implicit none integer counter do counter=1,5 write(*,*)"Fortran" end do end method two: program main implicit none integer counter counter=1 do while(counter<=5) write(*,*)"Fortran" counter=counter+1 end do end 6.2 program main implicit none integer counter integer::ans=0 counter=1 do while(counter<100) ans=ans+counter counter=counter+2 end do

Fortran平时编程练习及答案(上)

Fortran平时编程练习及答案(上)

1:编程找出并输出100—150之间和400—450之间能被9整除的数。

程序:program ex0401implicit noneinteger ido i=100, 150if(mod(i,9).eq.0) thenwrite(*,10) i10 format(i3)end ifend dodo i=400, 450if(mod(i,9).eq.0) thenwrite(*,20) i20 format(i3)end ifend doend运行结果:2:回文数是指正读与反读都一样的数。

如:232,编程求100到999之间的回文数。

程序:program ex0402implicit noneinteger ainteger cinteger ido 10 i=100,999a=i/100 !百位上的数字c=mod(i,10) !个位上的数字if(a.eq.c) thenwrite(*,20) i20 format(i8)endif10 continueEnd运行结果:3:输出所有水仙花数(水仙花数是指一个三位正整数,其各位数字的立方和等于该数本身)编程:program ex0403implicit noneinteger ainteger binteger cinteger iwrite(*,*)'水仙花数:'do 10 i=100,999a=i/100 !百位上的数字b=mod(i,100)/10 !十位上的数字c=mod(i,10) !个位上的数字if(i.eq.a**3+b**3+c**3) thenwrite(*,20)i,a,b,c20 format(5x,i3,'=',i2,'^3+',i2,'^3+',i2,'^3')endif10 continueend运行结果:4:编程输出如下图形** * ** * * * ** * * * * * ** * * * ** * **编程:program ex0404implicit noneinteger i,jcharacter a(7)do i=1,7 !输入数组a(i)='*'end dodo j=1,4 !前四行write(*,'(<3*(4-j)>x,10a3)')(a(i),i=1,2*j-1)end dodo j=3,1,-1 !后三行write(*,'(<3*(4-j)>x,10a3)')(a(i),i=1,2*j-1)end doend运行结果:5:输入n个数,找出所有大于n个数的平均值的那些数及最小数。

Fortran95第一章第六大题习题与答案

Fortran95第一章第六大题习题与答案

1. 从键盘输入a,b,c 的值,计算f=cos |a+b |/sin |b||a|++tan c 上机执行该程序,输入a=-4.6°,b=10°,c=21.85°,观察计算结果。

Program ex1_1implicit nonereal a,b,c,fprint*,'请输入a,b,c(角度值)'read*,a,b,ca=a*3.14159/180.0b=b*3.14159/180.0c=c*3.14159/180.0f=cos(abs(a+b))/sin(sqrt(abs(a)+abs(b)))+tan(c)write(*,*)'f=',fstopEnd2.设圆锥体底面半径r 为6,高h 为5,从键盘输入r 、h ,计算圆锥体体积。

计算公式为V=32h r π。

Program ex1_2implicit nonereal r,h,vprint*,'请输入r,h 的值'read*,r,hv=3.14159*r*r*h/3write(*,*)'v=',vstopEnd3.求一元二次方程02=++c bx ax 的两个根1x 和2x 。

方程的系数a 、b 、c 值从键盘输入并假定042>-ac b 。

Program ex1_3implicit nonereal a,b,c,x1,x2print*,'请输入a,b,c 的值'read*,a,b,cx1=(b+sqrt(b*b-4*a*c))/2*ax2=(b-sqrt(b*b-4*a*c))/2*awrite(*,*)'x1=',x1,'x2=',x2stopEnd4.从键盘输入一个三位十进制整数,分别输出其个位、十位、百位上的数字。

Program ex1_4implicit noneinteger xprint*,'请输入一个三位十进制整数'read*,xwrite(*,*)'个位数=',mod(x,10)write(*,*)'十位数=',mod(x/10,10)write(*,*)'百位数=',x/100stopEnd5.已知ysin(⋅)+=+,分别计算等号两边的算式并输出计算⋅sinyxcosxycosx sin结果(x=30°,y=45°从键盘输入)。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
程序改错(5*2=10)
编程序(20*1=20)
主要内容
Fortran基础知识
变量类型与表达式 例程和针
简单Fortran 90程序的构造形式:
[PROGRAM 程序名]
[声明语句] [执行语句] END [PROGRAM [MONEY]] 一个程序,只有end不能省,其他都是可选的
Example
正确的写法 axis1=1; axis2=2; axis3=3 Area = sqrt(half * (half - axis1) * (half - &
axis2) * (half - axis3)) Area = sqrt(half * (half - axis1) * (half- & & axis2) * (half - axis3))
复数型(COMPLEX)
复数型变量
– (r, i) – 复数型常量的类型参数,取实部和虚部的实数种类参数的极 大值
复数由实部和虚部组成,z=x+yi,x为实部,y为虚部,
x、y皆为实型变量 设定 z = (x, y) ! a = x + iy z = (1.5, 2.5) ! a = 1.5 + 2.5i z = 1.5 ! a = 1.5 + 0i Z= (0, 2.5) ! a= 0 + 2.5i
声明
Example:
implicit integer(A,B,C) ! A,B,C开头的变量都视为整数 implicit integer(A-F,I,K) ! A到F及I,K开头的变量都视为 整数 implicit real(M-P) ! M到P开头的变量都视为浮点数 implicit none ! 关闭隐含规则,所有的变量都要事先声 明
例子
―WHAT‘S YOUR NAME?‖
‗WHAT‘‘S YOUR NAME?‘ ― ‖
―Can I help you?‖
―New world record!!!‖
算术表达式

运算符 + * / 优先级增加 ** () 运算级相同:从左至右 连续的乘幂运算:从右至左 计算的结果是把“=”右边的结果传给左边的变 量 整数之间的除法,结果也是整数! 如果不能整除,小数部分无条件舍去!
逻辑型(LOGICAL)
逻辑型变量 – LOGICAL L – LOGICAL([KIND=]N) L 逻辑型常量 – .TRUE.(逻辑真) – .FALSE.(逻辑假)
字符型(CHARACTER)
字符型变量 CHARACTER [([LEN=]len)] CHARACTER [([LEN=]len [, [KIND=]n])] CHARACTER [(KIND=n [, LEN=len])] 字符串有两个可选参数:长度参数、种类参数 种类参数总是1,即一个字符占一个存储字节 假如两个可选参数都没给出,缺省值取1: character c 若只给出一个参数,则代表长度参数:character(20) c 若给出两个参数,则依次为长度参数和种类参数(种 类参数智能为1):character(20, 1) c 若采取关键字声明形式,则参数的顺序可以任意: character(KIND=1, LEN=20) c
所有的声明语句必须在执行语句之前
一个项目中只能有一个主程序
Fortran 77 & Fortran 90
Fortran 77
变量名长度 书写格式 行宽 注释 续行标志 6 固定格式 7~72 第一列为 C/c/*
第六列0以外的 任何字符
Fortran 90
31 自由格式 0~132 任意位置! &
变量名(Variable name)
有效的变量名
area distance z123456789 long_axis_of_elliptic_circle a3b4c5 abstract_3 TimeAndSpace century____
area,AREA,aREa等等都是一个变量
变量名(Variable name)
Fortran 课程要点总结
2014/7/12
2012-2013学年第二学期《工程分析程序设计》考试安排 考试时间:2012年6月16日( 周日晚) 晚7:00-9:30 班级 考试地点 C204 主监 副监
题型
填空(2*10=20)
判断正误(2*5=10) 选择(2*10=20)
读程序,回答问题(10*2=20)
例子
写出10以内的素数的逻辑表达式 分析: – 要求的数应该同时满足以下条件,即为并的关系
• • • • 大于1 小于10 除以2余数不为0 除以3余数不为0
N.GT.1.AND.N.LT.10.AND.MOD(N,2).NE.0.A
ND.MOD(N,3).NE.0
例子
写出1000以内的含有3或者能被3整除的整数 分析:
无效的变量名
This_is _a_very_long_variable_name 3_day $600 my-help exchange rate U.S.A Ask? ―UK‖ b/a engry&power wang@ fluent 6.3
语句(Statement)
语句是Fortran程序的基本单位,一条语句可包含0~
错误的写法 axis1=1, axis2=2, axis3=3 Area = sqrt(half * (half - axis1) * (half -
&axis2) * (half - axis3))
引号的使用
a=―Hello‖ ! FOTRAN 90可以用双引号界定字符串 b=‗Hello‘ ! FOTRAN 77只能用单引号界定字符串 c=―That‘s right.‖ ! 用双引号界定字符串,可以在字串 中任意使用单引号 d=‗That‘‘s right‘ !用单引号界定字符串时,输出单引号 要连续使用两个单引号 e=―That‘s ―‖right‖‖.‖ !用双引号界定字符串时,输出双 引号也要连续用两个双引号
算术表达式
2**3**2 2A : 2*A arcsinx :ASIN(X) (A+B)(C+D) : (A+B)*(C+D)
( A B )(C D ) : ((A+B)*(C+D))/(2*(E+F)) 2 (E F )
d x y z
2 2
2
d=sqrt(x**2+y**2+z**2)

132个字符; 除赋值语句外,所有的语句都从一个关键字开始; 一般情况下,每行一条语句; 如果一行有多条语句,它们之间以分号分隔 假如一条语句一行写不完,允许出现续行,但要求被 续行最后的非空白字符为“&‖ 续行从下一行(非注释行)的第一个非空白字符开始, 如果下一行的非空白字符为“&‖,则续行从该字符后 的第一个字符开始。 Fortran 90 允许出现多达39个续行。
Example
program convert real a a = 10 / 4 / 0.5 print *, 'a = ', a end !Integer a
关系运算符
F90 F77 == .EQ.
/= .NE. > .GT.
功能/意义 判断是否「等于」
判断是否「不相等」 判断是否「大于」
>= .GE. < .LT. <= .LE.
关系表达式

关系表达式的结果是一个逻辑量 .TRUE. 或者.FALSE.,输出时为T或者F Print*,3.GT.5 !输出为F 关系表达式允许不同类型的算术表达式比较 Print*, 8.EQ.(5/2+1.5)*2 !输出结果为F 比较两个实数大小时,由于机内表示为近似值, 所以不应当做两个数来比较 ABS(A-B).LE.1E-06 !表示A和B误差的绝对值小于10-6时,认为二者 相等
找错误
real(8) parameter a
integer(4) parameter ::b Complex, parameter c=(1.0,2.0) real(8), parameter a
integer(4), parameter ::b
Complex, parameter :: c=(1.0,2.0)
– 非数值型
• 字符型 CHARACTER • 逻辑型(布尔型) LOGICAL
自定义数据类型(派生数据类型)
变量声明及其初始化
声明部分必须出现在执行部分之前,而
不能将声明语句插在执行部分之中。 Fortran 90: 数据类型 [[, 属性]::] 变量列表
– DIMENSION、PARAMETER、TARGET、 – POINTER、ALLOCATABLE、INTENT – ::可以省略,但如果在声明的同时给变量赋 初值,则不能省略
注意: – IMPLICIT指令要马上接在PROGRAM指令 的下一行,不能放在其它位置
主要内容
Fortran基础知识
变量类型与表达式 例程和模块
控制结构
数组
派生类型和指针
数据类型
固有数据类型 – 数值型 (numerical)
• 整型 INTEGER • 实型 REAL • 复数型 COMPLEX
逻辑运算符——两个逻辑运算 式间的运算关系
.AND.
交集,如果两边的式子都成立,整 个条件就成立 或集,两边的式子只要有一个成立, 整个条件就成立 逻辑非,如果后面的式子不成立, 整个式子就算成立 两个式子的逻辑运算结果相同时, 整个式子就成立
相关文档
最新文档