2017-06-06 72 views
1

我試圖寫代碼來獲得以下格式計數句子

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
  • 從1而不是0開始的I值,使用1而不是0

  • 起始值開始的J值從0而不是20開始,在之後加上而不是之前的輸出 - 或者不是使用單獨的變量j只是從它計算它我

  • 不要打開文件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' 
+0

非常感謝dave_thompson_085 – Chung