a(i)跟a(j)交换t=a(i)a(i)=a(j)a(j)=tendifenddoenddowrite(*,"(10I4)")astopend(2,2)!
1+(2-1)+(2-1)*(5)=7a(3,3)!
1+(3-1)+(3-1)*(5)=13
第八章
mainimplicitnonerealradius,areawrite(*,*)"请输入半径长"read(*,*)radiuscallCircleArea(radius,area)write(*,"('面积='")areastopendprogramsubroutineCircleArea(radius,area)implicitnonereal,parameter:
:
PI=realradius,areaarea=radius*radius*PIreturnendsubroutine
mainimplicitnonerealradiusreal,external:
:
CircleAreawrite(*,*)"请输入半径长"read(*,*)radiuswrite(*,"('面积='")CircleArea(radius)stopendprogramrealfunctionCircleArea(radius)implicitnonereal,parameter:
:
PI=realradiusCircleArea=radius*radius*PIreturnendfunction
mainimplicitnonecallbar(3)callbar(10)stopendprogramsubroutinebar(length)implicitnoneinteger,intent(in):
:
lengthintegericharacter(len=79):
:
stringstring=""doi=1,lengthstring(i:
i)='*'enddowrite(*,"(A79)")stringreturnendsubroutine
mainimplicitnoneinteger,external:
:
addwrite(*,*)add(100)endprogramrecursiveintegerfunctionadd(n)result(sum)implicitnoneinteger,intent(in):
:
nif(n<0)thensum=0returnelseif(n<=1)thensum=nreturnendifsum=n+add(n-1)returnendfunction
mainimplicitnoneinteger,external:
:
gcdwrite(*,*)gcd(18,12)endprogramintegerfunctiongcd(A,B)implicitnoneintegerA,B,BIG,SMALL,TEMPBIG=max(A,B)SMALL=min(A,B)dowhile(SMALL/=1)TEMP=mod(BIG,SMALL)if(TEMP==0)exitBIG=SMALLSMALL=TEMPenddogcd=SMALLreturnendfunctionmainuseTextGraphLibimplicitnoneinteger,parameter:
:
maxx=60,maxy=20real,parameter:
:
StartX=,EndX=*real,parameter:
:
xinc=(EndX-StartX)/(maxx-1)realxintegeri,px,pycallSetScreen(60,20)callSetCurrentChar('*')x=StartXdopx=1,maxxpy=(maxy/2)*sin(x)+maxy/2+1callPutChar(px,py)x=x+xincenddocallUpdateScreen()stopendprogram
第九章
mainimplicitnonecharacter(len=79):
:
filenamecharacter(len=79):
:
bufferinteger,parameter:
:
fileid=10integercountinteger:
:
status=0logicalalivewrite(*,*)"Filename:
"read(*,"(A79)")filenameinquire(file=filename,exist=alive)if(alive)thenopen(unit=fileid,file=filename,&access="sequential",status="old")count=0dowhile(.true.)read(unit=fileid,fmt="(A79)",iostat=status)bufferif(status/=0)exit!
没有资料就跳出循环write(*,"(A79)")buffercount=count+1if(count==24)thenpausecount=0endifenddoelsewrite(*,*)TRIM(filename),"doesn'texist."endifstopend
mainimplicitnonecharacter(len=79):
:
filenamecharacter(len=79):
:
bufferinteger,parameter:
:
fileid=10integeriinteger:
:
status=0logicalalivewrite(*,*)"Filename:
"read(*,"(A79)")filenameinquire(file=filename,exist=alive)if(alive)thenopen(unit=fileid,file=filename,&access="sequential",status="old")dowhile(.true.)read(unit=fileid,fmt="(A79)",iostat=status)bufferif(status/=0)exit!
没有资料就跳出循环doi=1,len_trim(buffer)buffer(i:
i)=char(ichar(buffer(i:
i))-3)enddowrite(*,"(A70)")bufferenddoelsewrite(*,*)TRIM(filename),"doesn'texist."endifstopend
mainimplicitnonetypestudentintegerchinese,english,math,science,social,totalendtypetype(student):
:
s,totalinteger,parameter:
:
students=20,subjects=5integeriopen(10,file="",access="direct",recl=1)write(*,"(7A10)")"座号","中文","英文","数学","自然","社会","总分"total=student(0,0,0,0,0,0)doi=1,studentsread(10,rec=(i-1)*subjects+1)s%chineseread(10,rec=(i-1)*subjects+2)s%englishread(10,rec=(i-1)*subjects+3)s%mathread(10,rec=(i-1)*subjects+4)s%scienceread(10,rec=(i-1)*subjects+5)s%socials%total=s%chinese+s%english+s%math+s%science+s%socialtotal%chinese=total%chinese+s%chinesetotal%english=total%english+s%englishtotal%math=total%math+s%mathtotal%science=total%science+s%sciencetotal%social=total%social+s%socialtotal%total=total%total+s%totalwrite(*,"(7I10)")i,senddowrite(*,"(A10,")"平均",&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)stopendmainimplicitnonecharacter(len=79):
:
filenamecharacter(len=79):
:
bufferinteger,parameter:
:
fileid=10integeriinteger:
:
status=0logicalalivewrite(*,*)"Filename:
"read(*,"(A79)")filenameinquire(file=filename,exist=alive)if(alive)thenopen(unit=fileid,file=filename,&access="sequential",status="old")dowhile(.true.)read(unit=fileid,fmt="(A79)",iostat=status)bufferif(status/=0)exit!
没有数据就跳出循环doi=1,len_trim(buffer)buffer(i:
i)=char(ichar(buffer(i:
i))-(mod(i-1,3)+1))enddowrite(*,"(A70)")bufferenddoelsewrite(*,*)TRIM(filename),"doesn'texist."endifstopend
typedeftypestudentinteger:
:
numinteger:
:
Chinese,English,Math,Natural,Socialinteger:
:
totalinteger:
:
rankendtypeendmoduleprogrammainusetypedefimplicitnoneinteger,parameter:
:
fileid=10integer,parameter:
:
students=20character(len=80):
:
tempstrtype(student):
:
s(students)!
储存学生成绩type(student):
:
total!
计算平均分数用integeri,num,erroropen(fileid,file="",status="old",iostat=error)if(error/=0)thenwrite(*,*)"Openfail."stopendifread(fileid,"(A80)")tempstr!
读入第一行文字total=student(0,0,0,0,0,0,0,0)!
用循环读入每位学生的成绩doi=1,studentsread(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)%Chinesetotal%English=total%English+s(i)%Englishtotal%Math=total%Math+s(i)%Mathtotal%Natural=total%Natural+s(i)%Naturaltotal%Social=total%Social+s(i)%Socialtotal%Total=total%Total+s(i)%Totalenddocallsort(s,students)!
重新输出每位学生成绩write(*,"(8A7)")"座号","中文","英文","数学","自然","社会","总分","名次"doi=1,studentswrite(*,"(8I7)")s(i)enddo!
计算并输出平圴分数write(*,"(A7,")"平均",&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)stopendprogramsubroutinesort(s,n)usetypedefimplicitnoneintegerntype(student):
:
s(n),tintegeri,jdoi=1,n-1doj=i+1,nif(s(i)%total