Shiev 发表于 2014-10-16 07:12:38

提示运行错误,但能输出正确结果

这个程序是摘自宋叶志的《fortran 95/2003 科学计算与工程》,目的是求上、下三角矩阵方程组。其中有一个输入和输出txt文件的过程(fin.txt/fout.txt)。
编译成功,但运行不成功,提示"end of file during read,unit 11……"。奇怪的是可以成功得输出fout.txt 文件,运算结果也正确,这是什么原因呢?谢谢指教。

Win 7,IVF,Fortran95

module tri_eq
!----------------------------------------module coment
!Version   :V1.0   
!Coded by    :syz
!Date      :2010-4-8
!-----------------------------------------------------
!Description : 用于解上、下三角形线性方程组的回带方法模块
!   
!-----------------------------------------------------
!Contains    :
!      1.   
!      2.
!-----------------------------------------------------

contains


subroutine uptri(A,b,x,N)
!---------------------------------subroutinecomment
!Version   :V1.0   
!Coded by:syz
!Date      :2010-4-8
!-----------------------------------------------------
!Purpose   :上三角方程组的回带方法
!               Ax=b
!-----------------------------------------------------
!Inputparameters:
!       1.   A(N,N)系数矩阵
!       2.   b(N)右向量
!       3.   N方程维数
!Output parameters:
!       1.x方程的根
!       2.
!Common parameters:
!
!----------------------------------------------------

implicit real*8(a-z)

integer::i,j,k,N

real*8::A(N,N),b(N),x(N)

x(N)=b(N)/A(N,N)

!回带部分
do i=n-1,1,-1
   
    x(i)=b(i)
   do j=i+1,N
    x(i)=x(i)-a(i,j)*x(j)
   end do
    x(i)=x(i)/A(i,i)

end do

end subroutine uptri


subroutine downtri(A,b,x,N)
!---------------------------------subroutinecomment
!Version   :V1.0   
!Coded by:syz
!Date      :2010-4-9
!-----------------------------------------------------
!Purpose   :下三角方程组的回带方法
!               Ax=b
!-----------------------------------------------------
!Inputparameters:
!       1.   A(N,N)系数矩阵
!       2.   b(N)右向量
!       3.   N方程维数
!Output parameters:
!       1.x方程的根
!       2.
!Common parameters:
!
!----------------------------------------------------

implicit real*8(a-z)
integer::i,j,N
real*8::A(N,N),b(N),x(N)

x(1)=b(1)/a(1,1)

do k=2,N
   x(k)=b(k)
   do i=1,k-1
      x(k)=x(k)-a(k,i)*x(i)
   end do
   x(k)=x(k)/a(k,k)

end do

end subroutine downtri


end module tri_eq

!##############################################################
module driver
!----------------------------------------module coment
!Version   :V1.0   
!Coded by    :syz
!Date      :
!-----------------------------------------------------
!Description :驱动函数模块
!   
!-----------------------------------------------------
!Parameters:
!      1.
!      2.
!Contains    :
!      1.       dir_main驱动函数入口
!      2.       dri_up    当读到关键字uptri 时启动该函数
!      3.       dri_down当读到关键字 downtri时启动该函数
!-----------------------------------------------------
!Post Script :
!      1.
!      2.
!-----------------------------------------------------

contains

subroutine dri_main()
!---------------------------------subroutinecomment
!Version   :V1.0   
!Coded by:syz
!Date      :2010-4-9
!-----------------------------------------------------
!Purpose   :驱动程序入口函数
!   
!-----------------------------------------------------
!Inputfiles:
!       1.   fin.txt准备数据
!       2.   
!Output files:
!       1.fout.txt 输出结果文件
!       2.
!
!----------------------------------------------------

implicit real*8(a-z)
integer::ioerr
character(20)::upordown

open(unit=11,file='fin.txt',iostat=ioerr)
open(unit=12,file='fout.txt')

do
read(11,*)upordown
!读输入文件
!当读到关键字uptri时启动上三角矩阵计算
!当读到关键字downtri时候启动下三角矩阵计算

if ( upordown(1:5) == 'uptri' ) then
    call dri_up()
else if (upordown(1:)=='downtri')then
    call dri_down()
end if

if (ioerr/=0) exit
!读到文件结束时,退出读文件
end do

end subroutine dri_main


subroutine dri_up()
!---------------------------------subroutinecomment
!Version   :V1.0   
!Coded by:syz
!Date      :2010-4-9
!-----------------------------------------------------
!Purpose   : 启动上三角阵的计算
!   
!-----------------------------------------------------
use tri_eq
implicit real*8(a-z)

integer,parameter::N=4

integer::i,j
real*8::A(N,N),b(N),x(N)


read(11,*)((A(i,j),j=1,N),i=1,N)
!读入B向量
read(11,*) b

call uptri(A,b,x,N)

write(12,101)x
101 format(T5,'上三角形方程组的解',/,T4,'x=',4(/F12.8))

end subroutine dri_up


subroutine dri_down()
!---------------------------------subroutinecomment
!Version   :V1.0   
!Coded by:syz
!Date      :2010-4-9
!-----------------------------------------------------
!Purpose   :启动下三角阵的计算
!   
!-----------------------------------------------------

use tri_eq

implicit real*8(a-z)

integer,parameter::N=4

integer::i,j
real*8::A(N,N),b(N),x(N)


read(11,*)((A(i,j),j=1,N),i=1,N)
!读入B向量
read(11,*) b

call downtri(A,b,x,N)

write(12,101)x
101 format(/,T5,'下三角形方程组的解',/,T4,'x=',4(/F12.8))

end subroutine dri_down

end module driver



!-----------------------------------------------------
program main
!--------------------------------------program comment
!Version   :V1.0   
!Coded by:syz
!Date      :2010-4-9
!-----------------------------------------------------
!Purpose   :计算上、下三角形方程组
!   
!-----------------------------------------------------
!In put datafiles :
!       1.fin.txt输入方程系数
!       2.
!Output data files:
!       1. fout.txt计算结果
!       2.
!-----------------------------------------------------
!Post Script :
!       1.    需要准备输入数据
!
!       2.    由主函数启动驱动程序进行计算   
!-----------------------------------------------------
!use tri_eq
use driver

!调用驱动函数
call dri_main()

end program main

楚香饭 发表于 2014-10-16 08:14:15

所以说这本书垃圾呢。

第 151 行改为:
read(11,*,iostat=ioerr)upordown
if (ioerr/=0) exit

hanshikai 发表于 2014-10-16 08:16:48

本帖最后由 hanshikai 于 2014-10-16 08:19 编辑

额,没考虑清楚,编辑掉吧:-lol

andy8496 发表于 2014-10-16 13:37:13

楚香饭 发表于 2014-10-16 08:14
所以说这本书垃圾呢。

第 151 行改为:


不是这样?
read(11,*,iostat=ioerr)upordown
if (ioerr/=0) exit

fcode 发表于 2014-10-16 13:43:02

是的,沙发笔误了

Shiev 发表于 2014-10-16 15:19:19

谢谢各位,问题解决了:-loveliness::-loveliness:
页: [1]
查看完整版本: 提示运行错误,但能输出正确结果