这个c++有什么问题吗?代码?
我在这里做了一些更改,但仍然没有得到我期望的结果。例如,当我用 a 代替 1、b 代替 2、c 代替 2 时,我应该得到 -1+i 和 -1-i,但是当我运行代码时,它给出 -0.73205+i 和 -2.73205+i。我该如何解决这个问题?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double a, b, c, disc, x1, x2, root1, root2, imrt1, imrt2;
char i;
cout<<"Enter a, b and c ";
cin >> a >> b >> c ;
if(disc == 0.0 && b == 0.0)
cout<<"The equation is degenerate and has no real roots. \n";
else if(a == 0.0)
cout<<"The equation has one real root x = "<< -c/b <<endl;
else
{
disc = pow(b,2.0)-4*a*c;
if (disc > 0.0)
{
disc = sqrt(disc);
root1 = (-b+disc)/(2*a);
root2 = (-b-disc)/(2*a);
cout<<"The two real roots are "<<root1<<" and "<<root2<<endl;
}
else if(disc < 0.0)
disc = pow(b,2.0)+4*a*c;
disc = sqrt(disc);
imrt1 = (-b+disc)/(2*a);
imrt2 = (-b-disc)/(2*a);
cout<<"The two imaginary roots are "<<imrt1<<"+i"<<" and <<imrt2<<"+i"<<"\n";
else
cout<<"Both roots are equal to "<<-b/(2*a)<<endl;
}//End of compound statement for the outer else
system("PAUSE");
return 0;
}
I've made a few changes here but I'm still not getting what I expect to get. For example, when I substitute a for 1, b for 2 and c for 2, I should get -1+i and -1-i but when I run code it gives me -0.73205+i and - 2.73205+i. How do I fix this?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double a, b, c, disc, x1, x2, root1, root2, imrt1, imrt2;
char i;
cout<<"Enter a, b and c ";
cin >> a >> b >> c ;
if(disc == 0.0 && b == 0.0)
cout<<"The equation is degenerate and has no real roots. \n";
else if(a == 0.0)
cout<<"The equation has one real root x = "<< -c/b <<endl;
else
{
disc = pow(b,2.0)-4*a*c;
if (disc > 0.0)
{
disc = sqrt(disc);
root1 = (-b+disc)/(2*a);
root2 = (-b-disc)/(2*a);
cout<<"The two real roots are "<<root1<<" and "<<root2<<endl;
}
else if(disc < 0.0)
disc = pow(b,2.0)+4*a*c;
disc = sqrt(disc);
imrt1 = (-b+disc)/(2*a);
imrt2 = (-b-disc)/(2*a);
cout<<"The two imaginary roots are "<<imrt1<<"+i"<<" and <<imrt2<<"+i"<<"\n";
else
cout<<"Both roots are equal to "<<-b/(2*a)<<endl;
}//End of compound statement for the outer else
system("PAUSE");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您错过了 else if(disc <0.0) 中的大括号,因此下一个 else 是孤立的
You missed braces from else if(disc <0.0) hence the next else is orphaned
在第一次测试
if (disc == 0.0 && b == 0.0)
时,变量disc
未初始化,因此可以具有任何值。您可能打算编写if (a == 0.0 && b == 0.0)
...在想象的情况下,您的数学非常可疑。如果判别式为负,则需要“-b / 2a”的实部,虚部为“±√(b² - 4ac)/2a”。所以,也许您需要:
示例输出:
At the first test for
if (disc == 0.0 && b == 0.0)
, the variabledisc
is uninitialized and hence could have any value whatsoever. You probably intended to writeif (a == 0.0 && b == 0.0)
...Your mathematics in the imaginary case is more than a little suspect. If the discriminant is negative, then you need the real part of '-b / 2a' and the imaginary parts are '±√(b² - 4ac)/2a'. So, maybe you need:
Sample outputs:
这是您正确缩进的代码(在记事本++下30秒)
并且很明显(如vinothkr发布的)您缺少大括号
另外,第一个测试中的 Disc 不是 init ...
而且我还建议始终将 {} 与 if else 一起使用。即使不写入它可以节省 5 秒,调试任何更改也会损失 1/2 小时。
相比之下
}
here is your code correctly indented (30 sec under notepad++)
and it becomes obvious (as posted by vinothkr) that your missing the braces
Also Disc in the first test isn't init ...
And I would also recommend to always use {} with if else. Even if it saves 5 secs to not write it, you lose 1/2h debugging any changes.
in comparison
}