2016-02-25 68 views
2

我原本有3个方程:Pu,Pm & Pd。它运行良好。 一旦我引入了if声明,并根据循环迭代对3个等式进行变更,我会收到一个运行时错误。 任何帮助,将不胜感激。if语句运行时错误

提前欢呼。

#include <cmath> 
#include <iostream> 
#include <vector> 
#include <iomanip> 


int Rounding(double x) 
{ 
    int Integer = (int)x; 
    double Decimal = x - Integer; 

    if (Decimal > 0.49) 
    { 
     return (Integer + 1); 
    } 
    else 
    { 
     return Integer; 
    } 
} 

int main() 
{ 
double a = 0.1; 
double sigma = 0.01; 
int delta_t = 1; 
double M = -a * delta_t; 
double V = sigma * sigma * delta_t; 
double delta_r = sqrt(3 * V); 
int count; 

double PuValue; 
double PmValue; 
double PdValue; 

int j_max; 
int j_min; 

j_max = Rounding(-0.184/M); 
j_min = -j_max; 

std::vector<std::vector<double>> Pu((20), std::vector<double>(20)); 
std::vector<std::vector<double>> Pm((20), std::vector<double>(20)); 
std::vector<std::vector<double>> Pd((20), std::vector<double>(20)); 

std::cout << std::setprecision(10); 
for (int i = 0; i <= 2; i++) 
    { 
     count = 0; 
     for (int j = i; j >= -i; j--) 
      { 
       count = count + 1; 
       if (j = j_max) // Exhibit 1C 
       { 
        PuValue = 7.0/6.0 + (j * j * M * M + 3 * j * M)/2.0; 
        PmValue = -1.0/3.0 - j * j * M * M - 2 * j * M; 
        PdValue = 1.0/6.0 + (j * j * M * M + j * M)/2.0; 
       } 
       else if (j = j_min) // Exhibit 1B 
       { 
        PuValue = 1.0/6.0 + (j * j * M * M - j * M)/2.0; 
        PmValue = -1.0/3.0 - j * j * M * M + 2 * j * M; 
        PdValue = 7.0/6.0 + (j * j * M * M - 3 * j * M)/2.0; 
       } 
       else 
       { 
        PuValue = 1.0/6.0 + (j * j * M * M + j * M)/2.0; 
        PmValue = 2.0/3.0 - j * j * M * M; 
        PdValue = 1.0/6.0 + (j * j * M * M - j * M)/2.0; 
       } 
       Pu[count][i] = PuValue; 
       Pm[count][i] = PmValue; 
       Pd[count][i] = PdValue; 

       std::cout << Pu[count][i] << ", "; 
      } 
     std::cout << std::endl; 
    } 
return 0; 
} 

回答

2

您分配,而不是检查相等:在你的if语句j_maxj

if (j = j_max) 
// ^

else if (j = j_min) 
//  ^


变化if (j = j_max)if (j == j_max)
else if (j = j_min)else if (j == j_min)

+0

你真棒Andreas。干杯 – Fitzy

0

更正以下if条件检查和if检查

if(j=j_max) 

with 

if (j == j_max) 

你检查的平等不是分配的所有其他实例。

您的代码正在进入无限循环。