Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm fortran中数据提取的建议_Algorithm_Loops_Fortran - Fatal编程技术网

Algorithm fortran中数据提取的建议

Algorithm fortran中数据提取的建议,algorithm,loops,fortran,Algorithm,Loops,Fortran,我使用F95/90和IBM编译器。我试图从块中提取数值并写入文件。我在输出中遇到了一个我无法理解的奇怪错误。每次我执行程序时,它都会跳过“Beta”和“END”之间的循环。我正在尝试读取和存储这些值。 Alpha和Beta循环中的行数不是固定的。所以一个简单的“做循环”对我来说是没有用的。我尝试了“do-while”循环和“if-else”,但它仍然跳过了“Beta”部分 Alpha Singles Amplitudes 15 3 23 4 -0.186952 15

我使用F95/90和IBM编译器。我试图从块中提取数值并写入文件。我在输出中遇到了一个我无法理解的奇怪错误。每次我执行程序时,它都会跳过“Beta”和“END”之间的循环。我正在尝试读取和存储这些值。 Alpha和Beta循环中的行数不是固定的。所以一个简单的“做循环”对我来说是没有用的。我尝试了“do-while”循环和“if-else”,但它仍然跳过了“Beta”部分

Alpha Singles Amplitudes
15      3    23      4   -0.186952
15      3    26      4    0.599918
15      3    31      4    0.105048
15      3    23      4    0.186952
Beta  Singles Amplitudes
15      3    23      4    0.186952
15      3    26      4   -0.599918
15      3    31      4   -0.105048
15      3    23      4   -0.186952
END `
简单的简短代码是:

program test_read

   implicit none

      integer::nop,a,b,c,d,e,i,j,k,l,m,ios
      double precision::r,t,rr
      character::dummy*300
      character*15::du1,du2,du3
      open (unit=10, file="1.txt", status='old',form='formatted')

  100   read(10,'(a100)')dummy

        if (dummy(1:3)=='END') goto 200

        if(dummy(2:14)=='Alpha Singles') then
             i=0
  160       read(10,'(a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')du1,b,du2,c,du3,d,du4,e,r              
            do while(du1.ne.' Bet') 
    write(*,'(a2,a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')'AS',du1,b,du2,c,du3,d,du4,e,r
    goto 160
    end do                     

        elseif (dummy(2:14)=='Beta  Singles') then
  170       read(10,'(a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')du1,b,du2,c,du3,d,du4,e,r
            if((du1=='END'))then
              stop
        else      

     write(*,'(a2,a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')'BS',du1,b,du2,c,du3,d,du4,e,r             
            goto 170      
    end if
    end if
        goto 100      

   200    print*,'This is the end' 

  end program test_read

你的程序永远不会退出检查Beta版的循环,因为当你的while循环退出时,它已经读取了Beta版的行。然后它转到100,在测试版之后的下一行,所以你永远不会看到测试版的单曲。试试下面的方法

character(len=2):: tag
read(10,'(a100)')dummy

do while (dummy(1:3).ne.'END')
    if (dummy(2:14)=='Alpha Singles') then
       tag = 'AS'
    else if (dummy(2:14)=='Beta  Singles') then
       tag = 'BS'
    else
       read(dummy,'(a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')du1,b,du2,c,du3,d,du4,e,r              
       write(*,'(a2,a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')tag,du1,b,du2,c,du3,d,du4,e,r
    end if
    read(10, '(a100)') dummy
 end do

 print*,'This is the end'

像那样跳来跳去是非常糟糕的做法。没有理由在fortran中使用goto——正如您所看到的那样,它会让调试非常痛苦。