求助:求精简一个自己写的格式化输出小程序
想要实现的功能如下:输入文件input.txt内容如下
Trajectory MaxJob I 4
Traj num 1 Geometries R N= 4
a1 a2 a3 a4
Traj num 2 Geometries R N= 3
b1 b2 b3
Traj num 3 Geometries R N= 6
c1 c2 c3 c4 c5
c6
Traj num 4 Geometries R N= 1
d1
输入文件input.txt内容如下
1a1 b1 c1 d1
2a2 b2 c2
3a3 b3 c3
4a4 c4
5 c5
6 c6
我的fortran程序段fortran90.f90内容如下
module util
contains
subroutine loclabel(fileid,label,ifound,irewind)
integer fileid,error
integer,optional :: ifound,irewind
character(len=80) :: c80
CHARACTER(LEN=*) label
if ((.not.present(irewind)).or.(present(irewind).and.irewind==1)) rewind(fileid)
do while(.true.)
read(fileid,"(a80)",iostat=ierror) c80
if (index(c80,label)/=0) then
backspace(fileid)
if (present(ifound)) ifound=1
return
end if
if (ierror/=0) exit
end do
if (present(ifound)) ifound=0
end subroutine
end module
program formout
!implicit none
use util
character infilename*200,outfilename*200,ord*8,findtraj*27
integerntraj,nmax,i,ntmp,j,k,l
logical alive
integer(4),allocatable :: nnum(:)
character(8),allocatable :: bond(:,:)
write(*,*) "Input the .txt filename to be loaded"
do while(.true.)
read(*,"(a)") infilename
inquire(file=infilename,exist=alive)
if (alive) exit
write(*,*) "Cannot found this file, input again"
end do
open(10,file=infilename,status="old")
call loclabel(10,'Trajectory MaxJob')
read(10,"(49x,i12)") ntraj
write(*,"('Number of trajs:',i10)") ntraj
allocate(nnum(ntraj))
nnum=0
do i=1,ntraj
write(findtraj,"(a,i8,a)") 'Traj num',i,' Geometries'
call loclabel(10,findtraj,ifound)
read(10,"(49x,i12)") ntmp
nnum(i)=ntmp
end do
nmax=MaxVal(nnum)
write(*,"('The max step in some traj :',i10)") nmax
close(10)
allocate(bond(nmax,(ntraj+1)))
bond=' '
do i=1,nmax
write(ord,"(i8)") i
bond(i,1)=ord
end do
l=1
do while (l <= ntraj)
open(10,file=infilename,status="old")
write(findtraj,"(a,i8,a)") 'Traj num',l,' Geometries'
call loclabel(10,findtraj,ifound)
read(10,"(49x,i12)") ntmp
l=l+1
!REWIND(10)
!nnum(i)
do k=1,ntmp
read(10,'(a8,$)') bond(k,l)!为了不换行
end do
write(*,*) bond
end do
write(*,*) "aaaaa"
write(*,*) bond
close(10)
print*, "4.0"
write(*,*) "Input the .txt filename to be outputted"
read(*,"(a)") outfilename
open(10,file=outfilename,status="replace")
do i=1,nmax
do j=1,(ntraj+1)
write(10,'(a10,$)') bond(i,j)
end do
write(10,"(/)")
end do
close(10)
pause
end program formout
看了一遍代码,感觉没有什么值得精简的。
也编译了一下,没有问题。
另外,没搞明白 input.txt 到底应该是什么内容。所以也没运行 vvt 发表于 2016-6-14 09:13
看了一遍代码,感觉没有什么值得精简的。
也编译了一下,没有问题。
另外,没搞明白 input.txt 到底应该是 ...
谢谢回复。
input.txt内容已在帖子开头给出来了。 你给了两段 input.txt 的内容。到底是第一段,还是第二段?还是两端的组合?
或者你可以直接上传该文件。 本帖最后由 一声叹息010 于 2016-6-17 12:05 编辑
fcode 发表于 2016-6-17 08:23
你给了两段 input.txt 的内容。到底是第一段,还是第二段?还是两端的组合?
或者你可以直接上传该文件。 ...
是我写错了,第二个input.txt其实应该是output.txt,文件已经上传
我自己减了减,还是觉得啰嗦
subroutine loclabel(fileid,label)
integer fileid,ierror
character(80) c80
CHARACTER(LEN=*) label
do while(.true.)
read(fileid,"(a80)",iostat=ierror) c80
if (index(c80,label)/=0) then
backspace(fileid)
return
end if
if (ierror/=0) exit
end do
end subroutine
program formatting
implicit none
character infilename*200,outfilename*200,findtraj*27
integerntraj,nmax,ntmp,i,j
logical alive
integer(4),allocatable :: nnum(:)
character(8),allocatable :: bond(:,:)
write(*,*) "Input the .txt filename to be loaded"
do while(.true.)
read(*,"(a)") infilename
inquire(file=infilename,exist=alive)
if (alive) exit
write(*,*) "Cannot found this file, input again"
end do
open(10,file=infilename,status="old")
call loclabel(10,'Trajectory MaxJob')
read(10,"(49x,i12)") ntraj
write(*,"('Number of trajs:',i10)") ntraj
allocate (nnum(ntraj))
do i=1,ntraj
write(findtraj,"(a,i8,a)") 'Traj num',i,' Geometries'
call loclabel(10,findtraj)
read(10,"(49x,i12)") ntmp
nnum(i)=ntmp
end do
nmax=MaxVal(nnum)
write(*,"('The max-step :',i10)") nmax
allocate (bond(ntraj,nmax))
bond=' '
rewind(10)
i=1
do while (i <= ntraj)
write(findtraj,"(a,i8,a)") 'Traj num',i,' Geometries'
call loclabel(10,findtraj)
read(10,"(49x,i12)") ntmp
do j=1,ntmp
read(10,'(a8,$)') bond(i,j)
end do
i=i+1
end do
close(10)
write(*,*) "Input the .txt filename to be outputted"
read(*,"(a)") outfilename
open(11,file=outfilename,status="replace")
do j=1,nmax
write(11,"(i4,'',$)") j
do i=1,ntraj
write(11,"(a8,$)") bond(i,j)
end do
write(11,"(/)")
end do
close(11)
end program formatting
页:
[1]