西安交通大学工程分析程序设计Fortran上机作业GanlvJune2,2016Contents1Fortran程程程序序序设设设计计计基基基础础础11.1求表达式的值.................................................11.2求数学表达式的值..............................................21.3求六边形面积.................................................21.4计算人口增长.................................................22模模模块块块化化化编编编程程程(((1)))32.1求六边形面积(函数)............................................32.2牛顿迭代法解方程..............................................32.3表达式求值(子程序)............................................42.4表达式求值(模块).............................................43模模模块块块化化化编编编程程程(((2)))53.1求余数.....................................................53.2求最大公约数.................................................63.3求双曲正弦..................................................63.4Euler法求解微分方程.............................................74结结结构构构化化化编编编程程程74.1分段函数求值.................................................84.2简单排序...................................................84.3判断素数...................................................84.4验证哥德巴赫猜想..............................................94.5分解质因数..................................................95数数数组组组(((1)))105.1杨辉三角形..................................................105.2矩阵乘法...................................................105.3冒泡排序...................................................115.4差集......................................................116数数数组组组(((2)))126.1数组逆序...................................................126.2数组轮换...................................................126.3选择排序...................................................126.4高斯消去法..................................................137派派派生生生类类类型型型157.1构建派生类型.................................................157.2操作符重载..................................................1618指指指针针针、、、格格格式式式化化化输输输入入入/输输输出出出、、、文文文件件件操操操作作作178.1格式化输入输出................................................178.1.1整数..................................................178.1.2实数..................................................188.1.3复数..................................................188.1.4逻辑型.................................................188.1.5字符串.................................................198.2输出金字塔形状................................................198.3整齐的杨辉三角形..............................................208.4龙格-库塔法求解微分方程..........................................208.5Shell排序...................................................229参参参考考考文文文献献献2310LICENSE241Fortran程程程序序序设设设计计计基基基础础础上机目的:掌握程序书写、字符集和标识符、数据类型、声明、算术表达式、表控输入输出语句等编程基本概念。1.1求求求表表表达达达式式式的的的值值值若有实数𝐴=1.0,𝐵=3.5,𝑇=10.0,𝑋=5.0,整数𝐼=−5,𝐽=7,𝐾=3,求下列表达式的值。−(𝐴+𝑇)(𝐵+(𝑋/𝑇))/(4.0*𝐴)(𝐼*𝐽)/𝐾(𝐼/𝐾)*𝐽+𝑇/𝑋−(𝐾+1)/5+𝐼*𝐴−𝐵𝑆𝑄𝑅𝑇(𝑅𝐸𝐴𝐿(𝐴𝐵𝑆(𝐾)+1))𝑀𝐴𝑋(𝐽,𝑀𝑂𝐷(𝐽,𝐾))𝐽+𝐼𝑁𝑇(𝑇/𝐵)/2programEX0101implicitnonereal,parameter::A=1.0,B=3.5,T=10.0,X=5.0integer,parameter::I=-5,J=7,K=3print*,’-(A+T)’,-(A+T)print*,’(B+(X/T))/(4.0*A)’,(B+(X/T))/(4.0*A)print*,’(I*J)/K’,(I*J)/Kprint*,’(I/K)*J+T/X’,(I/K)*J+T/Xprint*,’-(K+1)/5+I*A-B’,-(K+1)/5+I*A-Bprint*,’SQRT(REAL(ABS(K)+1))’,sqrt(real(abs(K)+1))print*,’MAX(J,MOD(J,K))’,max(J,mod(J,K))print*,’J+INT(T/B)/2’,J+int(T/B)/2endprogram21.2求求求数数数学学学表表表达达达式式式的的的值值值若有𝐴=1.0,𝐵=2.0,𝐶=−1.0,求下列数学表达式的值:3𝐴2+4𝐵2𝐴−𝐵−𝐵+√𝐵2−4𝐴𝐶2𝐴6ln(𝐵+𝐶)21403+𝐴cos𝐵√𝐴2+𝐵2sin(︃arctan√𝐴2+𝐵2|𝐶|)︃programEX0102implicitnonereal,parameter::A=1.0,B=2.0,C=-1.0print*,’(3*A^2+4*B^2)/(A-B)’,(3*A**2+4*B**2)/(A-B)print*,’(-B+SQRT(B^2-4*A*C))/(2*A)’,(-B+sqrt(B**2-4*A*C))/(2*A)print*,’6*LN((B+C)^2)/(140/(3+A))’,6*log((B+C)**2)/(140/(3+A))print*,’COS(B/SQRT(A^2+B^2))’,cos(B/sqrt(A**2+B**2))print*,’SIN(ATAN(SQRT(A^2+B^2)/ABS(C)))’,sin(atan(sqrt(A**2+B**2)/abs(C)))endprogram1.3求求求六六六边边边形形形面面面积积积如图,求六边形面积。l1=10l2=20l3=18l4=15l5=21l6=14l7=30l8=36l9=28提示:三角形面积𝑎𝑟𝑒𝑎=√︀𝑠(𝑠−𝑎)(𝑠−𝑏)(𝑠−𝑐),其中𝑠=𝑎+𝑏+𝑐2,𝑎,𝑏,𝑐为三个边长。programEX0103implicitnonereal,parameter::L1=10,L2=20,L3=18,L4=15,L5=21,L6=14,L7=30,L8=36,L9=28real::s,area=0s=(L1+L8+L9)/2area=area+sqrt(s*(s-L1)*(s-L8)*(s-L9))s=(L2+L7+L8)/2area=area+sqrt(s*(s-L2)*(s-L7)*(s-L8))s=(L3+L4+L7)/2area=area+sqrt(s*(s-L3)*(s-L4)*(s-L7))s=(L5+L6+L9)/2area=area+sqrt(s*(s-L5)*(s-L6)*(s-L9))print*,’theareaofthehexagonis’,areaendprogram1.4计计计算算算人人人口口口增增增长长长假设目前我国人口13.0亿人,每年增长率为1.5%,求𝑛年以后的人口数。3要求:𝑛从键盘输入。programEX0104implicitnonereal,parameter::N0=13.0integer::nprint*,’Pleaseinputyearspast:’read*,nprint*,’Nowthereare’,N0*(1.015**n),’*10^8people’endprogram2模模模块块块化化化编编编程程程(((1)))上机目的:掌握主程序、内部例程、外部例程、模块等功能的使用方法。2.1求求求六六六边边边形形形面面面积积积(((函函函数数数)))如图,求六边形面积。l1=10l2=20l3=18l4=15l5=21l6=14l7=30l8=36l9=28要求:用内部函数来实现提示:三角形面积𝑎𝑟𝑒𝑎=√︀𝑠(𝑠−𝑎)(𝑠−𝑏)(𝑠−𝑐),其中𝑠=𝑎+𝑏+𝑐2,𝑎,𝑏,𝑐为三个边长。programEX0201implicitnonereal,parameter::L1=10,L2=20,L3=18,L4=15,L5=21,L6=14,L7=30,L8=36,L9=28print*,’theareaofthehexagonis’,&area(L1,L8,L9)+area(L2,L7,L8)+area(L3,L4,L7)+area(L5,L6,L9)containsfunctionarea(a,b,c)reala,b,c,area,ss=(a+b+c)/2area=sqrt(s*(s-a)*(s-b)*(s-c))endfunctionendprogram2.2牛牛牛顿顿顿迭迭迭代代代法法法解解解方方方程程程使用牛顿法求解以下方程的根:𝑥2+4𝑥+1=07𝑥4+6𝑥3−5𝑥2+4𝑥+3=0要求:用