简体   繁体   English

在C#中实现此Fortran代码段的最佳方法是什么

[英]What is the best way to implement this Fortran snippet in C#

I have a Fortran routine which reads data from a file like this: 我有一个Fortran例程,该例程从这样的文件读取数据:

10   READ(X,*,ERR=8000,END=9000) ... Read header line of data sequence
C    Some processing of header line data...
     READ(X,*,ERR=8000) ... Read secondary line of data sequence
C    Some processing of secondary line data...
20   READ(X,*,ERR=8000) ... Read data line
     IF data contains 'X' GOTO 10
C    Do some calculations with the data ...
     IF (X.LT.Y)
         GOTO 10
C    Do some more calculations ...
100  CONTINUE
8000 'Output some error to the log'
9000 'Some final stuff'
     RETURN

The original code is a lot longer than this, but this is the gist. 原始代码比这要长得多,但这是要点。 I am thinking that C# code like below should do the same thing (coding from memory, so might be some errors...), but for some reason this seems enormously complicated to achieve the same result. 我认为像下面的C#代码应该做同样的事情(从内存中进行编码,因此可能会出现一些错误...),但是出于某种原因,要获得相同的结果,这似乎非常复杂。 Is there a simple way of replicating the flow of the Fortran routine? 有没有简单的方法可以复制Fortran例程的流程? Is it just a case where using gotos provides shorter code than using code blocks? 是否只是使用gotos提供比使用代码块短的代码的情况?

private void MyFunction(Stream MyData)
{
    string s = string.Empty;
    bool flag;
    StreamReader sr = new StreamReader(MyData);
    try
    {
        while (!sr.EndOFStream)
        {
            s = sr.ReadLine(); ... Read header line of data sequence
            //Some processing of header line data ...
            s = sr.Readline(); ... Read secondary line of data sequence
            //Some processing of secondary line data ...
            flag = false;
            while (!(s = sr.ReadLine()).Contains("X"))
            {
                //Do some calculations with the data ...
                if (X < Y)
                {
                    flag = true;
                    break;
                }
                //Do some more calculations ...
            }
            if (flag) continue;
        }
        //Some final stuff ...
        return;
    }
    catch
    {
         //Output error to log...
    }
}

Certainly the goto statements can be avoided. 当然,可以避免使用goto语句。

It seems to me though, your C# sample is not doing the same thing as the Fortran snippet (at least I think so). 不过在我看来,您的C#示例与Fortran代码段的功能不同(至少我认为是这样)。 I don't know C# but here is a Fortran version without gotos. 我不知道C#,但这是一个没有gotos的Fortran版本。 It should be equivalent to the other version with one exception: I did not include the I/O error checks. 它应该与其他版本等效,但有一个例外:我没有包括I / O错误检查。

 readloop : do while(.true.)
    read(X,*,iostat=stat) ! ... Read header line of data sequence
    if (stat /= 0) exit
    ! Some processing of header line data...
    read(X,*)             ! ... Read secondary line of data sequence
    ! Some processing of secondary line data...
    read(X,*)             ! ... Read data line
    if (data contains 'X') cycle readloop
    ! Do some calculations with the data ...
    if (X >= Y) exit readloop
 end do
 ! Some final stuff

This should translate to C#-ish code as (I deduce the syntax from your code example): 这应该转换为C#-ish代码,如下所示(我从您的代码示例中推断出语法):

 while (!sr.EndOfStream) {
   s = sr.ReadLine();
   // process
   s = sr.ReadLine();
   // process
   s = sr.ReadLine();
   if (s.Contains("X")) continue;
   // calculations
   if (X >= Y) break; 
 }
 // final processing

and it should be simple to include an error check here, using the try...catch construct. 并且使用try ... catch构造在此处包含错误检查应该很简单。

When reading the third line, you may read a line multiple times. 读取第三行时,您可能会多次读取一行。

I tend to avoid assigning a variable in a test. 我倾向于避免在测试中分配变量。 That makes the code hard to read. 这使得代码难以阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM