2015-12-01 91 views
-11

我想將以下fortran 77子例程複製到C++中。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 
+3

這是因爲你使用未初始化的局部變量... – BlackDwarf

+0

首先,它說「警告」。沒有錯誤。所以你應該能夠編譯和運行......然後,如果我真的試圖編譯你的代碼,它就會*提供錯誤。所以你應該做的第一件事是修復你的文章中的代碼,所以我們實際上可以重現*你的*問題。但是,真正讓我感興趣的是'未初始化的局部變量'sp'used'的哪一部分對理解有困難? – dingalapadum

+3

請不要污衊你的問題。 – JAL

回答

4

這有點瘋狂和大膽的,但你能嘗試是初始化您的局部變量。

例如:

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

如果你去真的瘋了,你甚至可以考慮使用其它的值大於0 - 這實際上使得意義在該算法將是真棒!

注意:我對Fortran沒有絲毫的線索,也沒有看到代碼的進一步問題。

+2

這將是激進的! –

相關問題