c# - 在 C# 中实现此 Fortran 片段的最佳方法是什么

标签 c# fortran

我有一个 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

原始代码比这长很多,但这是要点。 我认为像下面这样的 C# 代码应该做同样的事情(从内存编码,所以可能会出现一些错误......),但出于某种原因,这似乎非常复杂以达到相同的结果。是否有一种简单的方法来复制 Fortran 例程的流程?这只是使用 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...
    }
}

最佳答案

当然可以避免 goto 语句。

但在我看来,您的 C# 示例与 Fortran 代码片段的功能不同(至少我是这么认为的)。我不懂 C#,但这是没有 goto 的 Fortran 版本。它应该等同于另一个版本,但有一个异常(exception):我没有包括 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

这应该转换为 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

在这里使用 try...catch 结构进行错误检查应该很简单。

关于c# - 在 C# 中实现此 Fortran 片段的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8410686/

相关文章:

c# - 是否存在用于从包含给定字符集和计数的输入字符串中查找最小子字符串的 O(n) 算法?

c# - Azure SQL 数据库连接问题 - 连接太多?

c# - Fortran90 到 C# 的转换问题

c - 我怎样才能找出哪个图书馆是给定对象的所在地?

c# - 我需要从 Web 服务中使用 Windows 身份验证和 asp.net 模拟来调用同一网站内的另一个 Web 服务

c# - 在 C# 中,如何根据名称向对象添加事件处理程序?

c# - 将多个程序集版本加载到多个 AppDomains

loops - 关于MPI并行循环的问题

fortran - Fortran 中变量和静态数组的生命周期

arrays - 创建给定数组的频率数组时出现问题