我在这里做了一些改变,但我仍然没有得到我期望得到的东西。例如,当我用 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;
}