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

发布于 2024-10-02 06:43:35 字数 1420 浏览 4 评论 0原文

我在这里做了一些更改,但仍然没有得到我期望的结果。例如,当我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虐人心 2024-10-09 06:43:35

您错过了 else if(disc <0.0) 中的大括号,因此下一个 else 是孤立的

You missed braces from else if(disc <0.0) hence the next else is orphaned

奶茶白久 2024-10-09 06:43:35

在第一次测试 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

At the first test for if (disc == 0.0 && b == 0.0), the variable disc is uninitialized and hence could have any value whatsoever. You probably intended to write if (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:

#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;
}

Sample outputs:

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
朦胧时间 2024-10-09 06:43:35
#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 < 0.0)

另外,第一个测试中的 Disc 不是 init ...

而且我还建议始终将 {} 与 if else 一起使用。即使不写入它可以节省 5 秒,调试任何更改也会损失 1/2 小时。

相比之下

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;

}

#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;
}

here is your code correctly indented (30 sec under notepad++)

and it becomes obvious (as posted by vinothkr) that your missing the braces

else if(disc < 0.0)

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

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;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文