Fortran 77到C++的翻译——它的工作原理

Translation of Fortran 77 to C++ -How it works?

本文关键字:工作 翻译 C++ Fortran      更新时间:2023-10-16

我正试图将下面的fortran 77子程序复制到c++中。

     subroutine calcul(nb, m, sd, x_min, n, y_w)
     common / speed_1 / v_0
     common / dat1 / x_m
     common / dat2 / x_l0, x_r0, x_lq, e_l, e_r, a1, a2, b1 
     common / sor_arr / x(6500), y(6500), z(6500)
     common / ef_mat / x_f(35000), y_f(35000)
     y_m = y_w - rl_h
     y_ma = rl_h
     y_mi = -rl_h
     x_ma = rb_h
     x_mi = -rb_h
     do 10 i = m, 1, -1
     y_y = z(i) - y_m
     if (y_y.gt.y_ma) goto 10
     if (y_y.le.y_mi) goto 10
     c1_s = y(i) / v_0
     x_l = x_l0 + e_l*c1_s**a1
     x_r = x_r0 + e_r*c1_s**a2
     cl_r = x_r - x_l
     c2_s = x(i) + x_min
    i1 = 1
    i2 = nb
    i3 = 1
    if (mod(i, 2).eq. 0) goto 20
    i1 = nb
    i2 = 1
    i3 = -1
 20 do 30 i = i1, i2, i3
    r_m1 = float(i - 1)
    x_q = c2_s + r_m1*sd - x_l
    x_q = x_q * b1 / c1_r + xl_q
    x_x = x_q - x_m
    if (x_x.gt.x_ma) goto 30
    if (x_x.le.x_mi) goto 30
    n = n + 1
    y_f(n) = y_y
    x_f(n) = x_x
 30 continue
 10 continue
    return
    end  

这是我的翻译

 #include "stdafx.h"
 #include <iostream>

 using namespace std;


 void calcul(int& nb, int& m, double& sd, double& x_min, int& n, double& y_w);

struct speed
{
   double v_0;
};
struct dat1
{
double x_m;
};
struct dat2
{
double x_l0, x_r0, x_lq, e_l, e_r, rl_h, rb_h, a1, a2, b1;
};

struct sor_arr
{
double x[6500], y[6500], z[6500];
};

struct ef_mat
{
double x_f[35000], y_f[35000];
};


int main()
{
int nb1 = 10;
int m1 = 250;
double sd1 = 16;
double x1nmin = 1.e38;
int n1 = 0;
double yw1 = 0;


calcul(nb1, m1, sd1, x1nmin, n1, yw1);

system("PAUSE");
return 0;
}
void calcul(int& nb, int& m, double& sd, double& x_min, int& n, double& y_w)
{
struct speed sp;
struct dat1 d1;
struct dat2 d2;
struct sor_arr s;
struct ef_mat ef;
int i1;
int i2;
int i3;

double y_m;
double y_ma;
double y_mi;
double x_ma;
double x_mi;
double y_y;
double x_x;
double c1_s;
double x_l;
double x_r;
double cl_r;
double c2_s;
double r_m1;
double x_q;


y_m = y_w - d2.rl_h;
y_ma = d2.rl_h;
y_mi = -d2.rl_h;
x_ma = d2.rb_h;
x_mi = -d2.rb_h;
for (int i = m; i > 1;  i--)
{
    y_y = s.z[i] - y_m;
    if (y_y > y_ma)
    {
        continue;
    }
    if (y_y <= y_ma)
    {
        continue;
    }
    c1_s = s.y[i] / sp.v_0;
    x_l = d2.x_l0 + d2.e_l * pow(c1_s, d2.a1);
    x_r = d2.x_r0 + d2.e_r * pow(c1_s, d2.a2);
    cl_r = x_r - x_l;
    c2_s = s.x[i] + x_min;
    i1 = 1;
    i2 = nb;
    i3 = 1;
    if (fmod(i,2)== 0)
    {

        for (int i = i1; i < i2; i+= i3)
        {
            r_m1 = float(i - 1);
            x_q = c2_s + r_m1 * sd - x_l;
            x_q = x_q * d2.b1 / cl_r + d2.x_lq;
            x_x = x_q - d1.x_m;

            if (x_x >= x_ma)
            {
                continue;
            }
            if (x_x < x_ma)
            {
                continue;
            }
            n = n + 1;
            ef.y_f[n] = y_y;
            ef.x_f[n] = x_x;

        }
        i1 = nb;
        i2 = 1;
        i3 = -1;
    }

   }
}

错误如下。有人能帮我,告诉我怎么修吗?

error C4700: uninitialized local variable 'sp' used
error C4700: uninitialized local variable 'd2' used
error C4700: uninitialized local variable 'd1' used

这有点疯狂和大胆,但可以尝试的是初始化本地变量。

例如:

struct speed sp = {0};
struct dat1 d1 = {0};
struct dat2 d2 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

如果你真的疯了,你甚至可以考虑使用0以外的其他值-在这个算法中真正让有意义的东西将非常棒!!!

注意:我对Fortran一无所知,也没有深入研究您的代码中的其他问题。