fortran95教程4章
- 格式:doc
- 大小:174.50 KB
- 文档页数:17
第13章过程及其通讯如果说一个语句可以看成是一条指令,那么在FORTRAN 95语言里,一个具有一定结构的计算任务可以对应的最小程序单位,就是一个过程。
从专致于科学计算的初衷出发,FORTRAN在语言的结构层面上,可以认为是面向过程的一种语言,尽管在编程语言流行面向对象的今天,面向过程显得有点落伍,但却有效地适用于描述计算任务,当然随着现代技术对于计算的要求越来越复杂与庞大,FORTRAN也不是一味地守旧,可以预计FORTRAN的下一个版本,就会具有适应大型软件工程的要求的面向对象的语言特性。
FORTRAN 95语言作为一种语言的主要特点,可以说就体现在它的过程这个主要的程序结构上。
而从我们程序编写者的角度来看,能否把一个完整的算法转写成一个完整的程序,关键也就在于能否构造出一些恰当的过程来作为程序的基本单位。
特别是对于大型的FORTRAN 95程序,需要把它分解为好几百个过程是很常见的,这时如何恰当地使用过程来构建整个程序,可以说是编写程序最主要的工作。
因此如何构建过程,如何根据需求运用过程,然后在不同的过程之间建立必要的通讯,是值得我们非常仔细地加以讨论的。
本章的主要任务即在于此。
13.1 过程的分类与性质由于过程具有多方面的功能与属性,因此对于过程的分类可以有多种方式。
下面我们首先讨论过程的各种分类方式及其相应的分类意义,然后我们讨论有关过程引用的要点与相应概念。
13.1.1 过程的分类过程可以有几个不同的分类方式,每种分类方式反映了过程的某个方面的特性。
下面分别予以讨论。
从形式上根据调用的方式的不同,FORTRAN的过程分为两类:●函数;函数返回一个可以供表达式使用的值。
因此函数的调用总是作为一个表达式的算元,函数的值也就是相应表达式算元的取值。
函数调用时直接使用函数的名称和它的变量,或者作为一个自定义运算,它返回值之后,它的功能就算完成,不对程序产生后效,当然,FORTRAN 标准也不绝对禁止使用产生一定后效的函数。
Fortran95简介-全文版Fortran95簡介-全文版By陳鯨太FORTRAN的演進FORTRAN的起源,要追溯到1954年IBM公司的一項計畫。
由JOHN BACKUS 領導的一個小組,嘗試著在IBM 704電腦上面發展一套程式,它可以把使用接近數學語言的文字,翻譯成機械語言。
這個計畫在剛開始並不被大家看好,但他們在1957年交出了成果,也就是第一套FORTRAN編譯器,FORTRAN語言也就因此誕生了。
FORTRAN語言的執行效率普遍的令各界滿意,它證明了這項計畫的可行性,也成為第一個被廣泛使用的高階語言。
FORTRAN的名字來自於英文的FORMULA TRANSLATOR這兩個字,而這兩個字恰是數學公式翻譯器的意思。
舊版的FORTRAN77是在1978年由美國國家標準局(ANSI)所正式公布的,之後改版有1992年提出的FORTRAN90以及1997年的FORTRAN95,本文是為了FORTRAN 95所撰寫。
編譯器簡介1、VISUAL FORTRANVISUAL FORTRAN一開始是起源於MICROSOFT的FORTRAN POWERSTATION 4.0,這套工具後來賣給DIGITAL公司來繼續發展,下一個版本稱為DIGITAL VISUAL FORTRAN 5.0,DIGITAL後來被COMPAQ合併,所以接下來的6.0及6.5版就稱為COMPAQ VISUAL FORTRAN。
而COMPAQ目前又跟HP合併,也許下一個版本會稱為HP VISUAL FORTRAN。
VISUAL FORTRAN被整合在一個叫作MICROSOFT VISUAL STUDIO的圖形介面開發環境中,VISUAL STUDIO提供一個統一的使用介面,這個介面包括文書編輯功能,PROJECT的管理、除錯工具等等,所以在使用上其實跟上學期的VISUAL C++滿類似的,同學們上課用過VISUAL C++,對VISUAL FORTRAN應該不會陌生。
fortran95课程设计一、课程目标知识目标:1. 理解Fortran 95编程语言的基本概念和语法结构;2. 掌握Fortran 95的数据类型、变量声明和运算符使用;3. 学会使用控制结构(如循环、条件语句)进行程序设计;4. 了解数组、函数和子程序在Fortran 95中的应用。
技能目标:1. 能够编写简单的Fortran 95程序,实现基本的输入输出功能;2. 能够运用控制结构进行逻辑判断和循环操作;3. 能够使用数组进行批量数据处理;4. 能够编写简单的函数和子程序,实现代码的模块化。
情感态度价值观目标:1. 培养学生对编程的兴趣,激发自主学习编程的热情;2. 培养学生严谨、细致的编程习惯,注重代码的可读性和效率;3. 培养团队合作精神,学会在编程过程中与他人交流、协作;4. 提高学生的逻辑思维能力,培养解决实际问题的能力。
课程性质:本课程为计算机编程入门课程,以Fortran 95编程语言为载体,培养学生编程技能和逻辑思维能力。
学生特点:学生处于初中或高中阶段,具备一定的数学基础,对编程感兴趣,但可能缺乏实际编程经验。
教学要求:教师应注重理论与实践相结合,以实例为主线,引导学生掌握编程技能,培养编程兴趣。
同时,关注学生的个体差异,提供针对性的指导和支持。
通过本课程的学习,使学生能够达到上述课程目标,为后续编程学习打下坚实基础。
二、教学内容1. Fortran 95基础语法- 程序结构- 数据类型与变量声明- 运算符与表达式- 基本输入输出操作2. 控制结构- 选择结构(IF语句)- 循环结构(DO循环、WHILE循环)3. 数组与函数- 数组的基本操作与应用- 内置函数与自定义函数- 子程序与模块化编程4. 实践项目与案例分析- 简单的计算器程序- 温度转换程序- 数组排序程序- 函数与子程序的应用实例5. 编程规范与调试技巧- 编码规范与命名规则- 调试方法与技巧- 性能优化建议教学内容安排与进度:第一周:Fortran 95基础语法及程序结构第二周:数据类型与变量声明、运算符与表达式第三周:基本输入输出操作、选择结构(IF语句)第四周:循环结构(DO循环、WHILE循环)第五周:数组的基本操作与应用第六周:内置函数与自定义函数、子程序与模块化编程第七周:实践项目与案例分析(计算器程序、温度转换程序等)第八周:编程规范与调试技巧、性能优化本教学内容根据课程目标制定,涵盖了Fortran 95编程语言的核心知识点,通过理论与实践相结合的方式,使学生能够逐步掌握编程技能,培养解决实际问题的能力。
Fortran95程序设计习题答案第四章 1.program main implicit none write(*,*) "Have a good time." write(*,*) "That's not bad." write(*,*) '"Mary" isn''t my name.' end program 2.program main real, parameter :: PI=3 implicit none.14159real radius write(*,*) "请输入半径长" read(*,*) radius write(*,"(' 面积='f8. 3)") radius*radius*PI end program 3.program main implicit none real grades write(*,*) "请输入成绩" read(*,*)grades write(*,"(' 调整后成绩为 'f8.3)") SQRT(grades)*10.0 end program 4.integer a,b real ra,rb a=2 b=3 ra=2.0 rb=3.0 write(*,*) b/a ! 输出1, 因为使用整数计算, 小数部分会无条件舍去 write(*,*) rb/ra ! 输出1.5 5.program main implicit none type distance real meter, inch, cm end type type(distance) :: d write(*,*) "请输入长度:" read(*,*) d%meter d%cm = d%meter*100 d%inch = d%cm/2.54 write(*,"(f8.3'米 ='f8.3'厘米='f8.3'英寸')") d%meter, d%cm, d%inch end program 第五章 1.program main implicit none integer money real tax write(*,*) "请输入月收入" read(*,*) money if ( money<1000 ) then tax = 0.03 else if ( money<5000) then tax = 0.1 else tax = 0.15 end if write(*,"(' 税金为 'I8)") nint(money*tax) end program 2.program main implicit none integer day character(len=20) :: tv write(*,*) "请输入星期几" read(*,*) day select case(day) case(1,4) tv = "新闻" case(2,5) tv = "电视剧" case(3,6) tv = "卡通" case(7) tv = "电影" case default write(*,*) "错误的输入" stop end select write(*,*) tv end program 3.program main implicit none integer age, money real tax write(*,*) "请输入年龄"write(*,*) "请输入月收入" read(*,*) money if ( age<50 ) thenread(*,*) ageif ( money<1000 ) then tax = 0.03 else if ( money<5000 )then tax = 0.10 else tax = 0.15 end if else if ( money<1000 ) then tax = 0.5 else if ( money<5000 )then tax = 0.7 else tax = 0.10 end if end ifwrite(*,"(' 税金为 'I8)") nint(money*tax) end program 4.program main implicit none integer year, days logical mod_4, mod_100, mod_400write(*,*) "请输入年份" read(*,*) year mod_4 = ( MOD(year,4) == 0 ) mod_100 = ( MOD(year,100) == 0 ) mod_400 = ( MOD(year,400) == 0 ) if ( (mod_4 .NEQV. mod_100) .or. mod_400 ) then days = 366 else days = 365 end if write(*,"('这一年有'I3'天')") days stop end program 第六章1.program main implicit none integer i do i=1,5 write(*,*) "Fortran" end do stop end program2.program main implicit none integer i,sum sum = 0 do i=1,99,2 sum = sum+i end do write(*,*) sum stop end program3.program main implicit none integer, parameter :: answer = 45 integer, parameter :: max = 5 integer weight, i do i=1,max write(*,*) "请输入体重" read(*,*) weight if ( weight==answer ) exit end do if ( i<=max ) then write(*,*) "猜对了" else write(*,*) "猜错了" end if stop end program4.program main implicit none integer, parameter :: max=10 integer i real item real ans ans = 1.0 item = 1.0 do i=2,max item = item/real(i) ans = ans+item end do write(*,*) ans stop end program5.program main implicit none integer, parameter :: length = 79 character(len=length) :: input, output integer i,j write(*,*) "请输入一个字串" read(*,"(A79)") input j=1 do i=1, len_trim(input) if( input(i:i) /= ' ' ) then output(j:j)=input(i:i) j=j+1 end if end do write(*,"(A79)") output stop end program 第七章 1.program mainimplicit none integer, parameter :: max = 10 integer i integer ::a(max) = (/ (2*i, i=1,10) /) integer :: t ! sum()是fortran库函数write(*,*) real(sum(a))/real(max) stop end program2.integer a(5,5) ! 5*5=25 integer b(2,3,4) ! 2*3*4=24 integerc(3,4,5,6) ! 3*4*5*6=360 integer d(-5:5) ! 11 integer e(-3:3, -3:3) ! 7*7=49 3.program main implicit none integer, parameter :: max=10integer f(max) integer i f(1)=0 f(2)=1 do i=3,max f(i)=f(i-1)+f(i-2) end do write(*,"(10I4)") f stop end program 4.program main implicit none integer, parameter :: size=10 integer :: a(size) = (/5,3,6,4,8,7,1,9,2,10 /) integer :: i,j integer :: t do i=1, size-1 do j=i+1, size if ( a(i) < a(j) ) then ! a(i)跟a(j)交换 t=a(i)a(i)=a(j) a(j)=t end if end do end do write(*,"(10I4)") a stop end5.a(2,2) ! 1+(2-1)+(2-1)*(5) = 7 a(3,3) ! 1+(3-1)+(3-1)*(5) = 13 第八章1.program main implicit none real radius, area write(*,*) "请输入半径长" read(*,*) radius call CircleArea(radius, area) write(*,"(' 面积 ='F8.3)") area stop end program subroutine CircleArea(radius, area) implicit none real, parameter :: PI=3.14159 real radius, area area = radius*radius*PI return end subroutine 2.program main implicit nonereal radius real, external :: CircleArea write(*,*) "请输入半径长" read(*,*) radius write(*,"(' 面积 = 'F8.3)") CircleArea(radius) stop end program real function CircleArea(radius) implicit none real, parameter :: PI=3.14159 real radius CircleArea = radius*radius*PI returnend function 3.program main implicit none call bar(3) call bar(10) stop end program subroutine bar(length) implicit none integer, intent(in) :: length integer i character(len=79) :: string string=" " do i=1,length string(i:i)='*' end do write(*,"(A79)") string return end subroutine 4.program main implicit none integer, external :: add write(*,*)add(100) end program recursive integer function add(n)integer, intent(in) :: n if ( n<0 ) then sum=0 return elseresult(sum) implicit noneif ( n<=1 ) then sum=n return end if sum = n + add(n-1) return end function 5.program main implicit none integer, external :: gcdwrite(*,*) gcd(18,12) end program integer function gcd(A,B) implicit none integer A,B,BIG,SMALL,TEMP BIG=max(A,B) SMALL=min(A,B) dowhile( SMALL /= 1 )TEMP=mod(BIG,SMALL) if ( TEMP==0 ) exit BIG=SMALL SMALL=TEMP enddo gcd=SMALL return end function 6.program main use TextGraphLib implicit none integer, parameter :: maxx=60, maxy=20 real, parameter :: StartX=0.0, EndX=3.14159*2.0 real, parameter :: xinc = (EndX-StartX)/(maxx-1) real x integer i,px,py call SetScreen(60,20) call SetCurrentChar('*') x=StartX do px=1,maxx py = (maxy/2)*sin(x)+maxy/2+1 call PutChar(px,py) x=x+xinc end docall UpdateScreen() stop end program 第九章 1.program main implicitnone character(len=79) :: filename character(len=79) :: buffer integer, parameter :: fileid = 10 integer count integer :: status = 0 logical alive write(*,*) "Filename:" read (*,"(A79)") filenameinquire( file=filename, exist=alive) if ( alive ) then open(unit=fileid, file=filename, & access="sequential", status="old") count = 0 dowhile(.true.) read(unit=fileid, fmt="(A79)", iostat=status ) bufferif ( status/=0 ) exit ! 没有资料就跳出循环 write(*,"(A79)") buffercount = count+1 if ( count==24 ) then pause count = 0 end if end do else write(*,*) TRIM(filename)," doesn't exist." end if stop end2.program main implicit none character(len=79) :: filenamecharacter(len=79) :: buffer integer, parameter :: fileid = 10 integer i integer :: status = 0 logical alive write(*,*) "Filename:" read (*,"(A79)") filename inquire( file=filename, exist=alive) if ( alive ) then open(unit=fileid, file=filename, & access="sequential",status="old") do while(.true.) read(unit=fileid, fmt="(A79)",iostat=status ) buffer if ( status/=0 )exit ! 没有资料就跳出循环 do i=1, len_trim(buffer) buffer(i:i) = char( ichar(buffer(i:i))-3 ) end do write(*,"(A70)") buffer enddo else write(*,*) TRIM(filename)," doesn't exist." end if stop end3.program main implicit none type student integer chinese, english, math, science, social, total end type type(student) :: s, total integer, parameter :: students=20, subjects=5 integer iopen(10,file="grades.bin",access="direct",recl=1) write(*,"(7A10)") "座号","中文","英文","数学","自然","社会","总分" total =student(0,0,0,0,0,0) do i=1, students read(10,rec=(i-1)*subjects+1)s%chinese read(10,rec=(i-1)*subjects+2) s%english read(10,rec=(i-1)*subjects+3) s%math read(10,rec=(i-1)*subjects+4) s%scienceread(10,rec=(i-1)*subjects+5) s%social s%total =s%chinese+s%english+s%math+s%science+s%social total%chinese =total%chinese+s%chinese total%english = total%english+s%englishtotal%math = total%math+s%math total%science = total%science+s%science total%social = total%social+s%social total%total = total%total+s%total write(*,"(7I10)") i, s end do write(*,"(A10,6F10.3)") "平均", & real(total%chinese)/real(students),&real(total%english)/real(students),&real(total%math)/real(students),&real(total%science)/real(students),&real(total%social)/real(students),& real(total%total)/real(students) stop end 4.program main implicit none character(len=79) :: filename character(len=79) :: buffer integer, parameter :: fileid = 10 integer i integer :: status = 0 logical alive write(*,*) "Filename:" read (*,"(A79)") filename inquire( file=filename, exist=alive) pen(unit=fileid, file=filename, & access="sequential", if ( alive ) then ostatus="old") do while(.true.) read(unit=fileid, fmt="(A79)",iostat=status ) buffer if ( status/=0 ) exit ! 没有数据就跳出循环 doi=1,len_trim(buffer) buffer(i:i) = char( ichar(buffer(i:i))-(mod(i-1,3)+1) ) end do write(*,"(A70)") buffer end do else write(*,*)TRIM(filename)," doesn't exist." end if stop end 5.module typedef typestudent integer :: num integer :: Chinese, English, Math, Natural, Social integer :: total integer :: rank end type end module program main use typedef implicit none integer, parameter :: fileid=10 integer, parameter :: students=20 character(len=80) :: tempstrtype(student) :: s(students) ! 储存学生成绩 type(student) :: total ! 计算平均分数用 integer i, num, error open(fileid,file="grades.txt",status="old", iostat=error) if ( error/=0 ) then write(*,*) "Open grades.txt fail." stop end if read(fileid, "(A80)") tempstr ! 读入第一行文字 total=student(0,0,0,0,0,0,0,0) ! 用循环读入每位学生的成绩 do i=1,students read(fileid,*) s(i)%num, s(i)%Chinese,s(i)%English, & s(i)%Math, s(i)%Natural, s(i)%Social ! 计算总分s(i)%Total = s(i)%Chinese + s(i)%English + & s(i)%Math + s(i)%Natural + s(i)%Social ! 累加上各科的分数, 计算各科平均时使用 total%Chinese = total%Chinese +s(i)%Chinese total%English = total%English + s(i)%Englishtotal%Math = total%Math + s(i)%Math total%Natural = total%Natural +s(i)%Natural total%Social = total%Social + s(i)%Social total%Total = total%Total + s(i)%Total end do call sort(s,students) ! 重新输出每位学生成绩 write(*,"(8A7)") "座号","中文","英文","数学","自然","社会","总分","名次" do i=1,students write(*,"(8I7)") s(i) end do ! 计算并输出平圴分数 write(*,"(A7,6F7.1)") "平均", &real(total%Chinese)/real(students),&real(total%English)/real(students),&real(total%Math) /real(students),&real(total%Natural)/real(students),& real(total%Social)/real(students),& real(total%Total) /real(students) stop end program subroutine sort(s,n) use typedef implicit none integer ntype(student) :: s(n), t integer i,j do i=1,n-1 do j=i+1,n if( s(i)%total < s(j)%total ) then t = s(i) s(i)=s(j) s(j) = t end if end do end do forall(i=1:n) s(i)%rank = i end forall end subroutine 第十章 1.integer(kind=4) ::4 bytes real(kind=4) :: b ! 4 bytes real(kind=8) :: c ! 8 bytes character(len=10) :: a !str ! 10 bytes integer(kind=4), pointer :: pa ! 4 bytesreal(kind=4), pointer :: pb ! 4 bytes real(kind=8), pointer :: pc ! 4 bytes character(len=10), pointer :: pstr ! 4 bytes type studentinteger Chinese, English, Math end type type(student) :: s ! 12 bytes type(student), pointer :: ps ! 4 bytes 2.integer, target :: a = 1 integer, target :: b = 2 integer, target :: c = 3 integer, pointer :: p p=>a write(*,*) p ! 1 p=>b write(*,*) p ! 2 p=>c p=5 write(*,*) c ! 53.module linklist type student integer :: num integer :: Chinese, English, Math, Science, Social end type type datalink type(student) :: item type(datalink), pointer :: next end type contains function SearchList(num, head) implicit none integer :: num type(datalink), pointer :: head, p type(datalink), pointer :: SearchList p=>headnullify(SearchList) do while( associated(p) ) if ( p%item%num==num ) then SearchList => p return end if p=>p%next end do return end function end module linklist program ex1016 use linklist implicit nonecharacter(len=20) :: filename character(len=80) :: tempstrtype(datalink), pointer :: head type(datalink), pointer :: ptype(student), allocatable :: s(:) integer i,error,size write(*,*) "filename:" read(*,*) filename open(10, file=filename, status="old", iostat=error) if ( error/=0 ) then write(*,*) "Open file fail!" stop end if allocate(head) nullify(head%next) p=>head size=0 read(10,"(A80)") tempstr ! 读入第一行字符串, 不需要处理它 ! 读入每一位学生的成绩do while(.true.) read(10,fmt=*, iostat=error) p%item if ( error/=0 )exit size=size+1 allocate(p%next, stat=error) ! 新增下一个数据 if( error/=0 ) then write(*,*) "Out of memory!" stop end if p=>p%next ! 移动到链表的下一个数据 nullify(p%next) end do write(*,"('总共有',I3,'位学生')") size allocate( s(size) ) p=>head do i=1,size s(i)=p%itemp=>p%next end do do while(.true.) write(*,*) "要查询几号同学的成绩?" read (*,*) i if ( i<1 .or. i>size ) exit ! 输入不合理的座号write(*,"(5(A6,I3))") "中文",s(i)%Chinese,& "英文",s(i)%English,& "数学",s(i)%Math,& "自然",s(i)%Science,& "社会",s(i)%Social end do write(*,"('座号',I3,'不存在, 程序结束.')") i stop end program 4.module typedef implicit none type :: datalink integer :: i type(datalink), pointer :: next end type datalink end module typedef program ex1012 use typedef implicit none type(datalink) , pointer :: p, head, nextinteger :: i,n,err write(*,*) 'Input N:' read(*,*) n allocate( head ) head%i=1 nullify(head%next) p=>head do i=2,n allocate( p%next,stat=err ) if ( err /= 0 ) then write(*,*) 'Out of memory!' stop endif p=>p%next p%i=i end do nullify(p%next) p=>head dowhile(associated(p)) write(*, "(i5)" ) p%i p=>p%next end do ! 释放链表的存储空间 p=>head do while(associated(p)) next => p%nextdeallocate(p) p=>next end do stop end program 第十一章 1.moduleutility implicit none interface area module procedure CircleArea module procedure RectArea end interface contains real function CircleArea(r) real, parameter :: PI=3.14159 real rCircleArea = r*r*PI return end function real function RectArea(a,b) real a,b RectArea = a*b return end function end module program main use UTILITY implicit none write(*,*) area(1.0) write(*,*) area(2.0,3.0)stop end program 2.module time_utility implicit none type :: timeinteger :: hour,minute,second end type time interface operator(+) module procedure add_time_time end interface contains functionadd_time_time( a, b ) implicit none type(time) :: add_time_timetype(time), intent(in) :: a,b integer :: seconds,minutes,carryseconds=a%second+b%second carry=seconds/60minutes=a%minute+b%minute+carry carry=minutes/60add_time_time%second=mod(seconds,60)add_time_time%minute=mod(minutes,60)add_time_time%hour=a%hour+b%hour+carry return end functionadd_time_time subroutine input( a ) implicit none type(time),intent(out) :: a write(*,*) " Input hours:" read (*,*) a%hourwrite(*,*) " Input minutes:" read (*,*) a%minute write(*,*) " Input seconds:" read (*,*) a%second return end subroutine input subroutine output( a ) implicit none type(time), intent(in) :: a write(*, "(I3,'hours',I3,' minutes',I3,' seconds')" ) a%hour,a%minute,a%second return end subroutine output end module time_utility program main usetime_utility implicit none type(time) :: a,b,c call input(a) callinput(b) c=a+b call output(c) stop end program main 3.modulerational_utility implicit none private public :: rational, &operator(+), operator(-), operator(*),& operator(/),assignment(=),operator(>),& operator(<), operator(==), operator(/=),& output, input type :: rational integer :: num, denom end type rational interface operator(+) module procedure rat__rat_plus_rat end interface interface operator(-)module procedure rat__rat_minus_rat end interface interfaceoperator(*) module procedure rat__rat_times_rat end interfaceinterface operator(/) module procedure rat__rat_div_rat end interface interface assignment(=) module procedure rat_eq_rat module procedureint_eq_rat module procedure real_eq_rat end interface interface operator(>) module procedure rat_gt_rat end interface interface operator(<) module procedure rat_lt_rat end interface interface operator(==) module procedure rat_compare_rat end interface interface operator(/=) module procedure rat_ne_rat end interface containsfunction rat_gt_rat(a,b) implicit none logical :: rat_gt_rattype(rational), intent(in) :: a,b real :: fa,fbfa=real(a%num)/real(a%denom)fb=real(b%num)/real(b%denom) if ( fa > fb ) then rat_gt_rat=.true. else rat_gt_rat=.false. end if return end function rat_gt_ratfunction rat_lt_rat(a,b) implicit none logical :: rat_lt_rattype(rational), intent(in) :: a,b real :: fa,fbfa=real(a%num)/real(a%denom) fb=real(b%num)/real(b%denom) if ( fb > fa ) then rat_lt_rat=.true. else rat_lt_rat=.false. end if return end function rat_lt_rat function rat_compare_rat(a,b) implicit nonelogical :: rat_compare_rat type(rational), intent(in) :: a,btype(rational) :: c c=a-b if ( c%num == 0 ) thenrat_compare_rat=.true. else rat_compare_rat=.false. end if returnend function rat_compare_rat function rat_ne_rat(a,b) implicit none logical :: rat_ne_rat type(rational), intent(in) :: a,btype(rational) :: c c=a-b if ( c%num==0 ) then rat_ne_rat=.false.else rat_ne_rat=.true. end if return end function rat_ne_ratsubroutine rat_eq_rat( rat1, rat2 ) implicitnone type(rational), intent(out):: rat1 type(rational),intent(in) :: rat2 rat1%num = rat2%num rat1%denom = rat2%denom return end subroutine rat_eq_rat subroutine int_eq_rat( int, rat ) implicit none integer, intent(out):: int type(rational), intent(in) :: rat int = rat%num / rat%denom return end subroutine int_eq_rat subroutinereal_eq_rat( float, rat ) implicit none real, intent(out) :: floattype(rational), intent(in) :: rat float = real(rat%num) /real(rat%denom) return end subroutine real_eq_rat function reduse( a ) implicit none type(rational), intent(in) :: a integer :: btype(rational) :: reduse b=gcv_interface(a%num,a%denom) reduse%num =a%num/b reduse%denom = a%denom/b return end function reduse functiongcv_interface(a,b) implicit none integer, intent(in) :: a,b integer :: gcv_interface if ( min(a,b) .eq. 0 ) then gcv_interface=1 return end if if (a==b) then gcv_interface=a return else if ( a>b ) thengcv_interface=gcv(a,b) else if ( a<b ) then gcv_interface=gcv(b,a)end if return end function gcv_interface recursive function gcv(a,b) result(ans) implicit none integer, intent(in) :: a,b integer :: m integer :: ans m=mod(a,b) select case(m) case(0) ans=b returncase(1) ans=1 return case default ans=gcv(b,m) end select return end function gcv function rat__rat_plus_rat( rat1, rat2 ) implicit none type(rational) :: rat__rat_plus_rat type(rational), intent(in) :: rat1,rat2 type(rational) :: act act%denom= rat1%denom * rat2%denom act%num = rat1%num*rat2%denom + rat2%num*rat1%denom rat__rat_plus_rat = reduse(act) return end function rat__rat_plus_rat functionrat__rat_minus_rat( rat1, rat2 ) implicit none type(rational) ::rat__rat_minus_rat type(rational), intent(in) :: rat1, rat2type(rational) :: temp temp%denom = rat1%denom*rat2%denom temp%num =rat1%num*rat2%denom - rat2%num*rat1%denom rat__rat_minus_rat = reduse( temp ) return end function rat__rat_minus_ratfunction rat__rat_times_rat( rat1, rat2 ) implicit nonetype(rational) :: rat__rat_times_rat type(rational), intent(in) :: rat1, rat2 type(rational) :: temp temp%denom = rat1%denom* rat2%denom temp%num = rat1%num * rat2%num rat__rat_times_rat = reduse(temp)return end function rat__rat_times_rat function rat__rat_div_rat( rat1, rat2 ) implicit none type(rational) :: rat__rat_div_rattype(rational), intent(in) :: rat1, rat2 type(rational) :: temptemp%denom = rat1%denom* rat2%num temp%num = rat1%num * rat2%denomrat__rat_div_rat = reduse(temp) return end function rat__rat_div_rat subroutine input(a) implicit none type(rational), intent(out) :: awrite(*,*) "分子:" read(*,*) a%num write(*,*) "分母:" read(*,*)a%denom return end subroutine input subroutine output(a) implicit none type(rational), intent(in) :: a if ( a%denom/=1 ) then write(*, "(' (',I3,'/',I3,')' )" ) a%num,a%denom else write(*, "(I3)" ) a%num end if return end subroutine output end module rational_utility program main use rational_utility implicit none type(rational) :: a,b,c call input(a) call input(b) c=a+b write(*,*) "a+b=" call output(c) c=a-bwrite(*,*) "a-b=" call output(c) c=a*b write(*,*) "a*b=" call output(c)c=a/b write(*,*) "a/b=" call output(c) if (a>b) write(*,*) "a>b" if(a<b) write(*,*) "a<b" if (a==b) write(*,*) "a==b" if (a/=b) write(*,*) "a/=b" stop end program main 4.module vector_utility implicit none type vector real x,y end type interface operator(+) module procedurevector_add_vector end interface interface operator(-) module procedurevector_sub_vector end interface interface operator(*) module procedure real_mul_vector module procedure vector_mul_real module procedure vector_dot_vector end interface interface operator(.dot.) module procedure vector_dot_vector end interface contains type(vector) functionvector_add_vector(a,b) type(vector), intent(in) :: a,bvector_add_vector = vector(a%x+b%x, a%y+b%y) end function type(vector) functionvector_sub_vector(a,b) type(vector), intent(in) :: a,bvector_sub_vector = vector(a%x-b%x, a%y-b%y) end function type(vector) function real_mul_vector(a,b) real, intent(in) :: a type(vector), intent(in) :: b real_mul_vector= vector( a*b%x, a*b%y ) end functiontype(vector) functionvector_mul_real(a,b) type(vector), intent(in) :: a real, intent(in) :: b vector_mul_real = real_mul_vector(b,a) end function real function vector_dot_vector(a,b) type(vector), intent(in) :: a,bvector_dot_vector = a%x*b%x + a%y*b%y end function subroutineoutput(vec) type(vector) :: vec write(*,"('('F6.2','F6.2')')") vec end subroutine end module program main use vector_utility implicit none type(vector) a,b,c a=vector(1.0, 2.0) b=vector(2.0, 1.0) c=a+b call output(c) c=a-b call output(c) write(*,*) a*b end program main。
Sun Microsystems, Inc.有关本文档的建议请发到:/hwdocs/feedback Fortran 用户指南Sun™ Studio 8部件号码 817-5802-10 2004 年 4 月,修订 A版权所有© 2004 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. 保留所有权利。
美国政府权利―商业软件。
政府用户在使用时应遵循 Sun Microsystems, Inc. 的标准许可协议和 FAR 及其补充中的适用规定。
使用本软件应遵守许可证条款。
此次分发可能包含由第三方开发的内容。
该产品的部分内容可能出自 Berkeley BSD 系统,由加州大学 (University of California) 授权。
UNIX 是在美国和其它国家(地区)的注册商标,由 X/Open Company, Ltd. 独家授权。
Sun、Sun Microsystems、Sun 徽标、Java、JavaHelp 是 Sun Microsystems, Inc. 在美国和其它国家(地区)的商标或注册商标。
所有的 SPARC 商标均需获得授权才能使用,它们是 SPARC International, Inc. 在美国和其它国家(地区)的商标或注册商标。
标有 SPARC 商标的产品都基于由Sun Microsystems, Inc. 开发的体系结构。
该产品为美国的出口控制法所涵该和控制,并有可能要遵守其它国家(地区)的出口或进口法规。
严禁将产品用于核、导弹、生化武器或核海事等最终目的或交给有此类企图的最终用户,无论是直接还是间接。
严禁将产品出口或再次出口到美国禁运的国家(地区)或美国出口排除列表中的实体,包括但不限于被拒绝的人员以及专门指定的公民列表。
本文档按“原样”提供,对所有明示或默示的条件、陈述和担保,包括对适销性、特殊用途的适用性或非侵权性的默示保证,均不承担任何责任,除非此免责声明的适用范围在法律上无效。
《FORTRAN-95程序设计》学习笔记《FORTRAN 95程序设计》学习笔记66RPG gg★目录★《FORTRAN 95程序设计》学习笔记 (2)基础知识(基础、字符串、FORMAT、隐式、TYPE)............................. 错误!未定义书签。
流程与控制(if、select、do) (8)数组(声明、隐式循环、整体操作、可变数组) (10)函数与子程序(子程序、函数、全局变量)13MODULE与面向对象(重载操作符、虚函数) (18)文件相关(OPEN、WRITE、READ) (20)指针(指向变量、数组、函数) (22)Visual Fortran 编译器(DLL,VB调用) (24)数值算法与IMSL(数值算法插件) (28)常用库函数(数学、数组、零碎、子程序) (30)◆10^10⏹复数:complex :: a=(2,3)◆实部:real(a) ;虚部:imag(a)⏹布尔型:Logical,.true. 和.false.★【语法与函数】字符串:character(20) string ⏹注意理解,fortran的弱智字符串就是一个长度不能变的一维的东西,极其猥琐,和Java、Ruby不能相提并论的⏹string(13:13) = “a”:对第13个字节的读、存⏹string(2:3) = “go”⏹string(6) = “我的妈呀”:从第6个位置开始设置为“我的妈呀”⏹a = string_a // string_b:用“//”连接两个字符串⏹【常用函数】char(num),ichar(char):ASCII码的转换相关功能⏹【常用函数】len(string),len_trim(string):长度,去掉尾部空格后的长度⏹【常用函数】index(string,key):找key在string首出现的位置⏹【常用函数】trim(string):返回去掉尾部空格的字符串(用途不大)⏹【函数】repeat(char,int):返回一个重复int次的char串⏹character(len=20) string 普通声明;character(len=*) string 接收的时候可自动长度★【规范格式】FORMAT格式化⏹e.g.◆w rite (*,100) A◆100 format(I4) ←这里是100号标识调用的格式⏹参数控制符(前面加数字为重复次数,如4I6或<a>I6。
FORTRAN 95 语法基础目录:一、应用程序的创建与运行/FORTRAN 95所用的字符/ 变量类型及其声明,常量声明/表达式与运算符二、输入与输出:表控、有格式三、选择语句与结构:IF语句、CASE结构四、DO循环结构五、数组:数组的声明,数组的引用,数组的算术运算,数组的输入、输出,给数组赋初值,动态数组,WHERE、FORALL语句六、子程序:语句函数,内部子程序,调用子程序时的虚实结合:形参为数组、非定界数组、子程序名、星号,递归子程序,外部子程序,纯子程序,逐元子程序七、派生数据类型与结构体八、指针与动态链表九、文件:存取方式,基本操作语句,各类文件的读写操作十、接口、模块十一、公用区、存储关联、数据块子程序十二、绘图:坐标系、设置图形颜色、创建图形程序/ 常用过程:设置线型、绘一像素点、设置当前位置、绘直线、绘弧线、绘矩形、绘多边形、绘制扇形(圆、椭圆)/ 文字信息的显示附/录:标准函数与标准子例行程序一、基础部份1-1 FORTRAN 95 应用程序的创建与运行创建或运行FORTRAN 95程序必须在Microsoft Developer Studio平台上进行。
尽管程序文本及相关文件的编辑可以在任一文本编辑器上进行,然后再拷到Studio的文档窗口中。
但最好还是一开始就进入Studio环境。
创建FORTRAN 95 程序的步骤大致如下:1)启动Microsoft Developer Studio可以通过不同方式运行dfdev.exe程序以启动Microsoft Developer Studio[开始] \ Compaq Visual Fortran 6 \ Developer Studio \ dfdev.exe:或……\CVF66 \\MSDEV98\dfdev.exeMicrosoft Developer Studio的界面如下图所示:文档窗口工作空间窗口输出窗口2)建立工作空间(WorkSpace)工作空间(WorkSpace)对应着windows资源管理器的一个文件夹。
第四章1.program main implicit none write(*,*)"Have a good time."write(*,*)"That's not bad."write(*,*) '"Mary" isn''t my name.' end program2.program main real, parameter :: PI=3implicit none.14159real radius write(*,*) "请输入半径长 "read(*,*)radius write(*,"('面积 ='f8.3)")radius*radius*PI end program3.program main implicit none real grades write(*,*)"请输入成绩 "read(*,*)grades write(*,"('调整后成绩为 'f8.3)") SQRT(grades)*10.0 end program4.integer a,b real ra,rb a=2 b=3 ra=2.0 rb=3.0 write(*,*) b/a! 输出 1,因为使用整数计算,小数部分会无条件舍去write(*,*) rb/ra! 输出 1.55.program main implicit none type distance real meter,inch,cm end type type(distance) ::d write(*,*)" 请输入长度 :"read(*,*)d%meter d%cm = d%meter*100 d%inch=d%cm/2.54write(*,"(f8.3'米 ='f8.3' 厘米 ='f8.3' 英寸 ')") d%meter,d%cm, d%inch end program第五章1.program main implicit none integer money real tax write(*,*) "请输入月收入"read(*,*) money if ( money<1000 ) then tax = 0.03else if ( money<5000) then tax=0.1else tax=0.15end if write(*,"('税金为 'I8)")nint(money*tax)end program2.program main implicit none integer day character(len=20) :: tv write(*,*) "请输入星期几 "read(*,*) day select case(day)case(1,4)tv = " 新闻 "case(2,5)tv = " 电视剧"case(3,6)tv=" 卡通 "case(7)tv = " 电影 "case default write(*,*) "错误的输入 "stop end select write(*,*) tv end program3.program main implicit none integer age, money real tax write(*,*)" 请输入年龄 " read(*,*)age write(*,*)"请输入月收入 "read(*,*)money if (age<50 )then if ( money<1000 ) then tax = 0.03else if ( money<5000 )then tax = 0.10else tax = 0.15end if else if(money<1000) then tax= 0.5else if( money<5000 )then tax = 0.7else tax = 0.10end if end if write(*,"('税金为 'I8)") nint(money*tax) end program4.program main implicit none integer year, days logical mod_4,mod_100, mod_400 write(*,*)" 请输入年份 "read(*,*) year mod_4= ( MOD(year,4)== 0 )mod_100 = ( MOD(year,100) == 0 )mod_400 = (MOD(year,400) ==0)if ((mod_4.NEQV. mod_100) .or. mod_400 ) then days = 366else days = 365end if write(*,"('这一年有 'I3' 天 ')") days stop end program第六章1.program main implicit none integer i do i=1,5write(*,*)"Fortran"end do stop end program2.program main implicit none integer i,sum sum = 0do i=1,99,2sum = sum+i end do write(*,*) sum stop end program3.program main implicit none integer,parameter:: answer= 45integer,parameter :: max = 5integer weight, i do i=1,max write(*,*) "请输入体重 "read(*,*) weight if (weight==answer) exit end do if(i<=max ) then write(*,*)" 猜对了 "else write(*,*) "猜错了 "end if stop end program4.program main implicit none integer, parameter :: max=10integer i real item real ans ans = 1.0item = 1.0do i=2,max item = item/real(i)ans = ans+itemend do write(*,*) ans stop end program5.program main implicit none integer, parameter :: length = 79character(len=length) :: input,output integer i,j write(*,*)"请输入一个字串 "read(*,"(A79)")input j=1 do i=1, len_trim(input)if ( input(i:i) /= ' ' ) then output(j:j)=input(i:i)j=j+1end if end do write(*,"(A79)") output stop end program第七章1.program main implicit none integer,parameter:: max= 10integer i integer:: a(max) =(/ (2*i,i=1,10) /) integer :: t!sum() 是 fortran库函数write(*,*) real(sum(a))/real(max)stop end program2.integer a(5,5)!5*5=25integer b(2,3,4)!2*3*4=24integer c(3,4,5,6) !3*4*5*6=360 integer d( -5:5)! 11 integer e( -3:3, -3:3)! 7*7=493.program main implicit none integer,parameter:: max=10integer f(max)integer i f(1)=0f(2)=1do i=3,max f(i)=f(i -1)+f(i-2)end do write(*,"(10I4)")f stop end program4.program main implicit none integer,parameter::size=10integer:: a(size)=(/ 5,3,6,4,8,7,1,9,2,10/)integer::i,j integer::t do i=1, size-1do j=i+1,size if ( a(i) < a(j) ) then ! a(i)跟 a(j)交换t=a(i)a(i)=a(j)a(j)=t end if end do end do write(*,"(10I4)")a stop end5.a(2,2)!1+(2 -1)+(2-1)*(5)=7a(3,3)! 1+(3-1)+(3-1)*(5) = 13第八章1.program main implicit none real radius,area write(*,*)" 请输入半径长 " read(*,*) radius call CircleArea(radius, area)write(*,"('面积 = 'F8.3)") area stop end program subroutine CircleArea(radius, area)implicit none real, parameter:: PI=3.14159 real radius, area area = radius*radius*PI return end subroutine2.program main implicit none real radius real,external:: CircleArea write(*,*)" 请输入半径长 "read(*,*)radius write(*,"('面积 ='F8.3)")CircleArea(radius)stop end program real function CircleArea(radius)implicit none real,parameter:: PI=3.14159 real radius CircleArea = radius*radius*PI return end function3.program main implicit none call bar(3)call bar(10)stop end program subroutine bar(length)implicit none integer,intent(in)::length integer i character(len=79)::string string=" "do i=1,length string(i:i)='*'end do write(*,"(A79)") string return end subroutine4.program main implicit none integer, external :: add write(*,*) add(100) end program recursive integer function add(n)result(sum)implicit none integer, intent(in):: n if ( n<0 ) then sum=0return else if ( n<=1 ) then sum=n return end if sum = n + add(n-1) return end function5.program main implicit none integer, external :: gcd write(*,*) gcd(18,12) end program integer function gcd(A,B)implicit none integer A,B,BIG,SMALL,TEMP BIG=max(A,B) SMALL=min(A,B)do while(SMALL /= 1 )TEMP=mod(BIG,SMALL)if ( TEMP==0 ) exit BIG=SMALL SMALL=TEMP end do gcd=SMALL return end function6.program main use TextGraphLib implicit none integer,parameter::maxx=60, maxy=20real,parameter:: StartX=0.0,EndX=3.14159*2.0real,parameter::xinc= (EndX-StartX)/(maxx -1)real x integer i,px,py call SetScreen(60,20)call SetCurrentChar('*')x=StartX do px=1,maxx py= (maxy/2)*sin(x)+maxy/2+1callPutChar(px,py)x=x+xinc end docall UpdateScreen()stop end program第九章1.program main implicit none character(len=79)::character(len=79)::buffer integer,parameter:: fileid = 10integer count integer::status=0logical alive write(*,*)":"read(*,"(A79)")inquire(,exist=alive)if(alive)then open(unit=fileid, , &access="sequential", status="old")count = 0do while(.true.) read(unit=fileid, fmt="(A79)", iostat=status ) buffer if ( status/=0 ) exit!没有资料就跳出循环write(*,"(A79)")buffer count=count+1if( count==24)then pause count=0end if end do else write(*,*)TRIM(),"doesn't exist." end if stop end2.program main implicit none character(len=79)::character(len=79)::buffer integer, parameter :: fileid = 10integer i integer :: status = 0logical alive write(*,*) ":" read(*,"(A79)")inquire(, exist=alive)if (alive )then open(unit=fileid,,& access="sequential",status="old")do while(.true.)read(unit=fileid,fmt="(A79)", iostat=status)buffer if(status/=0)exit!没有资料就跳出循环do i=1, len_trim(buffer)buffer(i:i) = char( ichar(buffer(i:i))-3 )end do write(*,"(A70)") buffer end do else write(*,*) TRIM()," doesn't exist."end if stop end3.program main implicit none type student integer chinese, english,math, science, social,total end type type(student)::s,total integer,parameter:: students=20, subjects=5integer i open(10,file="grades.bin",access="direct",recl=1)write(*,"(7A10)") " 座号 "," 中文 "," 英文 "," 数学 "," 自然 "," 社会 "," 总分 "total=student(0,0,0,0,0,0)do i=1, students read(10,rec=(i -1)*subjects+1)s%chinese read(10,rec=(i -1)*subjects+2) s%english read(10,rec=(i -1)*subjects+3)s%math read(10,rec=(i -1)*subjects+4) s%science read(10,rec=(i -1)*subjects+5)s%social s%total= s%chinese+s%english+s%math+s%science+s%social total%chinese= total%chinese+s%chinese total%english= total%english+s%english total%math= total%math+s%math total%science = total%science+s%science total%social=total%social+s%social total%total= total%total+s%total write(*,"(7I10)") i, s end do write(*,"(A10,6F10.3)") " 平均 ", & real(total%chinese)/real(students),&real(total%english)/real(students),& real(total%math)/real(students),&real(total%science)/real(students),& real(total%social)/real(students),&real(total%total)/real(students)stop end4.program main implicit none character(len=79)::character(len=79)::buffer integer, parameter :: fileid = 10integer i integer :: status = 0logical alive write(*,*) ":"read (*,"(A79)")inquire( , exist=alive)if ( alive ) then open(unit=fileid, , & access="sequential",status="old")do while(.true.)read(unit=fileid,fmt="(A79)", iostat=status)buffer if(status/=0)exit!没有数据就跳出循环do i=1, len_trim(buffer)buffer(i:i)=char(ichar(buffer(i:i)) -(mod(i -1,3)+1))end do write(*,"(A70)")buffer end do else write(*,*)TRIM()," doesn't exist."end if stop end5.module typedef type student integer::num integer::Chinese,English, Math, Natural, Social integer :: total integer :: rank end type end module program main use typedef implicit none integer,parameter::integer,parameter::students=20 character(len=80):: tempstr type(student) :: s(students) !储存学生成绩type(student) :: total!计算平均分数用integer i,num,error open(fileid,file="grades.txt",status="old",iostat=error)if( error/=0)then write(*,*)"Open grades.txt fail."stop end if read(fileid,"(A80)")tempstr! 读入第一行文字total=student(0,0,0,0,0,0,0,0)! 用循环读入每位学生的成绩do i=1,students read(fileid,*) s(i)%num, s(i)%Chinese, s(i)%English, &s(i)%Math, s(i)%Natural, s(i)%Social!计算总分s(i)%Total=s(i)%Chinese +s(i)%English+& s(i)%Math + s(i)%Natural + s(i)%Social!累加上各科的分数,计算各科平均时使用total%Chinese =total%Chinese+s(i)%Chinese total%English=total%English+ s(i)%English total%Math=total%Math+ s(i)%Math total%Natural=total%Natural+ s(i)%Natural total%Social= total%Social + s(i)%Social total%Total= total%Total + s(i)%Total end do call sort(s,students)!重新输出每位学生成绩write(*,"(8A7)") "座号 "," 中文 ","英文 "," 数学 "," 自然 "," 社会 "," 总分 "," 名次 "do i=1,students write(*,"(8I7)")s(i)end do!计算并输出平圴分数write(*,"(A7,6F7.1)")"平均 ",& real(total%Chinese)/real(students),&real(total%English)/real(students),&real(total%Math) /real(students),&real(total%Natural)/real(students),&real(total%Social)/real(students),& real(total%Total)/real(students)stop end program subroutine sort(s,n)use typedef implicit none integer n type(student) :: s(n), t integer i,j do i=1,n -1do j=i+1,n if ( s(i)%total < s(j)%total ) then t = s(i)s(i)=s(j)s(j) = t end if end do end do forall(i=1:n)s(i)%rank = i end forall end subroutine第十章1.integer(kind=4):: a! 4bytes real(kind=4):: b! 4 bytes real(kind=8):: c!8bytes character(len=10):: str!10bytes integer(kind=4),pointer:: pa ! 4 bytes real(kind=4), pointer :: pb! 4 bytes real(kind=8), pointer :: pc! 4 bytes character(len=10), pointer :: pstr ! 4 bytes type student integer Chinese, English, Math end type type(student) :: s! 12 bytes type(student), pointer :: ps ! 4 bytes2.integer, target :: a = 1 integer, target :: b = 2 integer, target :: c = 3 integer, pointer :: p p=>awrite(*,*) p! 1 p=>b write(*,*) p! 2 p=>c p=5 write(*,*) c! 53.module linklist type student integer :: num integer :: Chinese, English, Math, Science, Social end type type datalink type(student) :: item type(datalink), pointer :: next end type contains function SearchList(num, head)implicit none integer:: num type(datalink),pointer::head,p type(datalink),pointer::SearchList p=>head nullify(SearchList) do while( associated(p) )if ( p%item%num==num ) then SearchList => p return end if p=>p%next end do return end function end module linklist program ex1016use linklist implicit none character(len=20)::character(len=80):: tempstr type(datalink),pointer::head type(datalink),pointer:: p type(student), allocatable :: s(:)integer i,error,size write(*,*) ":"read(*,*)open(10, , status="old", iostat=error)if ( error/=0 ) then write(*,*) "Open !"stop end if allocate(head) nullify(head%next)p=>head size=0read(10, "(A80)") tempstr !读入第一行字符串 ,不需要处理它!读入每一位学生的成绩do while(.true.)read(10,fmt=*,iostat=error) p%item if( error/=0) exit size=size+1allocate(p%next,stat=error)! 新增下一个数据if ( error/=0 ) then write(*,*) "Out of memory!"stop end if p=>p%next !移动到链表的下一个数据nullify(p%next)end do write(*,"('总共有 ',I3,'位学生 ')")size allocate(s(size))p=>head do i=1,size s(i)=p%item p=>p%next end do do while(.true.)write(*,*) "要查询几号同学的成绩?"read (*,*) i if ( i<1 .or. i>size ) exit !输入不合理的座号write(*,"(5(A6,I3))")"中文 ",s(i)%Chinese,&" 英文 ",s(i)%English,&" 数学 ",s(i)%Math,&" 自然",s(i)%Science,&" 社会 ",s(i)%Social end do write(*,"(' 座号 ',I3,' 不存在 ,程序结束 .')") i stop end program4.module typedef implicit none type:: datalink integer:: i type(datalink), pointer:: next end type datalink end module typedef program ex1012use typedef implicit none type(datalink) , pointer :: p, head, next integer :: i,n,err write(*,*) 'Input N:'read(*,*) n allocate( head )head%i=1nullify(head%next)p=>head do i=2,n allocate(p%next,stat=err )if (err/= 0)then write(*,*)'Out of memory!' stop end if p=>p%next p%i=i end do nullify(p%next)p=>head do while(associated(p))write(*,"(i5)") p%i p=>p%next end do!释放链表的存储空间p=>head do while(associated(p))next => p%next deallocate(p)p=>next end do stop end program第十一章1.module utility implicit none interface area module procedure CircleArea module procedure RectArea end interface contains real function CircleArea(r)real, parameter:: PI=3.14159real r CircleArea=r*r*PI return end function real function RectArea(a,b)real a,b RectArea=a*b return end function end module program main use UTILITY implicit none write(*,*) area(1.0)write(*,*) area(2.0,3.0) stop end program2.module time_utility implicit none type :: time integer::hour,minute,second end type time interface operator(+)module procedure add_time_time end interface contains function add_time_time( a, b )implicit none type(time) :: add_time_time type(time),intent(in)::a,b integer::seconds,minutes,carry seconds=a%second+b%second carry=seconds/60 minutes=a%minute+b%minute+carry carry=minutes/60 add_time_time%second=mod(seconds,60)add_time_time%minute=mod(minutes,60) add_time_time%hour=a%hour+b%hour+carry return end function add_time_time subroutine input( a )implicit none type(time), intent(out) :: a write(*,*) " Input hours:"read(*,*)a%hour write(*,*)"Input minutes:"read(*,*)a%minute write(*,*)"Input seconds:"read (*,*)a%second return end subroutine input subroutine output( a )implicit none type(time), intent(in)::a write(*,"(I3,' hours',I3,' minutes',I3,' seconds')" ) a%hour,a%minute,a%second return end subroutine output end module time_utility program main use time_utility implicit none type(time):: a,b,c call input(a)call input(b)c=a+b call output(c)stop end program main3.module rational_utility implicit none private public:: rational, & operator(+),operator( -),operator(*),&operator(/),assignment(=),operator(>),& operator(<),operator(==), operator(/=),&output,input type:: rational integer :: num,denom end type rational interface operator(+)module procedure rat__rat_plus_rat end interface interface operator( -)module procedure rat__rat_minus_rat end interface interface operator(*)module procedure rat__rat_times_rat end interface interface operator(/)module procedure rat__rat_div_rat end interface interface assignment(=)module procedure rat_eq_rat module procedure int_eq_rat module procedure real_eq_rat endinterface interface operator(>)module procedure rat_gt_rat end interface interface operator(<)module procedure rat_lt_rat end interface interface operator(==) module procedure rat_compare_rat end interface interface operator(/=)module procedure rat_ne_rat end interface contains function rat_gt_rat(a,b)implicit none logical :: rat_gt_rat type(rational),intent(in)::a,b real::fa,fb fa=real(a%num)/real(a%denom)fb=real(b%num)/real(b%denom)if(fa>fb ) then rat_gt_rat=.true.else rat_gt_rat=.false.end if return end function rat_gt_rat function rat_lt_rat(a,b)implicit none logical:: rat_lt_rat type(rational),intent(in)::a,b real::fa,fb fa=real(a%num)/real(a%denom) fb=real(b%num)/real(b%denom)if( fb>fa) then rat_lt_rat=.true.else rat_lt_rat=.false.end if return end function rat_lt_rat function rat_compare_rat(a,b)implicit none logical:: rat_compare_rat type(rational), intent(in)::a,b type(rational)::c c=a-b if ( c%num==0)then rat_compare_rat=.true.else rat_compare_rat=.false.end if return end function rat_compare_rat function rat_ne_rat(a,b)implicit none logical:: rat_ne_rat type(rational),intent(in)::a,b type(rational):: c c=a-b if ( c%num==0)then rat_ne_rat=.false.else rat_ne_rat=.true.end if return end function rat_ne_rat subroutine rat_eq_rat(rat1, rat2)implicit none type(rational),intent(out)::rat1type(rational),intent(in)::rat2rat1%num= rat2%num rat1%denom=rat2%denom return end subroutine rat_eq_rat subroutine int_eq_rat( int, rat )implicit none integer, intent(out):: int type(rational), intent(in):: rat int= rat%num/rat%denom return end subroutine int_eq_rat subroutine real_eq_rat( float,rat)implicit none real,intent(out)::float type(rational),intent(in):: rat float = real(rat%num) / real(rat%denom)return end subroutine real_eq_rat function reduse( a )implicit none type(rational), intent(in)::a integer::b type(rational)::reduse b=gcv_interface(a%num,a%denom)reduse%num=a%num/b reduse%denom= a%denom/b return end function reduse function gcv_interface(a,b)implicit none integer, intent(in) :: a,b integer :: gcv_interface if ( min(a,b) .eq. 0 ) then gcv_interface=1return end if if(a==b)then gcv_interface=a return else if(a>b)then gcv_interface=gcv(a,b)else if(a<b)then gcv_interface=gcv(b,a)end if return end function gcv_interface recursive function gcv(a,b) result(ans)implicit none integer, intent(in) :: a,b integer:: m integer :: ans m=mod(a,b)select case(m)case(0)ans=b return case(1)ans=1return case default ans=gcv(b,m)end select return end function gcv function rat__rat_plus_rat( rat1,rat2)implicit none type(rational) :: rat__rat_plus_rat type(rational), intent(in) :: rat1,rat2type(rational) :: act act%denom= rat1%denom * rat2%denom act%num= rat1%num*rat2%denom + rat2%num*rat1%denom rat__rat_plus_rat =reduse(act)return end function rat__rat_plus_rat function rat__rat_minus_rat(rat1,rat2)implicit none type(rational):: rat__rat_minus_rat type(rational),intent(in)::rat1,rat2 type(rational):: temp temp%denom= rat1%denom*rat2%denom temp%num= rat1%num*rat2%denom- rat2%num*rat1%denom rat__rat_minus_rat= reduse(temp) return end function rat__rat_minus_rat function rat__rat_times_rat(rat1,rat2)implicit none type(rational) :: rat__rat_times_rat type(rational), intent(in) :: rat1, rat2 type(rational):: temp temp%denom= rat1%denom*rat2%denom temp%num= rat1%num *rat2%num rat__rat_times_rat= reduse(temp)return end function rat__rat_times_rat function rat__rat_div_rat( rat1,rat2)implicit none type(rational) :: rat__rat_div_rat type(rational), intent(in) :: rat1, rat2type(rational) :: temp temp%denom= rat1%denom*rat2%num temp%num= rat1%num* rat2%denom rat__rat_div_rat =reduse(temp)return end function rat__rat_div_rat subroutine input(a)implicit none type(rational),intent(out):: a write(*,*)" 分子 :"read(*,*)a%num write(*,*)" 分母 :"read(*,*)a%denom return end subroutine input subroutine output(a)implicit none type(rational), intent(in) :: a if ( a%denom/=1 ) then write(*, "(' (',I3,'/',I3,')' )" ) a%num,a%denom else write(*,"(I3)" )a%num end if return end subroutine output end module rational_utility program main use rational_utility implicit none type(rational) :: a,b,c call input(a)call input(b)c=a+b write(*,*)"a+b="call output(c)c=a-b write(*,*)"a -b="call output(c)c=a*b write(*,*)"a*b="call output(c)c=a/b write(*,*)"a/b="call output(c)if (a>b) write(*,*) "a>b"if (a<b) write(*,*) "a<b"if (a==b) write(*,*) "a==b"if (a/=b) write(*,*) "a/=b" stop end program main4.module vector_utility implicit none type vector real x,y end type interface operator(+)module procedure vector_add_vector end interface interface operator( -) module procedure vector_sub_vector end interface interface operator(*)module procedure real_mul_vector module procedure vector_mul_real module procedure vector_dot_vector end interface interface operator(.dot.)module procedure vector_dot_vector end interface contains type(vector) function vector_add_vector(a,b) type(vector), intent(in) :: a,b vector_add_vector = vector(a%x+b%x, a%y+b%y)end function type(vector) function vector_sub_vector(a,b)type(vector),intent(in)::a,b vector_sub_vector= vector(a%x-b%x, a%y-b%y)end function type(vector)function real_mul_vector(a,b)real, intent(in):: a type(vector),intent(in)::b real_mul_vector = vector(a*b%x, a*b%y)end functiontype(vector)function vector_mul_real(a,b) type(vector), intent(in) :: a real, intent(in) :: b vector_mul_real = real_mul_vector(b,a) end function real function vector_dot_vector(a,b)type(vector), intent(in) ::a,b vector_dot_vector= a%x*b%x+a%y*b%y end function subroutine output(vec) type(vector) :: vec write(*,"('('F6.2','F6.2')')")vec end subroutine end module program main use vector_utility implicit none type(vector)a,b,c a=vector(1.0, 2.0) b=vector(2.0, 1.0)c=a+b call output(c)c=a-b call output(c)write(*,*)a*b end program main。
《Fortran95语言程序设计》课程教学大纲课程英文名称:Fortran95 Programming Design课程编号:0332232002课程计划学时:32学分:2课程简介:FORTRAN语言程序设计是材料物理专业的开设的专业基础课, FORTRAN语言在科学计算领域有着十分广泛的应用。
通过本课程的学习,应使学生掌握FORTRAN95的基本概念,语法规则和利用FORTRAN95进行程序设计的方法。
使学生在后继课的学习中,能够利用FORTRAN95上机编程,解决相应的实际问题,并能在今后的学习和工作中,结合自己的专业知识,开发相应的计算机应用程序。
一、课程教学内容及教学基本要求第一章 Fortran语言程序设计概述本章重点:算法、程序基本结构难点:语言元素本章学时:2学时教学形式:讲授与上机实践相结合教具:计算机,投影仪第一节 Fortran语言程序设计概述本节要求了解:程序设计的过程、基本方法、程序设计语言的分类、Fortran语言的发展、Fortran77、Fortran95程序设计的构成及其兼容性,(考核概率20%)理解:算法的概念,掌握:算法的描述、程序基本结构与书写规则(考核概率100%)1 程序设计的过程算法的描述(重点,难点)2 程序设计的基本方法3 程序设计语言4 Fortran语言的发展5 Fortran95程序基本结构与书写规则(重点)6 Fortran95程序设计的兼容性第二节Fortran95开发环境(第一次上机实验课讲述)本节要求了解:在可视化编程的条件下Fortran 语言所具备的一些新的特点和功能,掌握:可视化编程所需的基础知识和一般步骤(考核概率100%)1 熟悉Fortran95 环境进入系统2 建立项目文件3 建立源程序文件4 输入源程序的内容5 编译、连接、运行作业:认真复习本章内容,预习第二章内容。
第二章数据类型及其运算本章重点:Fortran语言的基本数据类型及其常量表示方法难点:算术表达式的写法本章学时:1学时教学形式:讲授与上机实践相结合教具:计算机,投影仪第一节数据类型及其运算本节要求了解:各种类型常量、变量的定义、算术表达式的写法,(考核概率100%)掌握:Fortran语言的基本数据类型及其常量表示方法(考核概率50%)1 常量2 变量及其定义3符号常量及其定义4 Fortran表达式(重点)作业:认真复习本章内容。
FORTRAN95程序设计与数据结构基础教程课程设计一、课程设计概述本课程设计旨在让学生通过实战演练,掌握FORTRAN95程序设计与数据结构基础知识。
本课程设计需要完成以下任务:1.学习FORTRAN95程序设计语言的基础知识2.掌握FORTRAN95数据结构的基础知识3.基于FORTRAN95设计和实现一个程序,实现特定功能二、课程设计内容2.1 FORTRAN95程序设计语言的基础知识1.FORTRAN95的发展历史2.FORTRAN95程序结构3.FORTRAN95数据类型4.FORTRAN95运算符5.FORTRAN95输入输出语句6.FORTRAN95选择结构和循环结构7.FORTRAN95数组8.FORTRAN95子程序2.2 FORTRAN95数据结构的基础知识1.数据结构的概念与分类2.数组、队列、栈3.链表、树、图2.3 基于FORTRAN95设计和实现一个程序,实现特定功能本课程设计要求在掌握FORTRAN95程序设计语言和数据结构基础知识的基础上,设计和实现一个能够实现如下功能的程序:1.读取文件中保存的学生信息,包括学生姓名、学号、性别、年龄、成绩等2.在屏幕上输出学生信息,要求输出的信息包括学生姓名、学号、性别、年龄、成绩、排名等3.根据学生成绩,计算并输出班级平均分、最高分、最低分4.根据学生成绩,计算并输出优秀、及格和不及格的人数和比例5.根据学生成绩,将学生按照成绩排序,并按照成绩排名输出学生信息三、课程设计要求1.学生需使用FORTRAN95程序设计语言,完成指定任务2.程序设计需通过FORTRAN95编译器进行编译,并能正确运行3.代码需具备良好的注释和文档4.学生需提交程序设计报告,报告应包括以下内容:–设计思路–程序的详细设计–实现过程–测试结果分析四、课程设计考核方式学生将按照以下方式进行考核:1.对程序进行代码评分,包括代码的可读性、规范性、有效性和健壮性2.对程序设计报告进行评分,包括报告的逻辑性、清晰性、详细性和语言表达能力五、课程设计参考资料1.《Fortran 95/2003入门经典》2.网络教学资源3.教师提供的其他参考资料六、总结通过本课程设计,学生将能够深入了解FORTRAN95程序设计语言和数据结构基础知识,掌握程序设计能力,并拥有完成实际项目的能力。
第2篇. 计算的叙述算法的每一个步骤,都必须给予确切的定义。
对于算法当中所考虑的每一种情况,每一个有待执行的动作,都必须严格地和不含混地加以规定。
…对于以描述算法作为目的而设计出来的,采用了形式的定义的程序设计语言,或者说计算机语言,它的每一个语句都必须有非常确切的意义。
---- D.E.Knuth[1]《The Art of Computer Programming》本质上FORTRAN就是一门语言,一门人与计算机赖以进行有效交流的语言,在这个意义上和我们使用的中文,英文等没有本质差别。
现在假设要来描述一种大家都陌生的语言,那么总是要分成两个方面来描述,即一方面要描述这门语言的表象和形态,也就是它使用哪些符号,哪些词汇,一般的句式如何,怎样才能完整叙述一个任务之类;另一方面需要说明这门语言的语义,也就是说这门语言是如何用来表达我们需要它表达的意思的。
第4章基本上就是描述FORTRAN作为一种语言的基本形态,也就是书写这种语言的书写规则。
接下来几章则逐步说明如何用FORTRAN来表达我们的要求,或者反过来说,FORTRAN提供了些什么表达方式,以便我们用来向计算机提出合理的任务:● 表达基本数据;● 表达数据的结构;● 完整地描述数据;● 构造表达式;● 驱动计算的赋值;● 计算过程的结构控制;在整个第二篇,我们将领略到FORTRAN 95是如何能够做到精致地描述计算的,而把一个问题阐述清楚了,就意味着问题已经解决了一大半。
[1]Donald E. Knuth (高纳德),Stanford University的The Art of Computer Programming荣休教授,而The Art of Computer Programming(计算机程序设计技巧)正是他的伟大著作的名称。
洋洋七大卷的《The Art of Computer Programming》是当今全世界每一个计算机科学家所膜拜的圣经。
1974年在该书刚完成前面很少一部分时,就给他带来了计算机科学家们梦寐以求的图灵奖。
第4章FORTRAN 95语言的形貌要说明一门语言的形态,必须回答以下问题:●它使用哪些符号来表达信息?●它的词汇如何构成?●它的语句如何构成?●如何表达一个完整的任务?具体的对于一门计算机语言,把这几个问题更加明确地转换过来,就是:●它使用键盘上的哪些符号,各个符号有哪些用途?●它的词汇如何由键盘字符构成?含有哪些固定的词汇?以及容许自由构成合法词汇的规则是什么?●它具有哪些固定的语句格式?以及容许自由构成合法语句的规则是什么?●我们交待给计算机的任何任务,都必须明确说明任务的开始,执行步骤和完成,因此一段完整的源代码应该具备什么样的形式?以及应该具备哪些要素?本章就是要回答这些问题。
4.1 FORTRAN语言所使用的字符从最抽象的层面来看,人与计算机的交流只是信息的交流,而信息总是需要依靠某种信号来表示,对于人来说,最方便的就是字符。
而对于计算机来说,自然就是键盘所能敲出的那些字符(信号),因此下面就是要说明:●FORTRAN 95能识别键盘上敲出的哪些字符?●每个字符对于FORTRAN 95来说又意味着什么?4.1.1 FORTRAN 95所使用的基本字符按照FORTRAN 95标准的规定,一切FORTRAN 95的实现平台都必须使用下面表4-1所列出来的这个基本的字符集,或者说,这个字符集是所有遵循FORTRAN 95标准的编译器所使用的字符集的公共子集。
这样原则上,局限在这个字符集上的源码是能够被任何遵循FORTRAN 95标准的编译器所识别的。
表4-1基本的FORTRAN 95字符集:文字字符英文字母 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z数字0 1 2 3 4 5 6 7 8 9可以看到基本字符分为两大类:文字字符和特殊字符。
除了货币符号可以本地化之外,其他任何字符都必须依照表里的形式。
对于基本字符有如下几个问题需要予以注意。
一.文字字符的用处:●主要是命名的作用,可以用来命名语言中的一切对象,这三种符号可以混合使用;●其中数字还具有它本来的含义,就是表示数目。
二.特殊字符的用处:特殊字符主要具有功能的意义,如编辑功能,运算功能,语法功能等。
FORTRAN 95标准原则上接受小写字母。
因此除了以下位置,大小写是等价的。
三.大小写必须区分的位置:●作为字符常量的字符串里面;●输入输出的纪录里面;●作为编辑描述符的引号或撇号里面。
因为在上述几种情形,大小写是字符型数据的不同数据取值。
如果不幸遇到一个FORTRAN 95标准的怪异的编译平台,偏偏不接受小写字母,这是FORTRAN 95标准所许可的,这时就得小心了。
不过幸好我们常用的编译平台,例如CVF,都是接受小写字母的。
另外,在OPEN或者INQUIRE语句里面的FILE=或NAME=后面是否区分大小写,也是由编译平台指定的。
如果是需要调用其他语言写的子程序,而恰好该种语言(例如C语言)是区分大小写的,这时就需要特别小心。
【例4-1】如果用C写了两个子程序EIGEN和eigen,然后有如下的FROTRAN片断:EXTERNAL EIGEN...CALL EIGEN...END这时它是该引用EIGEN还是eigen呢?如果所使用的FROTRAN系统正好是怪异的那种,没问题。
如果是常见的如CVF,这时它就无法区分EIGEN和eigen,这样就必须给它们更换名称了。
四.数字的涵义:除了以下情形,数字总是表示十进位数字●属于二进制,八进制,十六进制的字面常量;●带有B,O,Z编辑描述符的输入输出纪录。
【例4-2】以下语句当中的数字不是属于十进位数字:DATA I, J, K / O‟1001‟, 23.54, Z‟5CA2‟ /其中第一个为八进制数,第二个为十进制数,第三个为十六进制数。
五.下划线的涵义:●下划线的主要作用就是置于单词之间代替空格,使得我们在命名时使用清楚的英语词汇。
●下划线不能置于任意名称的前面,但是可以置于名称的最后。
●下划线也用于在字面常量中区隔常量的值和种别参数。
无论给什么对象起名,都尽量使用完整的英语单词,同时使用下划线以区隔不同的单词。
所谓好记性不如烂笔头,只有这样才能切实保证你在任何时候,在程序代码的任意位置都知道任意变量等的含义。
4.1.2 与平台有关的FORTRAN辅助字符集上节列出的基本字符集是在一切FROTRAN的编译平台都可以使用的,被FORTRAN 95标准规定为必须使用的默认字符集。
另外还有些辅助的字符则是不同的平台有不同的用法约定。
辅助字符分两类:可打印字符和不可打印字符。
●可打印字符;各种本地化语言的字符,象汉字,希腊字母等,都可以应用在字符串,注释,和输入输出纪录当中。
●不可打印字符。
主要就是控制字符,例如制表符Tab键。
制表符(Tab键)在FORTRAN77标准当中主要用来表示6个空格,这样在固定源程序形式的代码的每行的开头使用Tab,就自动地空出6个空格。
对于一个FORTRAN77标准的编译系统来说,在固定源程序形式里的Tab被看成是至少6个空格,而在自由源程序形式里的Tab被看成1个空格。
这样如果Tab被放在文本当中用于输出格式控制,那么这种默认的转换方式,有时就会导致输出格式的混乱。
有关FORTRAN 95的辅助字符集的使用规则,请参考具体的编译系统的说明。
4.2 词汇所谓FORTRAN的词汇就是一个语句的最小的意义单位,它由一个或多个FORTRAN 字符集里的字符组成。
包括两类共6种,分类例举如下:●由文字字符组成的词汇,包括4种:·语句关键词:IMPLICIT·名称:EIGEN_FREQUENCY_3·由单个词汇组成的字面常量:1.234567_long·标识符:213●由特殊字符组成的—·算符: +,.OR.·定界符:逗号,=,=〉,:,::,;,%。
FORTRAN 95的一切合法的词汇都必须按照语法来构成。
完备的构词语法规则在附录B 给出。
下面分别予以详细说明。
1. 语句关键词语句关键词的功用:●标志语句本身。
【例4-3】下面的DO语句中的关键词DO本身标志了该语句DO I=1,500●标志选项。
【例4-4】下面的INTENT语句当中的IN,OUT,或INOUT。
INTENT(IN),A。
BINTENT(INOUT),X,Y,Z●用在语句当中,起分界的作用。
【例4-5】如下面DO语句当中的WHILEDO WHILE( .NOT. VECTOR )并非所有的语句都必须包含关键词,在FORTRAN里面,赋值语句和函数都不需要关键词。
尽管FORTRAN 95不区分大小写,本书任何地方出现的语句关键词都使用大写字母。
纯粹是为了醒目的原因。
2.名称在一个程序当中,任何对象都需要有一个名称,给它们命名所得到的词汇,可以说就是一般语言里的名词,这样的对象包括:变量,命名常量,程序单元,过程,公用块,构造,派生类型,哑元等。
名称的拼写规则为:● 名称必须由字母开头,可以由文字字符混合组成,而下划线不能作为名称的第一个字符。
● 一个名称至多允许含有31个字符。
3. 常量一个常量就是对一个值的合乎语法的字符标记。
常量分为字面常量和命名常量两种:●一个值如果没有在程序里面经过命名,则称为字面常量,这种常量不能取派生数据类型。
【例4-6】66953Z‟5120A‟2.3417.TRUE.(33.2, 5.0)● 一个值如果在程序里面经过命名,则称为命名常量,这种常量能取派生数据类型。
【例4-7】在如下声明语句当中的常量UNSTABLE_POINT为命名常量:REAL, DIMENSION(3), PARAMETER ::UNSTABLE_POINT =&(/5.332, 0.221, 190.632/)对于常量的语义,将在说明数据时进一步讨论。
4. 语句标签在一个程序单元内部,对任何一条语句,都可以在该语句的前面加上语句标签,以便在该程序单元内部的任何其他位置引用该语句。
需要引用其他语句的语句包括CALL语句,DO结构,分支语句,输入输出语句等。
语句标签的书写规则为:● 语句标签由1到5个十进制数字组成,其中必须至少有一个数字不能是0,例如000不能作为标识符;● 标识符以0开头是没有任何意义的,例如0034与34没有区别。
● 标识符不能放置于空语句之前。
【例4-8】456上面的语句只出现了一个语句标签,是不合法的。
●对于在一个程序单元内部,标识符不唯一出现的情形,具有特殊的含义,将在后面讨论。
5. 算符算符用在表达式当中,通过运算而获得某种类型的值。