C++高手这语句哪错了?

来源:百度知道 编辑:UC知道 时间:2024/05/26 12:30:04
for (x1=0.05;x1<=2.55;x1=x1+0.01)
{
for (x2=0.05;x2<=2.55;x2=x2+0.01)
{
for (x3=10;x3<=240;x3++)
{
for (x4=10;x4<=240;x4++)
{
if ( g1(x1,x3) && g2(x2,x3) && g3(x3,x4) && g4(x4) )
{
fx=(0.6224*x1*x3*x4)+(1.7781*x2*x3*x3)+(3.1661*x1*x1*x4)+(19.84*x1*x1*x3);
if (min_fx==0)
{min_fx=fx;}
else {
if (fx<=min_fx)
min_fx=fx;
ct=x1;
ht=x2;
ir=x3;
cl=x4;
}//cout<<fx<<endl;

}
}
}
}
}

cout<<"minimun fx="<<min_fx<<endl;
cout << "cylinder thickness= " << ct << endl;
cout << "head thickness= &qu

这是一个不全的代码,不知你的main()函数和调用该字段的函数是怎么写的,还有X1~X4~X4,MIN_FX和fx这些变量是怎么初始化的.(在for循环中的X1~X4只是赋值,应写成flout X1=0.05, int X3=10等)
就目前看,这个代码有如下不妥之处:
1)你的第一个if语句if ( g1(x1,x3) && g2(x2,x3) && g3(x3,x4) && g4(x4) ){没有相应的else语句,一旦出现异常,即 g1(x1,x3) && g2(x2,x3) && g3(x3,x4) && g4(x4)的值为false时,该if条件句中的所有语句都将不被执行,当然X1~X4不能被正确赋值.
应当加上一个else语句处理该异常,如
else{
cout<<"minus error"<<min_fx<<endl;
}
2)第二个if
if (min_fx==0)
{min_fx=fx;}
缺少后续处理机制:该if中也应有赋值语句ct=x1; ht=x2;ir=x3;cl=x4;
否则一旦min_fx==0成立,且此时已循环执行到最后一层,赋值将没有机会被完成.
应加上赋值语句:
if (min_fx==0)
{min_fx=fx;
ct=x1;
ht=x2;
ir=x3;
cl=x4;
}

你的min_fx在上面赋初值了没有?没有的话if(fx<min_fx)句就有问题了

太JB长了
JB太长了
受不了