用句子

时间:2017-06-06 00:44:52

标签: fortran counting sentence

我正在尝试编写代码以获取以下格式

filein(i)='box_01_step_000000000000j.pdb'

输出应该是:

filein(1)='box_01_step_00000000000000.pdb' 
filein(2)='box_01_step_00000000000020.pdb'
filein(3)='box_01_step_00000000000040.pdb'
filein(4)='box_01_step_00000000000060.pdb'
filein(5)='box_01_step_00000000000080.pdb'
filein(6)='box_01_step_00000000000100.pdb'
filein(7)='box_01_step_00000000000120.pdb'
filein(8)='box_01_step_00000000000140.pdb'
filein(9)='box_01_step_00000000000160.pdb'
filein(10)='box_01_step_00000000000180.pdb'
filein(11)='box_01_step_00000000000200.pdb'

我的代码是

program main 
implicit none 
 integer :: i,n,j
 character(4):: filein,box,step,pdb
 do i = 0, 501
    open (300,file='in')
 end do
 j=0
 do i = 0, 501   
  j=j+20
  WRITE(300,*) "'filein'",'"("',(i),'")"',"'='","'box_01_step_'","'000000000000'",j,"'.pdb'"
 enddo
 close (300)
end program

我得到了

 'filein'"("           0 ")"'=''box_01_step_''000000000000'          20 '.pdb'
 'filein'"("           1 ")"'=''box_01_step_''000000000000'          40 '.pdb'
 'filein'"("           2 ")"'=''box_01_step_''000000000000'          60 '.pdb'
 'filein'"("           3 ")"'=''box_01_step_''000000000000'          80 '.pdb'

我怎么做到

filein(1)='box_01_step_00000000000000.pdb' 
filein(2)='box_01_step_00000000000020.pdb'
filein(3)='box_01_step_00000000000040.pdb'

1 个答案:

答案 0 :(得分:1)

  • 从1而不是0开始i值,使用起始值1而不是0

  • 从0而不是20开始j值,在输出之后添加而不是之前 - 或者不使用单独的变量j只是从i计算它

  • 不要打开文件502次;一次就够了

  • 将j转换为带有前导零的十进制最简单的方法是使用带有Iw.m说明符的格式化输出,格式化输出也是一种更简单的方法来完成你想要的其余部分

程序:

program SO44379759
  implicit none
  integer :: i
  open(300,file='in')
  ! j=0
  do i=1,501
    ! j=j+20
    write(300,'(a,i0,a,i14.14,a)') "filein(",i,")='box_01_step_",(i-1)*20,".pdb'"
  end do
  close(300)
end program

输出:

filein(1)='box_01_step_00000000000000.pdb'
filein(2)='box_01_step_00000000000020.pdb'
filein(3)='box_01_step_00000000000040.pdb'
filein(4)='box_01_step_00000000000060.pdb'
filein(5)='box_01_step_00000000000080.pdb'
filein(6)='box_01_step_00000000000100.pdb'
filein(7)='box_01_step_00000000000120.pdb'
filein(8)='box_01_step_00000000000140.pdb'
filein(9)='box_01_step_00000000000160.pdb'
filein(10)='box_01_step_00000000000180.pdb'
filein(11)='box_01_step_00000000000200.pdb'
filein(12)='box_01_step_00000000000220.pdb'
filein(13)='box_01_step_00000000000240.pdb'
filein(14)='box_01_step_00000000000260.pdb'
filein(15)='box_01_step_00000000000280.pdb'
filein(16)='box_01_step_00000000000300.pdb'
filein(17)='box_01_step_00000000000320.pdb'
filein(18)='box_01_step_00000000000340.pdb'
filein(19)='box_01_step_00000000000360.pdb'
filein(20)='box_01_step_00000000000380.pdb'