请教下面C++程式错在什么地方?
获取满足条件公式的x1,y1,z1的值,其中x1,y1,z1必须为两位小数:
我用dev-cpp,下面的程序错在什么地方?谢谢!
----------------------
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
float x1,y1,z1;
int x,y,z;
do {
for (x=0;x <=100;++x)
{ x1==float(x*0.01);
// cout < < x1; //测试用
//system( "PAUSE ");
for (y=1;y <=100;++y) {y1==y*0.01;}
for (z=1;z <=100;++z) {z1==z*0.01;}
}
}while( 58*x1+68*y1+78*z1==128) ;
cout < < x1 < < endl < < y1 < < endl < < z1 < < endl;
cout < < "结果= " < <58*x1+68*y1+78*z1;
cin.get();
return EXIT_SUCCESS;
}
[解决办法]
x1==float(x*0.01);
应为
x1=float(x*0.01);
[解决办法]
方法好像不对,结果永远不可能是两位小数,结果也是不正确的
[解决办法]
程序的逻辑不对吧
for (y=1;y <=100;++y) {y1==y*0.01;}
for (z=1;z <=100;++z) {z1==z*0.01;}
这两句就是什么都干循环了100遍而已
[解决办法]
你想做什么?
for (y=1;y <=100;++y) {y1==y*0.01;}
for (z=1;z <=100;++z) {z1==z*0.01;}
路过!
[解决办法]
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
float x1,y1,z1;
int x,y,z;
for (x=0;x <=100;++x)
{
x1=float(x*0.01);
for (y=1;y <=100;++y)
{
y1=y*0.01;
for (z=1;z <=100;++z)
{
z1=z*0.01;
if (((58*x1+68*y1+78*z1-128) <0.001) && ((58*x1+68*y1+78*z1-128)> -0.001))
{
cout < < x1 < < endl < < y1 < < endl < < z1 < < endl;
cout < < "结果= " < <58*x1+68*y1+78*z1 < <endl;;
cout < < "----------------------------------- " < <endl;
}
}
}
}
//cin.get();
return EXIT_SUCCESS;
}
[解决办法]
唉,怎么显示成这样。
程序逻辑不对,判断应该在嵌套循环的最内层。比较表达式也不对,浮点数不能直接用==比较。
[解决办法]
楼上正确,浮点数不能用==进行比较