module constant
implicit none
integer n,nmax,cnum
character(*):: filename
character*1 sign
integer count
integer error
end module constant
module dealfile
use constant
contains
subroutine totalnum(filename,count,nmax,sign,cnum())
integer count,nmax,i,n,tonum
character*1 buffer(1:nmax)
character(*):: filename
character*1 sign
character,allocatable :: string(:)
integer,allocatable :: cnum(:)
open(10,FILE=filename)
allocate(string(n))
allocate(cnum(n))
do n=1,count
READ(10,100) string(n)
100 format(<nmax>A1)
write(*,*)string(n)
buffer=string(n)
cnum(1)=1
do i=1,nmax-1
if((buffer(i)==sign) .AND. (buffer(i+1)/=sign)) cnum(n)=cnum(n)+1
end do
tonum=tonum+cnum(n)
end do
close(10)
end subroutine totalnum
end module dealfile
program main
use constant
use dealfile
print*,'文件名:'
read*,filename
count=0
open(10,file=filename,status='old')
do while (.true.)
read(10,*,iostat=error) line
if(error/=0)exit
count=count+1
end do
close(10)
print*,'行数:'
write(*,*)count
print*,'行的最大列数:'
read*,nmax
print*,'分隔符:'
read(*,100),sign
100 format(A1)
call totalnum(filename,count,nmax,sign,cnum(n))
print*,tonum
end program
F6(0AT]C6A(J`(2_22{MS]9.jpg (240.44 KB, 下载次数: 280)
错误····
Module Constant
Implicit None
Integer N, Nmax
integer , allocatable :: Cnum(:) !// 定义为可变
Character(len=512) :: Filename !// 必须指定长度
Character(len=1) :: Sign
integer :: Tonum !// Tonum放上面,不然主程序收不到
Integer Count
Integer Error
End Module Constant
Module Dealfile
Use Constant
Contains
Subroutine Totalnum(Filename, Count, Nmax, Sign, Cnum) !// 不带括号
Integer Count, Nmax, I, N
Character *1 Buffer(1:Nmax)
Character (*) :: Filename
Character *1 Sign
Character, Allocatable :: String(:)
Integer, Allocatable :: Cnum(:)
integer :: j
Open (10, File=Filename)
Allocate (String(Nmax)) !// 此处为 NMax
Allocate (Cnum(Nmax)) !// 同上
Tonum = 0 !// 初值要给
Do N = 1, Count
Read (10, 100) String(:N)
Write (*, *) String(N)
do j = 1 , n
Buffer(j:j) = String(j:j)
end do
Cnum(1) = 1
Do I = 1, Nmax - 1
If ((Buffer(I).Eq.Sign) .And. (Buffer(I+1).Ne.Sign)) Cnum(N) = Cnum(N) + 1
End Do
Tonum = Tonum + Cnum(N)
End Do
Close (10)
100 Format (<Nmax>A1)
End Subroutine Totalnum
End Module Dealfile
Program Main
Use Constant
Use Dealfile
Implicit None !// 非常重要
character(len=1) :: line
Print *, '文件名:'
Read *, Filename
Count = 0
Open (10, File=Filename, Status='old')
Do
Read (10, *, Iostat=Error) Line
If (Error.Ne.0) Exit
Count = Count + 1
End Do
Close (10)
Print *, '行数:'
Write (*, *) Count
Print *, '行的最大列数:'
Read *, Nmax
Print *, '分隔符:'
Read (*, 100), Sign
Call Totalnum(Filename, Count, Nmax, Sign, Cnum) !// cNum 不带括号
Print *, Tonum
100 Format (A1)
End Program Main
楚香饭 发表于 2014-11-28 17:26
你这代码问题太多了。语法错误一大堆,算法也不正确。
建议你直接使用这个代码:http://fcode.cn/code_gen ...
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |