fortran习题课
- 格式:ppt
- 大小:583.50 KB
- 文档页数:98
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 的数组元素。
工程分析程序设计上机作业(六)上机目的:练习派生类型的定义、构造、初始化,成员的调用和操作,操作符的重载,指针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编程语言是非常重要的。
它们不仅可以帮助我们检验学习成果,更可以帮助我们更好地理解知识点,提高编程能力,是我们学习的重要辅助工具。
希望大家能够充分利用课本习题答案,不断提高
自己的编程水平,取得更好的学习成绩。
第五章第一题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课后习题的答案,希望对大家的学习有所帮助。
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程序,计算并输出用户输入的一个数的平方根。
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个数的平均值的那些数及最小数。
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°从键盘输入)。
Fortran95第二章第五大题习题与答案1. 计算以下分段函数的值。
≤≤-<≤-+-=)0(s i n)2()0(s i n )2(ππππx x x x x x y Program ex2_1implicit nonereal x,yreal,parameter::pi=3.14159real,parameter::npi=-3.14159print*,'请输入x 的值'read*,xif(x>=npi.and.x<0)theny=-(2.0*pi+x)*sin(x)write(*,*)'y=',yelseif(x>=0.and.x<=pi)theny=(2.0*pi-x)*sin(x)write(*,*)'y=',yelsewrite(*,*)'x 值超出定义域范围'endifstopEnd2. 输入一个数M ,判断它是否能被3或5整除,如能被其中一个数整除,则输出M ,否则输出“此数不能被3或5整除”的提示信息。
Program ex2_2implicit noneinteger mprint*,'请输入一个整数的值'read*,mif(mod(m,3)==0.or.mod(m,5)==0)thenwrite(*,*)'m=',melsewrite(*,*)'输入的整数不能被3或5整除!'endifstopEnd3. 某电视台晚上8点的节目安排如下:星期一、星期四播出新闻;星期二、星期五播出电视剧;星期三、星期六播出儿童节目;星期日播出电影。
写出程序,根据输入星期几来查询当天晚上的节目。
Program ex2_3implicit noneinteger mprint*,'请输入星期序号(星期一~日依次为1~7)'read*,mif(m==1.or.m==4)thenwrite(*,*)'新闻'elseif(m==2.or.m==5)thenwrite(*,*)'电视剧'elseif(m==3.or.m==6)thenwrite(*,*)'儿童节目'elseif(m==7)thenwrite(*,*)'电影'elsewrite(*,*)'输入序号不正确!'endifstopEnd4.输入一个学生的学号和三门课程的成绩。
FORTRAN习题答案习题二一、问答题1. 给出下面变量名称,哪些是合法变量?哪些是非法变量?说明原因。
Count 、num_2、x&y 、4x+5y 、china-suzhou 、$us 、AbCdE 、Mr.bai 、t5、_another 、school_class_25、#125、2002Y 、π、β、A01/02、alpha 、date(1) 1. 判定下面整数,指出哪些是合法整数,哪些是非法整数?说明原因。
-0、+ 215、$125、3,245,895、5.3245、5#384、-524_3、#5DFE 、23-345、16#1A2B 、38#ABCD 、8#275_2、+327890、4 #3212. 判定下面实数,指出哪些是合法实数,哪些是非法实数?说明原因。
-0E2、45.2345E3.5、-5489E25_8、-.2345E-35、$185.45E 、+ 2.753425E24_3、58D85、+0.E-0、-00000.001E5、5,443,223.44、-12 34E+2、+ 18.5E 18、2.5E42习题三一、选择题1.下面是V isual Fortran 中正确的表达式是。
(A )A*COS(X)+∣B ∣ (B )2*EXP(2*X)/SQRT(16.0)(C )B 2-4AC (D )MOD (24.5,0.5)2.下面算术赋值语句中正确的语句是。
(A )M*N=(X-Y)/Z (B )+R=A+B/C(C )X=Y=Z-1.0 (D )Y=A*B/C/D3.算术表达式1/3+2/3的值为。
(A )0 (B ) 1 (C ) 0.99999999 (D )值不确定二、问答题1. 将下列代数式用Visual Fortran 表达式描述:① ②③ 4sin 3A-3sinA+sin3A ④ 2.执行下列赋值语句后,变量中的值。
变量的类型遵循I —N 规则。