这个c ++代码有什么问题?

时间:2010-11-15 05:44:30

标签: c++ formula quadratic

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

3 个答案:

答案 0 :(得分:4)

你错过了来自else的大括号if(disc&lt; 0.0)因此下一个是孤儿

答案 1 :(得分:0)

if (disc == 0.0 && b == 0.0)的第一次测试中,变量disc未初始化,因此可能具有任何值。您可能打算写if (a == 0.0 && b == 0.0) ...


你想象中的数学不仅仅是一个小小的怀疑。如果判别式为负,则需要'-b / 2a'的实部和虚部为'±√(b² - 4ac)/ 2a'。所以,也许你需要:

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

using namespace std;

int main()
{
    double a, b, c;

    cout << "Enter a, b and c: ";
    cin  >> a >> b >> c;

    if (a == 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
    {
        double disc = pow(b,2.0)-4*a*c;
        if (disc > 0.0)
        {
            disc = sqrt(disc);
            double root1 = (-b+disc)/(2*a);
            double root2 = (-b-disc)/(2*a);
            cout << "The two real roots are " << root1 << " and " << root2 << endl;
        }
        else if (disc < 0.0)
        {
            double imag = sqrt(-disc)/(2*a);
            double real = (-b)/(2*a);
            cout << "The two complex roots are "
                 << "(" << real << "+" << imag << "i)" << " and "
                 << "(" << real << "-" << imag << "i)" << endl;
        }
        else
            cout << "Both roots are equal to " << -b/(2*a) << endl;
    }
    return 0;
}

示例输出:

Enter a, b and c: 2 6 3
The two real roots are -0.633975 and -2.36603

Enter a, b and c: 2 4 3
The two complex roots are (-1+0.707107i) and (-1-0.707107i)

Enter a, b and c: 3 6 3
Both roots are equal to -1

答案 2 :(得分:0)

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main(){
    double a, b, c, disc, x1, x2, root1, root2, imrt1, imrt2, disc2;
    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)
            disc2 =  pow(b,2.0)-4*a*c;
        disc2 = sqrt(disc2);
        imrt1 = (-b+disc2)/(2*a);
        imrt2 = (-b-disc2)/(2*a);
        cout<<"The two imaginary roots are "<<"i"<<imrt1<<" and "<<"i"<<imrt2<<"\n";

        else
            cout<<"Both roots are equal to "<<-b/(2*a)<<endl;
    }//End of compound statement for the outer else

    system("PAUSE");
    return 0;
}

这里是你的代码正确缩进(在记事本++下30秒)

很明显(由vinothkr发布)你错过了大括号

  

否则if(disc&lt; 0.0)

第一次测试中的Disc也不是init ...

我还建议总是使用{}和if else。即使它没有写入也节省了5秒,你将失去1 / 2h调试任何变化。

比较

int main(){
double a, b, c, disc, x1, x2, root1, root2, imrt1, imrt2, disc2;
char i;
cout<<"Enter a, b and c ";
cin >> a >> b >> c ;

    //Disc never init...
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){
        disc2 =  pow(b,2.0)-4*a*c;
        disc2 = sqrt(disc2);
        imrt1 = (-b+disc2)/(2*a);
        imrt2 = (-b-disc2)/(2*a);
        cout<<"The two imaginary roots are "<<"i"<<imrt1<<" and "<<"i"<<imrt2<<"\n";
    }else{
        cout<<"Both roots are equal to "<<-b/(2*a)<<endl;
    }
}//End of compound statement for the outer else

system("PAUSE");
return 0;

}