【问题标题】:How to nest another Go To command when I am already using one当我已经在使用一个 Go To 命令时,如何嵌套另一个 Go To 命令
【发布时间】:2021-12-01 12:40:11
【问题描述】:

这是我的工作代码。目前它读取我的事件文件的第一行 (file=1),然后读取我的站文件的第一行 (file=2) 并将它们写出,执行 go to 命令并返回并读取下一行Stations 文件,然后再次将其全部写出,然后遍历该文件,直到遍历完stations 文件的最后一行。

我需要做的基本上是再次“循环”整个事情,以便现在正在读取和写入事件文件的第二行 (file=1),然后对其中的所有行执行此操作事件文件。

我尝试使用另一个“转到”命令,但我的输出从未改变它与当前代码输出的内容。

有谁知道如何添加另一个 go to 命令以便它再次循环通过这些东西?

 program events

            implicit none

            character*40 aline
            character*40 bline

            open (1, file="event", status="old")
            open (2, file="stations", status="old")
            open (3, file="output", status="new")

            read(1, '(a40)',end=60) aline
    1       read(2, '(a40)',end=60) bline

            write(3,*) aline, bline
            go to 1

    60      stop

 end program events

【问题讨论】:

  • 欢迎您,我建议您使用tour。有些人认为所有go to 都错了,有些人则没有。但几乎每个人都认为go to 向后指向是错误的。它会导致复杂的意大利面条代码。

标签: fortran goto


【解决方案1】:

我会完全避免使用goto——在现代 Fortran 中很少有任何需要,虽然我远非狂热者,但它们通常出现在更难以阅读的代码中。所以这就是我要做的——基本上你有两个循环,我会写两个循环:

ijb@ijb-Latitude-5410:~/work/stack$ cat british_rail.f90 
Program events

  Use, Intrinsic :: iso_fortran_env, Only : eof => iostat_end

  Implicit None

  Integer :: io_status

  Character( Len = 40 ) :: aline
  Character( Len = 40 ) :: bline

  Open( 10, file="event"   , status="old" )
  Open( 20, file="stations", status="old" )
  Open( 30, file="output"  , status="new" )

  events_read: Do
     Read( 10, '(a40)', iostat = io_status) aline
     If( io_status == eof ) Exit events_read
     stations_read: Do
        Read( 20, '(a40)', iostat = io_status ) bline
        If( io_status == eof ) Exit stations_read
        Write( 30, * ) aline, bline
     End Do stations_read
     Rewind 20
  End Do events_read

End Program events
ijb@ijb-Latitude-5410:~/work/stack$ gfortran --version
GNU Fortran (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ijb@ijb-Latitude-5410:~/work/stack$ gfortran -Wall -Wextra -std=f2008 -fcheck=all -g british_rail.f90 
ijb@ijb-Latitude-5410:~/work/stack$ cat event 
Leaves on track
Car Stuck on Level Crossing
Wrong kind of snow
ijb@ijb-Latitude-5410:~/work/stack$ cat stations 
Leicester
Market Harborough
Bedford
St Pancras
ijb@ijb-Latitude-5410:~/work/stack$ cat output
cat: output: No such file or directory
ijb@ijb-Latitude-5410:~/work/stack$ ./a.out
ijb@ijb-Latitude-5410:~/work/stack$ cat output
 Leaves on track                         Leicester                               
 Leaves on track                         Market Harborough                       
 Leaves on track                         Bedford                                 
 Leaves on track                         St Pancras                              
 Car Stuck on Level Crossing             Leicester                               
 Car Stuck on Level Crossing             Market Harborough                       
 Car Stuck on Level Crossing             Bedford                                 
 Car Stuck on Level Crossing             St Pancras                              
 Wrong kind of snow                      Leicester                               
 Wrong kind of snow                      Market Harborough                       
 Wrong kind of snow                      Bedford                                 
 Wrong kind of snow                      St Pancras                              
ijb@ijb-Latitude-5410:~/work/stack$ 

【讨论】:

    【解决方案2】:

    将读取的 end= 从 2 更改为从 1 循环回读取。由于您已到达结尾,因此您需要倒回 2。

    50      read(1, '(a40)',end=60) aline
            rewind 2
    1       read(2, '(a40)',end=50) bline
    
            write(3,*) aline, bline
            go to 1
    
    60      stop
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-09
      • 2012-09-17
      • 1970-01-01
      • 2021-04-03
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多