Fortran Coder

查看: 8734|回复: 1
打印 上一主题 下一主题

[文件读写] 求助怎么样去写这个info.txt才能正确运行

[复制链接]

1

帖子

1

主题

0

精华

新人

F 币
15 元
贡献
4 点
跳转到指定楼层
楼主
发表于 2016-4-4 21:26:35 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
[Fortran] 纯文本查看 复制代码
Module StudMod
Implicit None
Integer , Parameter :: INUMBER_OF_COURSE = 6
Type T_Stud
Integer :: iStuNumber !//学号
Real :: rScore( INUMBER_OF_COURSE ) !//6门成绩
Real :: rScoreAll , rScoreAvg !//总分和平均分
EndType T_Stud
Contains
Subroutine GetStudInfo( cFilename , stStudArray )
Character( Len = * ) ,Intent( IN ) :: cFilename
Type( T_Stud ) , Intent( OUT) :: stStudArray( : )
Integer :: i
Open( 12 , File = cFilename )
Do i = 1 , size( stStudArray )
Read( 12 , * ) stStudArray( i )%iStuNumber , stStudArray(i)%rScore
EndDo
Close( 12 )
EndSubroutine GetStudInfo
Subroutine CalcScore( stStud )
Type( T_Stud ) , Intent( INOUT ) :: stStud
Integer :: i
Real :: rTmp
rTmp = 0.0
Do i = 1 , INUMBER_OF_COURSE
rTmp = rTmp + stStud%rScore( i )
End Do
stStud%rScoreAll = rTmp
stStud%rScoreAvg = rTmp / INUMBER_OF_COURSE
End Subroutine CalcScore
Subroutine OutputInfo( stStud , iNum )
Type( T_Stud ) , Intent( IN ) :: stStud(:)
Integer , Intent( IN ) :: iNum
Write(*,"(i,6f7.1,f9.1,f9.3)") stStud( iNum )%iStuNumber,stStud( iNum )%rScore ,stStud( iNum )%rScoreAll, stStud( iNum )%rScoreAvg
End Subroutine OutputInfo
Subroutine HeapSort( stD , comp_f )
Type ( T_Stud ) , Intent( INOUT ) :: stD( : )
Real , External :: comp_f
Integer i,ir,j,l,n
Type ( T_Stud ) :: stTemp
n = size( stD )
If ( n < 2 ) Return
l = n / 2 + 1
ir= n
Do while( .TRUE. )
If( l > 1 ) then
l = l - 1
stTemp = stD( l )
Else
stTemp = stD( ir)
stD( ir ) = stD( 1 )
ir = ir - 1
If( ir == 1 ) then
stD( 1 ) = stTemp
return
End If
End If
i = l
j = l + l
Do while( j<=ir )
If( ( j < ir ) ) then
If ( comp_f( stD(j) , std(j+1) ) > 0.0 ) then
j = j+1
End If
EndIf
If( comp_f( stTemp , stD(j) ) > 0.0 )then
stD(i) = stD( j )
i = j
j = j + j
Else
j = ir + 1
End If
EndDo
stD( i ) = stTemp
End Do
End Subroutine HeapSort
Real Function comp_f_Score( st1 , st2 )
Type( T_Stud ) , Intent( IN ) :: st1 , st2
comp_f_Score = st1%rScoreAll - st2%rScoreAll
End Function comp_f_Score
End Module StudMod
Program Main
Use StudMod
Implicit None
Integer , Parameter :: INUMBER_OF_STUD = 35 !35个学生
Type( T_Stud ) :: stStuds( INUMBER_OF_STUD ) !定义学生数组结构体
Integer :: i , iCourseNumber 
Call GetStudInfo( "info.dat" , stStuds ) !读入学生信息
Do i = 1 , INUMBER_OF_STUD
Call CalcScore( stStuds(i) ) !计算平均分和总分
End Do
Call HeapSort( stStuds , comp_f_Score ) !排序
Do i = 1 , INUMBER_OF_STUD
Call OutputInfo( stStuds , i ) !输出排序后的学生信息
End Do
Write(*,*) "请输入挑选的课程号(1-6):"
Read(*,*) iCourseNumber
Do i = 1 , 5
If( stStuds(i)%rScore( iCourseNumber ) > 90.0 ) then
Call OutputInfo( stStuds , i ) !输出满足筛选条件的学生信息
End If
End Do
End Program Main

这儿是运行出错的:run-time error F6501: READ(info.dat)
- end of file encountered
Press any key to continue

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-6-17 09:11

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表