fortran77程序设计简介
- 格式:ppt
- 大小:1.86 MB
- 文档页数:281
Fortran77结构化程序设计Fortran77结构化程序设计章节一、引言1.1 程序设计的概述1.2 Fortran77的特点1.3 编写结构化程序的优势章节二、基本语法和数据类型2.1 Fortran77的语法规则2.2 数据类型和变量2.2.1 整型2.2.2 实型2.2.3 逻辑型2.2.4 字符型2.2.5 数组2.2.6 字符串章节三、控制结构3.1 顺序结构3.2 分支结构3.2.1 IF语句3.2.2 CASE语句3.3 循环结构3.3.1 DO循环3.3.2 WHILE循环3.3.3 循环控制语句章节四、子程序和函数4.1 子程序的定义和调用4.2 函数的定义和调用4.3 参数传递4.4 递归调用章节五、文件操作5.1 文件的打开和关闭5.2 读写文件的基本操作5.3 文件的格式化读写5.4 随机访问文件章节六、错误处理和调试技巧6.1 错误类型和处理方法6.2 调试工具和技巧章节七、实例应用7.1 计算圆的面积7.2 求解线性方程组7.3 求解微分方程7.4 图像处理附录:常用函数库A.1 标准数学函数库A.2 字符串处理函数库A.3 文件操作函数库A.4 其他常用函数库附件:示例代码文件法律名词及注释:1、Fortran77:一种高级编程语言,用于科学计算和工程应用。
2、数据类型:变量的属性,如整型、实型、字符型等。
3、分支结构:程序中根据条件选择不同执行路径的结构。
4、循环结构:程序中重复执行某段代码的结构。
5、子程序:独立的代码块,可以被多次调用。
6、函数:接受输入参数并返回结果的子程序。
7、文件操作:程序与外部文件的读写交互。
8、错误处理:处理程序执行过程中的错误和异常情况。
9、调试:通过检查和修复程序错误来确保程序正确执行。
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).Computer Programs by Chapter and Section1.0flmoon calculate phases of the moon by date1.1julday Julian Day number from calendar date1.1badluk Friday the13th when the moon is full1.1caldat calendar date from Julian day number2.1gaussj Gauss-Jordan matrix inversion and linear equation solution2.3ludcmp linear equation solution,LU decomposition2.3lubksb linear equation solution,backsubstitution2.4tridag solution of tridiagonal systems2.4banmul multiply vector by band diagonal matrix2.4bandec band diagonal systems,decomposition2.4banbks band diagonal systems,backsubstitution2.5mprove linear equation solution,iterative improvement2.6svbksb singular value backsubstitution2.6svdcmp singular value decomposition of a matrix2.6pythag calculate(a2+b2)1/2without overflow2.7cyclic solution of cyclic tridiagonal systems2.7sprsin convert matrix to sparse format2.7sprsax product of sparse matrix and vector2.7sprstx product of transpose sparse matrix and vector2.7sprstp transpose of sparse matrix2.7sprspm pattern multiply two sparse matrices2.7sprstm threshold multiply two sparse matrices2.7linbcg biconjugate gradient solution of sparse systems2.7snrm used by linbcg for vector norm2.7atimes used by linbcg for sparse multiplication2.7asolve used by linbcg for preconditioner2.8vander solve Vandermonde systems2.8toeplz solve Toeplitz systems2.9choldc Cholesky decomposition2.9cholsl Cholesky backsubstitution2.10qrdcmp QR decomposition2.10qrsolv QR backsubstitution2.10rsolv right triangular backsubstitution2.10qrupdt update a QR decomposition2.10rotate Jacobi rotation used by qrupdt 3.1polint polynomial interpolation3.2ratint rational function interpolation3.3spline construct a cubic spline3.3splint cubic spline interpolation3.4locate search an ordered table by bisectionxxivComputer Programs by Chapter and Section xxv Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).3.4hunt search a table when calls are correlated3.5polcoe polynomial coefficients from table of values3.5polcof polynomial coefficients from table of values3.6polin2two-dimensional polynomial interpolation3.6bcucof construct two-dimensional bicubic3.6bcuint two-dimensional bicubic interpolation3.6splie2construct two-dimensional spline3.6splin2two-dimensional spline interpolation4.2trapzd trapezoidal rule4.2qtrap integrate using trapezoidal rule4.2qsimp integrate using Simpson’s rule4.3qromb integrate using Romberg adaptive method4.4midpnt extended midpoint rule4.4qromo integrate using open Romberg adaptive method4.4midinf integrate a function on a semi-infinite interval4.4midsql integrate a function with lower square-root singularity4.4midsqu integrate a function with upper square-root singularity4.4midexp integrate a function that decreases exponentially4.5qgaus integrate a function by Gaussian quadratures4.5gauleg Gauss-Legendre weights and abscissas4.5gaulag Gauss-Laguerre weights and abscissas4.5gauher Gauss-Hermite weights and abscissas4.5gaujac Gauss-Jacobi weights and abscissas4.5gaucof quadrature weights from orthogonal polynomials4.5orthog construct nonclassical orthogonal polynomials4.6quad3d integrate a function over a three-dimensional space5.1eulsum sum a series by Euler–van Wijngaarden algorithm5.3ddpoly evaluate a polynomial and its derivatives5.3poldiv divide one polynomial by another5.3ratval evaluate a rational function5.7dfridr numerical derivative by Ridders’method5.8chebftfit a Chebyshev polynomial to a function5.8chebev Chebyshev polynomial evaluation5.9chder derivative of a function already Chebyshevfitted5.9chint integrate a function already Chebyshevfitted5.10chebpc polynomial coefficients from a Chebyshevfit5.10pcshft polynomial coefficients of a shifted polynomial5.11pccheb inverse of chebpc;use to economize power series5.12pade Pad´e approximant from power series coefficients5.13ratlsq rationalfit by least-squares method6.1gammln logarithm of gamma function6.1factrl factorial function6.1bico binomial coefficients function6.1factln logarithm of factorial functionxxvi Computer Programs by Chapter and Section Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).6.1beta beta function6.2gammp incomplete gamma function6.2gammq complement of incomplete gamma function6.2gser series used by gammp and gammq6.2gcf continued fraction used by gammp and gammq6.2erf error function6.2erfc complementary error function6.2erfcc complementary error function,concise routine6.3expint exponential integral E n6.3ei exponential integral Ei6.4betai incomplete beta function6.4betacf continued fraction used by betai6.5bessj0Bessel function J06.5bessy0Bessel function Y06.5bessj1Bessel function J16.5bessy1Bessel function Y16.5bessy Bessel function Y of general integer order6.5bessj Bessel function J of general integer order6.6bessi0modified Bessel function I06.6bessk0modified Bessel function K06.6bessi1modified Bessel function I16.6bessk1modified Bessel function K16.6bessk modified Bessel function K of integer order6.6bessi modified Bessel function I of integer order6.7bessjy Bessel functions of fractional order6.7beschb Chebyshev expansion used by bessjy6.7bessik modified Bessel functions of fractional order6.7airy Airy functions6.7sphbes spherical Bessel functions j n and y n6.8plgndr Legendre polynomials,associated(spherical harmonics)6.9frenel Fresnel integrals S(x)and C(x)6.9cisi cosine and sine integrals Ci and Si6.10dawson Dawson’s integral6.11rf Carlson’s elliptic integral of thefirst kind6.11rd Carlson’s elliptic integral of the second kind6.11rj Carlson’s elliptic integral of the third kind6.11rc Carlson’s degenerate elliptic integral6.11ellf Legendre elliptic integral of thefirst kind6.11elle Legendre elliptic integral of the second kind6.11ellpi Legendre elliptic integral of the third kind6.11sncndn Jacobian elliptic functions6.12hypgeo complex hypergeometric function6.12hypser complex hypergeometric function,series evaluation6.12hypdrv complex hypergeometric function,derivative of7.1ran0random deviate by Park and Miller minimal standard 7.1ran1random deviate,minimal standard plus shuffleComputer Programs by Chapter and Section xxvii Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).7.1ran2random deviate by L’Ecuyer long period plus shuffle7.1ran3random deviate by Knuth subtractive method7.2expdev exponential random deviates7.2gasdev normally distributed random deviates7.3gamdev gamma-law distribution random deviates7.3poidev Poisson distributed random deviates7.3bnldev binomial distributed random deviates7.4irbit1random bit sequence7.4irbit2random bit sequence7.5psdes“pseudo-DES”hashing of64bits7.5ran4random deviates from DES-like hashing7.7sobseq Sobol’s quasi-random sequence7.8vegas adaptive multidimensional Monte Carlo integration7.8rebin sample rebinning used by vegas7.8miser recursive multidimensional Monte Carlo integration7.8ranpt get random point,used by miser8.1piksrt sort an array by straight insertion8.1piksr2sort two arrays by straight insertion8.1shell sort an array by Shell’s method8.2sort sort an array by quicksort method8.2sort2sort two arrays by quicksort method8.3hpsort sort an array by heapsort method8.4indexx construct an index for an array8.4sort3sort,use an index to sort3or more arrays8.4rank construct a rank table for an array8.5selectfind the N th largest in an array8.5selipfind the N th largest,without altering an array8.5hpselfind M largest values,without altering an array8.6eclass determine equivalence classes from list8.6eclazz determine equivalence classes from procedure9.0scrsho graph a function to search for roots9.1zbrac outward search for brackets on roots9.1zbrak inward search for brackets on roots9.1rtbisfind root of a function by bisection9.2rtflspfind root of a function by false-position9.2rtsecfind root of a function by secant method9.2zriddrfind root of a function by Ridders’method9.3zbrentfind root of a function by Brent’s method9.4rtnewtfind root of a function by Newton-Raphson9.4rtsafefind root of a function by Newton-Raphson and bisection 9.5laguerfind a root of a polynomial by Laguerre’s method9.5zroots roots of a polynomial by Laguerre’s method withdeflation9.5zrhqr roots of a polynomial by eigenvalue methods9.5qroot complex or double root of a polynomial,Bairstowxxviii Computer Programs by Chapter and Section Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).9.6mnewt Newton’s method for systems of equations9.7lnsrch search along a line,used by newt9.7newt globally convergent multi-dimensional Newton’s method9.7fdjacfinite-difference Jacobian,used by newt9.7fmin norm of a vector function,used by newt9.7broydn secant method for systems of equations10.1mnbrak bracket the minimum of a function10.1goldenfind minimum of a function by golden section search10.2brentfind minimum of a function by Brent’s method10.3dbrentfind minimum of a function using derivative information10.4amoeba minimize in N-dimensions by downhill simplex method10.4amotry evaluate a trial point,used by amoeba10.5powell minimize in N-dimensions by Powell’s method10.5linmin minimum of a function along a ray in N-dimensions10.5f1dim function used by linmin10.6frprmn minimize in N-dimensions by conjugate gradient10.6df1dim alternative function used by linmin10.7dfpmin minimize in N-dimensions by variable metric method10.8simplx linear programming maximization of a linear function10.8simp1linear programming,used by simplx10.8simp2linear programming,used by simplx10.8simp3linear programming,used by simplx10.9anneal traveling salesman problem by simulated annealing10.9revcst cost of a reversal,used by anneal10.9revers do a reversal,used by anneal10.9trncst cost of a transposition,used by anneal10.9trnspt do a transposition,used by anneal10.9metrop Metropolis algorithm,used by anneal10.9amebsa simulated annealing in continuous spaces10.9amotsa evaluate a trial point,used by amebsa11.1jacobi eigenvalues and eigenvectors of a symmetric matrix11.1eigsrt eigenvectors,sorts into order by eigenvalue11.2tred2Householder reduction of a real,symmetric matrix11.3tqli eigensolution of a symmetric tridiagonal matrix11.5balanc balance a nonsymmetric matrix11.5elmhes reduce a general matrix to Hessenberg form11.6hqr eigenvalues of a Hessenberg matrix12.2four1fast Fourier transform(FFT)in one dimension12.3twofft fast Fourier transform of two real functions 12.3realft fast Fourier transform of a single real function 12.3sinft fast sine transform12.3cosft1fast cosine transform with endpoints12.3cosft2“staggered”fast cosine transform12.4fourn fast Fourier transform in multidimensionsComputer Programs by Chapter and Section xxix Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).12.5rlft3FFT of real data in two or three dimensions12.6fourfs FFT for huge data sets on external media12.6fourew rewind and permutefiles,used by fourfs13.1convlv convolution or deconvolution of data using FFT13.2correl correlation or autocorrelation of data using FFT13.4spctrm power spectrum estimation using FFT13.6memcof evaluate maximum entropy(MEM)coefficients13.6fixrts reflect roots of a polynomial into unit circle13.6predic linear prediction using MEM coefficients13.7evlmem power spectral estimation from MEM coefficients13.8period power spectrum of unevenly sampled data13.8fasper power spectrum of unevenly sampled larger data sets13.8spread extirpolate value into array,used by fasper13.9dftcor compute endpoint corrections for Fourier integrals13.9dftint high-accuracy Fourier integrals13.10wt1one-dimensional discrete wavelet transform13.10daub4Daubechies4-coefficient waveletfilter13.10pwtset initialize coefficients for pwt13.10pwt partial wavelet transform13.10wtn multidimensional discrete wavelet transform14.1moment calculate moments of a data set14.2ttest Student’s t-test for difference of means14.2avevar calculate mean and variance of a data set14.2tutest Student’s t-test for means,case of unequal variances14.2tptest Student’s t-test for means,case of paired data14.2ftest F-test for difference of variances14.3chsone chi-square test for difference between data and model14.3chstwo chi-square test for difference between two data sets14.3ksone Kolmogorov-Smirnov test of data against model14.3kstwo Kolmogorov-Smirnov test between two data sets14.3probks Kolmogorov-Smirnov probability function14.4cntab1contingency table analysis using chi-square14.4cntab2contingency table analysis using entropy measure14.5pearsn Pearson’s correlation between two data sets14.6spear Spearman’s rank correlation between two data sets14.6crank replaces array elements by their rank14.6kendl1correlation between two data sets,Kendall’s tau14.6kendl2contingency table analysis using Kendall’s tau14.7ks2d1s K–S test in two dimensions,data vs.model14.7quadct count points by quadrants,used by ks2d1s 14.7quadvl quadrant probabilities,used by ks2d1s14.7ks2d2s K–S test in two dimensions,data vs.data14.8savgol Savitzky-Golay smoothing coefficients15.2fit least-squaresfit data to a straight linexxx Computer Programs by Chapter and Section Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).15.3fitexyfit data to a straight line,errors in both x and y15.3chixy used by fitexy to calculate aχ215.4lfit general linear least-squaresfit by normal equations15.4covsrt rearrange covariance matrix,used by lfit15.4svdfit linear least-squaresfit by singular value decomposition15.4svdvar variances from singular value decomposition15.4fpolyfit a polynomial using lfit or svdfit15.4flegfit a Legendre polynomial using lfit or svdfit15.5mrqmin nonlinear least-squaresfit,Marquardt’s method15.5mrqcof used by mrqmin to evaluate coefficients15.5fgaussfit a sum of Gaussians using mrqmin15.7medfitfit data to a straight line robustly,least absolute deviation15.7rofuncfit data robustly,used by medfit16.1rk4integrate one step of ODEs,fourth-order Runge-Kutta16.1rkdumb integrate ODEs by fourth-order Runge-Kutta16.2rkqs integrate one step of ODEs with accuracy monitoring16.2rkck Cash-Karp-Runge-Kutta step used by rkqs16.2odeint integrate ODEs with accuracy monitoring16.3mmid integrate ODEs by modified midpoint method16.4bsstep integrate ODEs,Bulirsch-Stoer step16.4pzextr polynomial extrapolation,used by bsstep16.4rzextr rational function extrapolation,used by bsstep16.5stoerm integrate conservative second-order ODEs16.6stiff integrate stiff ODEs by fourth-order Rosenbrock16.6jacobn sample Jacobian routine for stiff16.6derivs sample derivatives routine for stiff16.6simpr integrate stiff ODEs by semi-implicit midpoint rule16.6stifbs integrate stiff ODEs,Bulirsch-Stoer step17.1shoot solve two point boundary value problem by shooting17.2shootf ditto,by shooting to afitting point17.3solvde two point boundary value problem,solve by relaxation17.3bksub backsubstitution,used by solvde17.3pinvs diagonalize a sub-block,used by solvde17.3red reduce columns of a matrix,used by solvde17.4sfroid spheroidal functions by method of solvde17.4difeq spheroidal matrix coefficients,used by sfroid17.4sphoot spheroidal functions by method of shoot17.4sphfpt spheroidal functions by method of shootf18.1fred2solve linear Fredholm equations of the second kind 18.1fredin interpolate solutions obtained with fred218.2voltra linear V olterra equations of the second kind18.3wwghts quadrature weights for an arbitrarily singular kernel 18.3kermom sample routine for moments of a singular kernel 18.3quadmx sample routine for a quadrature matrixComputer Programs by Chapter and Section xxxi Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website or call 1-800-872-7423 (North America only),or send email to directcustserv@ (outside North America).18.3fredex example of solving a singular Fredholm equation19.5sor elliptic PDE solved by successive overrelaxation method19.6mglin linear elliptic PDE solved by multigrid method19.6rstrct half-weighting restriction,used by mglin,mgfas19.6interp bilinear prolongation,used by mglin,mgfas19.6addint interpolate and add,used by mglin19.6slvsml solve on coarsest grid,used by mglin19.6relax Gauss-Seidel relaxation,used by mglin19.6resid calculate residual,used by mglin19.6copy utility used by mglin,mgfas19.6fill0utility used by mglin19.6maloc memory allocation utility used by mglin,mgfas19.6mgfas nonlinear elliptic PDE solved by multigrid method19.6relax2Gauss-Seidel relaxation,used by mgfas19.6slvsm2solve on coarsest grid,used by mgfas19.6lop applies nonlinear operator,used by mgfas19.6matadd utility used by mgfas19.6matsub utility used by mgfas19.6anorm2utility used by mgfas20.1machar diagnose computer’sfloating arithmetic20.2igray Gray code and its inverse20.3icrc1cyclic redundancy checksum,used by icrc20.3icrc cyclic redundancy checksum20.3decchk decimal check digit calculation or verification20.4hufmak construct a Huffman code20.4hufapp append bits to a Huffman code,used by hufmak20.4hufenc use Huffman code to encode and compress a character20.4hufdec use Huffman code to decode and decompress a character20.5arcmak construct an arithmetic code20.5arcode encode or decode a character using arithmetic coding20.5arcsum add integer to byte string,used by arcode20.6mpops multiple precision arithmetic,simpler operations20.6mpmul multiple precision multiply,using FFT methods20.6mpinv multiple precision reciprocal20.6mpdiv multiple precision divide and remainder20.6mpsqrt multiple precision square root20.6mp2dfr multiple precision conversion to decimal base20.6mppi multiple precision example,compute many digits ofπ。
第三讲FORTRAN 77 编程基础1主要内容⏹FORTRAN 77 基础⏹数据结构⏹控制结构(选择与循环)⏹基本输入输出⏹函数与子程序⏹计时函数⏹文件操作FORTRAN 基础⏹FORTRAN语言发展概况●FOR mula TRAN slation●适用于工程及科学计算的一种高级程序设计语言●1951 年由约翰•贝克斯等人开始研发●1957 年第一个FORTRAN 程序在IBM704 机上运行●1966 和1978 年先后颁布FORTRAN66 和FORTRAN77●1991 和1997 年颁布了Fortran 90和Fortran 95●2004 年公布Frotran2003 标准,面向对象程序设计●2010 年公布Fortran 2008 标准FORTRAN 编译器⏹免费的FORTRAN 编译器⏹商业版FORTRAN 编译器●GNU FORTRAN /gfortran●G95●Intel Fortran 编译器(非商业版)●Power Station 、Compaq Visual Fortran (已停止更新)●Intel Fortran●PGI Fortran●Absoft Pro Fortran●Lahey Fortran程序开发环境⏹Linux 系统⏹Windows 系统●字符界面,命令行方式●Code::Blocks + GCC●Microsoft Visual Studio + Intel Visual Fortran ●Code::Blocks + GCC一个简单的编程示例parameter(n=100)real a(n, n), b(n, n), c(n, n)cdo j = 1, ndo i = 1, na(i,j) = 1.0/(i+j‐1)b(i,j) = 1.0c(i,j) = 0.0enddoenddocdo i = 1, ndo j = 1, ndo k = 1, nc(i,j) = c(i,j) + a(i,k) * b(k,j) enddoenddoenddocprint*, "c(1,1) = ", c(1,1)stopendFORTRAN 程序分析●一个FORTRAN 源程序由一个或多个程序单元组成每个独立的程序单元以“end”语句结束●每个程序单元包括若干行(一行不能写多条语句,但一条语句可以分几行写)●每个程序单元可包含语句行和非语句行(注释行)●语句行:可执行语句和非执行语句行●非执行语句在所有可执行语句行前面●语句可以根据需要设置标号●各类语句在程序单元中的位置必须满足一定规则●程序必须按规定的格式书写源程序书写格式●每行只能在72 列内书写,并把80 列分为4 个区●1~5列:标号区(1~5 位整数)如果第1 列为“* ”或“c ”,则为注释行●第6列:续行标志区续行符可以是任意非空格/非零字符最多19 个续行●7~72 列:语句区(一行只能写一条语句)●73~:被忽略,有的编译器会报错⏹FORTRAN 77 源程序必须按规定的格式书写(Fixed Format 固定格式)⏹源程序●以.f为扩展名●纯文本文件,可使用任何文本编辑器编写⏹编译与执行g77‐O2 –o输出文件名源程序文件名./输出文件名g77 ‐O2 –o hello hello.f./hellog77常用选项-o:指定生成的可执行文件的文件名,缺省为a.out -c:只编译不链接,即只生成目标文件(.o文件)-I path:指定或增加包含文件(如*.h)的搜索路径-L path:指定(增加)库文件的搜索路径-l name:与库文件lib name.a链接-O, -O1, -O2, -O3:优化开关-g:在目标码中加入更多信息,用于程序调试●英文字母:A B C D ... ... X Y Z●下划线和阿拉伯数字:_ 0 1 2 3 4 5 6 7 8 9●特殊符号:空格= + -* / ( ) , . ' : "☞FORTRAN 不区分大小写☞早期的FORTRAN 卡片不允许小写故很多FORTRAN 程序源代码都是大写的FORTRAN 数据结构⏹F77支持基本类型和构造类型的数据结构●基本类型●构造类型:数组、记录、文件●F77 不支持指针整型、实型、双精度、复型、逻辑型、字符型正、负整数和零,缺省取值范围为:[-231, 231-1]②实型常量(Real)小数或指数形式:0.876 →8.76E-1 →87.6E-2指数部分只能是整数,取值范围:[10-38, 1038]③双精度常量(Double precision)8 字节:0.125D+45,取值范围:[10-308,10308]④复型常量(Complex)8 字节:(实数,实数)⑤逻辑型常量(Logical)⑥字符型常量(Character)⏹变量类型整型、实型、双精度、复型、逻辑型、字符型⏹变量名●第一个字符必须是字母●后面可以跟1~5 个字母、下划线或数字现在的FORTRAN 编译器允许多于6 个字符(g77 最多支持约51个字符)●变量名中的空格不起作用。
Fortran77结构化程序设计Fortran77结构化程序设计简介Fortran(Formula Translating System)是一种编程语言,特别适用于科学和工程领域的计算。
Fortran的第一版于1957年发布,至今已经发展到Fortran77版本。
Fortran77引入了结构化程序设计的概念,使得程序更易于编写和维护。
本文将介绍Fortran77结构化程序设计的基本原则、控制结构和常用语法。
基本原则结构化程序设计的目标是编写易读、易维护、易的程序。
为了达到这个目标,Fortran77结构化程序设计遵循以下基本原则:1. 模块化:将程序划分为小块的独立模块,每个模块负责完成特定的功能。
2. 单一入口:每个模块只有一个入口点,使得程序的执行流程清晰可见。
3. 自顶向下设计:从程序的总体结构开始,逐步细化到具体的算法和实现细节。
4. 注重可读性:使用有意义的变量和函数名,添加适当的注释,使得程序易于阅读和理解。
控制结构Fortran77提供了几种常用的控制结构来实现程序的流程控制:顺序结构顺序结构是Fortran77中最基本的控制结构,程序按照代码的顺序依次执行。
例如:fortranPROGRAM mnINTEGER :: i, sumsum = 0DO i = 1, 10sum = sum + iEND DOWRITE(,) 'Sum:', sumEND PROGRAM mn上述代码展示了一个简单的程序,通过循环计算1到10的和,并输出结果。
分支结构Fortran77提供了基本的分支控制结构:- `IF`语句:可以根据条件选择性执行代码块。
例如:fortranPROGRAM mnINTEGER :: iDO i = 1, 10IF (i < 5) THENWRITE(,) i, '小于5'ELSEWRITE(,) i, '大于等于5'END IFEND DOEND PROGRAM mn在上述代码中,根据变量`i`的值判断是否小于5,并分别输出不同的信息。
fortran77程序设计pdf
Fortran 77 是一种老旧的编程语言,主要用于科学计算。
以下是一个简单的Fortran 77 程序示例,用于计算斐波那契数列的第n 项:
```fortran
PROGRAM FIBONACCI
IMPLICIT NONE
INTEGER N, F1, F2, FN
INTEGER I
PARAMETER (N = 10)
DATA F1, F2 /0, 1/
DO I = 1, N
PRINT *, F1, F2
FN = F1 + F2
F1 = F2
F2 = FN
END DO
END PROGRAM FIBONACCI
```
该程序首先定义了几个整数变量N、F1、F2 和FN,然后使用PARAMETER 语句将N 设置为10。
接下来,程序使用DATA 语句初始化F1 和F2 为斐波那契数列的前两项0 和1。
然后,程序使用DO 循环迭代N 次,每次迭代中输出F1 和F2 的值,计算下一项FN,然后更新F1 和F2 的值。
最后,程序使用END PROGRAM 语句结束。
需要注意的是,Fortran 77 的语法比较繁琐,不支持现代编程语言的许多特性,如变量名中不能包含空格、不支持注释等。
因此,建议使用更现代的Fortran 版本进行编程。
fortran77中矩阵的表示方法【实用版5篇】目录(篇1)1.Fortran77 简介2.Fortran77 中矩阵的表示方法3.矩阵的存储方式4.矩阵的操作5.矩阵的访问6.矩阵的例子正文(篇1)1.Fortran77 简介Fortran77 是一种编程语言,主要用于数值计算和科学计算。
Fortran (Formula Translation)的意思是“公式翻译”,最早于 1957 年由 IBM 公司开发。
Fortran77 是 Fortran 语言的第七个版本,也是使用最广泛的版本之一。
2.Fortran77 中矩阵的表示方法在 Fortran77 中,矩阵用数组来表示。
一个数组可以是一个矩阵,也可以是一个向量。
要定义一个矩阵,需要指定矩阵的大小和类型,以及存放矩阵元素的数组。
3.矩阵的存储方式Fortran77 中矩阵的元素是按行存储的,也就是说,矩阵的第一行元素存储在数组的第一个位置,第二行元素存储在数组的第二个位置,以此类推。
4.矩阵的操作Fortran77 提供了许多用于矩阵操作的函数和操作符。
例如,可以使用“*”操作符来执行矩阵乘法,使用“+”操作符来执行矩阵加法。
此外,还可以使用一些内置函数,如“SUM”和“PROD”等来执行矩阵的求和和乘积操作。
5.矩阵的访问在 Fortran77 中,可以通过下标来访问矩阵的元素。
下标由两个部分组成:行号和列号。
例如,要访问一个 2x3 矩阵的第一行第二列的元素,可以使用下标(1,2)。
目录(篇2)1.Fortran77 简介2.Fortran77 中矩阵的表示方法3.示例:一个二维矩阵的表示4.结论正文(篇2)Fortran(Formula Translation)是一种高级编程语言,主要用于数值计算和科学计算。
Fortran77 是 Fortran 语言的一个版本,发布于1977 年。
尽管如今已经有了更新的版本,但 Fortran77 在科学计算领域仍然具有一定的地位。
FORTRAN77综述data (num(i),i=1,500)/500*0/,(num(i),i=501,1000)/500*1/Fortran77程序书写规则:1、程序中的变量名,不分大小写;2、变量名称是以字母开头再加上1到5位字母或数字构成,即变更名字串中只有前6位有效;3、一行只能写一个语句;4、程序的第一个语句固定为program 程序名称字符串5、某行的第1个字符至第5个字符位为标号区,只能书写语句标号或空着或注释内容;6、某行的第1个字符为C或*号时,则表示该行为注释行,其后面的内容为注释内容;7、某行的第6个字符位为非空格和非0字符时,则该行为上一行的续行,一个语句最多可有19个续行;8、某行的第7至72字符位为语句区,语句区内可以任加空格以求美观;9、某行的第73至80字符位为注释区,80字符位以后不能有内容。
Fortran77关系运算符:Fortran77逻辑运算符:.gt. 大于.ge. 天于或等于.lt. 小于.le. 小于或等于.eq. 等于.ne. 不等于.and. 逻辑与.or. 逻辑或.not. 逻辑非.eqv. 逻辑等.neqv. 逻辑不等运算符优先级由高到低顺序为:()→**→*或/→+或-→.gt.或.ge.或.lt.或.le.或.eq.或.ne.→.not.→.and.→.or.→.eqv.或.neqvFortran77语句:语句说明备注write(*,*) x1,x2 输出语句第一个*号是指输入输出文件设备号或其它设备机,第二个*号是指表控格式语句的行号,如不print *,x1,x2 输出语句*号是指表控格式语句的行号,如不指定则用默read *,x1,x2 输入语句*号是指表控格式语句的行号,如不指定则用默format(格式符) 控制输入输出格式语句。
注:1.在打印输出时,每一行第1个字符不显示,用来控制纵向走纸或显示行。
第1个字符作用空格正常回车换行0 回车并跳过1行1 换页+ 只回车不换行其它通常是回车换行2.输入小数时,自带小数点优先。
Fortran语言――Fortran77结构化程序设计第一章 FORTRAN语言程序设计初步 (1)1.1FORTRAN语言发展概况 (1)1.2 简单的FORTRAN程序分析 (1)1.3FORTRAN 源程序的书写格式 (1)1.4Fortran程序的编辑与运行 (1)1.5 常量 (1)1.5.1 整型常量 (1)1.5.2 实型常量 (1)1.6 变量 (2)1.6.1 变量的概念 (2)1.6.2 变量名 (2)1.6.3 变量类型 (2)1.7Fortran内部函数 (2)1.8Fortran算术表达式 (3)1.8.1 算术运算符和运算优先级 (3)1.8.2 表达式运算中的类型问题 (3)1.8.3 运算的误差问题 (3)1.9 赋值语句 (3)1.10 简单输出语句 (3)1.11 简单输入语句 (4)1.12PARAMETER语句 (5)1.13END,STOP,PAUSE语句 (5)总结 (5)第二章逻辑运算和选择结构 (6)2.1 引言 (6)2.2 关系表达式 (6)2.3 逻辑表达式 (6)2.4 用块 IF 实现选择结构 (6)2.5 逻辑IF语句 (7)2.6 算术IF语句 (7)第三章循环结构的实现 (8)3.1 用GOTO语句实现循环 (8)3.2 用DO语句实现循环 (8)3.3 当型循环的实现 (9)3.4 直到型循环的实现 (9)3.5 几种循环形式的关系和比较 (9)第四章 FORTRAN的数据结构 (10)4.1 程序中的数据结构 (10)4.2 双精度数据类型 (10)4.3 复型类型数据 (10)4.4 四种数值型数据之间的转换和运算 (10)4.5 字符型数据 (10)第五章数据的输入和输出 (12)i5.1 概述 (12)5.2 有格式的输出 (12)5.3 有格式的输入 (13)5.4 在PRINT,WRITE和READ语句中包含格式说明 (13)第六章数组 (14)6.1 数组的说明和数组元素的引用 (14)6.2 数组的逻辑结构和存储结构 (14)6.3 数组的输入和输出 (14)6.4 使用DATA语句给数组赋初值 (15)第七章语句函数 (16)7.1 语句函数的定义 (16)7.2 语句函数的引用 (16)第八章子程序 (17)8.1 函数子程序 (17)8.2 子例行程序 (18)8.3 实参和虚参之间的数据传递 (18)第九章数据共用存储单元 (20)9.1 等价语句 (EQUIV ALENCE语句) (20)9.2 公用语句 (20)9.2.1 无名公用区 (20)9.2.2 有名公用区 (21)9.3 数据块子程序 (22)第十章文件 (24)10.1 概述 (24)10.2 文件的基本概念 (24)10.3 数据文件的建立和使用 (24)ii第一章 FORTRAN语言程序设计初步1.1FORTRAN语言发展概况Formula Translation适用于工程及科学计算的一种高级程序设计语言1951年由约翰·贝克斯等人开始研究Fortran语言;1957年第一个Fortran程序在IBM704机上运行;1958和1962年先后推出FortranⅡ和FortranⅣ;1966和1978年先后颁布Fortran66和Fortran77;1991年颁布了Fortran90,1997年颁布Fortran95,2004年颁布Fortran20031.2 简单的FORTRAN程序分析Fortran程序的基本结构:⑴一个Fortran源程序由一个或多个程序单位组成,每个独立的程序单位以“end”语句结束。
[转载]Fortran 77, C, C++ 和 Fortran 90 的比较收藏发信人: quasar (飞贼克斯), 信区: Fortran标题: Fortran 77, C, C++ 和 Fortran 90 的比较(转载)发信站: 南京大学小百合站 (Tue Jun 1 10:59:14 2004)瀚海星云 -- 文章阅读 [讨论区: MathTools]发信人: HuiCai (老灰菜), 信区: SciComp标题: Fortran 77, C, C++ 和 Fortran 90 的比较(转载)发信站: 瀚海星云 (2002年12月19日10:40:38 星期四), 站内信件【以下文字转载自 Fortran 讨论区】【原文由 HuiCai 所发表】Fortran 77, C, C++ 和 Fortran 90 的比较/develop/article/16/16085.shtm三十年来, 从 Fortran 77 开始, Fortran 成为了计算科学的主要语言.在这段时间里, Fortran 的数值能力变得非常稳定而且优于其它计算机语言; 最大的改变来自于不断增长的各种可靠的数值过程库的种类. Fortran 联合(union), 它的使用技巧, 扩充的数值库为计算科学赋予了良好的基础.可是在过去十几年中, 动态数据结构(特别是动态数组)的重要性不窜上升, UNIX 工作站, 复杂的交互式可视化工具, 以及更近的并行体系结构--Fortran 77 都没有实现--刺激了其它语言作为计算语言的使用, 最明显的一个例子是C. 最近C++ 也已经引起人们的兴趣, Fortran 通过发展到 Fortran 90来弥补它在现代科学计算方面的不足. 这部分的一个通常的工作是比较四种语言对科学计算的适应性的, 这四种语言是两个C 的代表(C, C++) 和两个Fortran的代表(Fortran 77, Fortran 90). 下面的表格总结了这种比较, 后面的内容试图合理地解释这种等级排序, 从最好(1)到最差(4)..功能 ------------ F77 - C - C++ - F90数值健壮性 ---- 2 ---- 4 --- 3 ----- 1数据并行性 ---- 3 ---- 3 --- 3 ----- 1数据抽象 ------- 4 ---- 3 --- 2 ----- 1面向对象编程 - 4 ---- 3 --- 1 ----- 2函数型编程 ---- 4 ---- 3 --- 2 ----- 1平均等级 ------ 3.4 - 3.2 - 2.2 -- 1.21 数值健壮性Numeric Polymorphism(数值多态性)中是一个给定一个通用名称的几种版本的图形平滑过程的例子. 这里描述的通用能力是作为Fortran 90提供的一种额外的数值健壮性超过Fortran 77和C的特性. Fortran 77, Fortran 90 , 和 C 版本的SMOOTH子过程也在下面给出, 用于比较. (注意, Fortran 90版使用了第4部分描述的并行性)数值多态性, 加上实际类型的参数, 小数精度选择, 和数字环境变量检查等, 证明了Fortran 90排在这四种语言中的第一位. Fortran 77 列在第二为的原因在于它支持复杂变量, 这在很多计算科学应用中是很重要的. C ++ 把 C 挤出了第三位是由于它在通常领域多态性上的能力.2 数据并行化部分在这四种语言中, 只有Fortran 90具有对科学计算有价值的数据并行能力; 其它三种语言在这方面的特性基本上是一样的, 即全都没有. 这解释了四种语言在这个方面的排名.这里是完成高斯消去的一套Fortran 77 和 C 过程:****************************************************************** 编程决定正确的子过程处理过程: pivot.f , triang.f , 和 back.f. ** 子过程决定一系列同步方程的解*******************************************************************234567PROGRAM testgINTEGER IMAX, JMAXPARAMETER (IMAX = 3, JMAX = 4)REAL matrix(IMAX, JMAX)REAL matrix(IMAX)INTEGER i, j, nDATA ( ( matrix(i,j), j = 1, JMAX), i = 1, IMAX)+ /-1.0, 1.0, 2.0, 2.0, 3.0, -1.0, 1.0, 6.0,+ -1.0, 3.0, 4.0, 4.0/n = IMAXwrite(*,*) \"The original matrix,\",n,\"by\",n=1,\":\"call wrtmat(matrix, n, n +1)call pivot(matrix, n)write(*,*) \"The matrix after pivoting:\"call wrtmat(matrix, n, n +1)call triang(matrix, n)write(*,*) \"The matrix after lower triangulation:\"call wrtmat(matrix, n, n + 1)call back(solvec, matrix, n)write(*,*) \"The solution vector after back substitution:\"write(*,*) \"********************************************\"write(*,*) (solvec(i), i = 1, n)write(*,*) \"********************************************\"end******************************************************************** *子过程决定第一列系数矩阵的最大值, 把最大值所在的行和第一行交换, **处理器然后重复对其他的行和列做这种处理, 对于每一次叠代, 列的位置**和行的位置增加一(即, 第1行-第1列, 然后第2行-第2列, 然后第3行-第 **3列, 等 ********************************************************************* *234567SUBROUTINE pivot(matrix, n)INTEGER i, j, k, nREAL matrix(n, n + 1), maxval, tempvaldo 10, j = 1, nmaxval = matrix(j,j)do 20, i = j + 1, nif (maxval .lt. matrix(i,j)) thenmaxval = matrix(i,j)do 30, k = 1, n + 1tempval = matrix(i,k)matrix(i,k) = matrix(j, k)matrix(j,k) = tempval30continue20 continue10 continueend****************************************完成一个输入矩阵的低级分解的子过程 *****************************************234567SUBROUTINE triang(matrix, n)INTEGER i, j, k, nREAL matrix(n, n + 1), pivot, pcelemdo 10, j = 1, npivot = matrix(j,j)do 20, k = j + 1, n + 1matrix(j,k) = matrix(j,k) / pivot20 continuedo 30, i = j + 1, npcelem = matrix(i,j)do 40, k = j + 1, n + 1matrix(i,k) = matrix(i,k) - pcelem * matrix(j,k)40 continue30continueend********************************************************** * 子过程从一个已经经历了低级分解的参数矩阵计算一个解向量 *********************************************************** *234567SUBROUTINE back(solvec, matrix, n)REAL solvec(n), matrix(n, n + 1), sumsolvec(n) = matrix(n, n + 1)do 10, i = n -1, 1, -1sum = 0.0do 20, j = i + 1, nsum = sum + matrix(i, j) * solvec(j)20 continuesolvec(i) = matrix(i, n + 1) - sum10 continueend*********************************************************** * 测试子过程bisec.f的程序, bisec.f 决定一个方程(f.f中)的解** 可是这个函数确实假设函数-f由两个值支撑. 即在用户给定的终** 点之间的解不超过一个************************************************************ *234567PROGRAM testbsREAL xleft, xrightREAL fEXTERNAL fwrite(*,*) \"Please enter an initial left and right value:\"read(*,*) xleft, xrightcall bisec(f, xleft, xright)end这里是同一个算法的C 过程:/********************************************************* 决定三个函数(pivot.c, triang.c, back.c)正确处理的程序 ** 这些函数决定了一系列同步方程的解*********************************************************/#include <stdio.h>#define IMAX 3#define JMAX 4float matrix[IMAX][JMAX] = {{-1.0, 1.0, 2.0, 2.0 },{3.0, -1.0, 1.0, 6.0 },{-1.0, 3.0, 4.0, 4.0 }};float solvec[IMAX] = { 0.0, 0.0, 0.0 };main(){void wrt_output(void);void pivot(void);void triang(void);void back(void);void wrt_vector(void);(void)printf("The original matrix %d by %d :\n", IMAX, JMAX);(void)wrt_output();(void)pivot();(void)printf("The matrix after pivoting:\n");(void)wrt_output();(void)triang();(void)printf("The matrix after lower decomposition:\n");(void)wrt_output();(void)back();(void)printf("The solution vector after back substitution:\n");(void)wrt_vector();}/*********************************************************** * 决定参数矩阵中第一列的最大元素并移动第一列含有最大值的行 ** 到第一行. 然后重复对其他的行和列做这种处理, 对于每一次叠 ** 代, 列的位置和行的位置增加一(即, 第1行-第1列, 然后第2行- ** 第2列, 然后第3行-第3列, 等*************************************************************/ void pivot(){int i, j, k;float maxval, tempval;for(j = 1; j < IMAX; j++) {maxval = matrix[j][j];for ( i = (j + 1); i < IMAX; i++) {if ( maxval < matrix[i][j] ) {maxval = matrix[i][j];for( k = 0; k <= IMAX; k++) {tempval = matrix[i][k];matrix[i][k] = matrix[j][k];matrix[j][k] = tempval;}}}}}/************************************ 完成一个输入矩阵的低级分解的函数 *************************************/void triang(void){int i, j, k;float pivot, pcelem;for ( j = 0; j < IMAX; j++) {pivot = matrix[j][j];for ( k = ( j + 1 ); k <= IMAX; K++) {matrix[j][k] = matrix[j][k] / pivot;}for ( i = ( j + 1 ); i < IMAX; i++) {pcelem = matrix[i][j];for ( k = ( j + 1 ); k <= IMAX; k++) {matrix[i][k] = matrix[i][k] - ( pcelem * matrix[j][k] );}}}}/********************************************************* * 子过程从一个已经经历了低级分解的参数矩阵计算一个解向量 **********************************************************/ void back(void){int i, j;float sum;solvec[IMAX - 1] = matrix[IMAX - 1][JMAX -1];for ( i = (IMAX -1); i > -1; i--) {sum = 0.0;for ( j = (i + 1); j < IMAX; j++) {sum = sum + matrix[i][j] * solvec[j];}solvec[i] = matrix[i][IMAX] - sum;}}void wrt_output(void){int i, j;(void)printf("**************************************\n");for ( i = 0; i < IMAX; i++) {for ( j = 0; j < (JMAX - 1); j++) {(void)printf("%f", matrix[i][j]);}(void)printf("%f\n", matrix[i][JMAX - 1]);}(void)printf("****************************************\n"); }void wrt_vector(void);{(void)printf("*************************************\n");(void)printf("%f", solvec[0]);(void)printf(" %f", solvec[1]);(void)printg(" %f\n", solvec[2]);(void)printf("****************************************\n");}/************************************************************ 测试函数bisec.f的程序, bisec.f 决定一个方程(f中)的解** 可是这个函数确实假设函数-f由两个值支撑. 即在用户给定的终 ** 点之间的解不超过一个 *************************************************************/#include <stdio.h>#include <math.h>main(){void bisec(float init_left_val, float init_right_val);float f(float value):float xleft, xright;char line[100];(void)printf("Please enter an initial left and right value:");(void)fgets(line, sizeof(line), stdin);(void)sscanf(line, "%f %f", &xleft, &xright );(void)bisec(xleft, xright);return(0);}3 数据抽象Fortran 90有一个非常使用的,使用简单的数据抽象能力。