[Fortran] 纯文本查看 复制代码
module linklist
type student
integer::num,Chi,Eng,Math,Sci,Sol
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 ex1016
use linklist
implicit none
character(len=20)::filename
character(len=80)::tempstr
type(datalink),pointer::head
type(datalink),pointer::p
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
do while(.true.)
write(*,*)"要查询几号同学的成绩?"
read(*,*)i
if(i<1.or.i>size)exit
p=>SearchList(i,head)
if(associated(p))then
write(*,"(5(A6,I3))")"中文",p%item%Chi,"英文",p%item%Eng,&
"数学",p%item%Math,"自然",p%item%Sci,"社会",p%item%Sol
else
exit
end if
end do
write(*,"('座号',I3,'不存在,程序结束')")i
stop
end program