C++ 二次方程求解器 - 不正确的虚数解

标签 c++ c++11 computer-science program-structure

正如标题所说,每当我在二次方程求解器中输入某些导致虚解的数字时,我一直无法表达正确的虚解。

我尝试过的一些事情包括创建一个新的转换变量,它基本上是判别式的绝对值,但作为一个全新的变量。

此外,我尝试使用判别变量本身,同时使用 abs() 函数和负数乘以绝对值。

我感谢任何意见。如果你们需要,这是我的代码。

编辑:如果您想知道我输入的某些值,我输入了 2、-5 和 4 作为 A、B 和 C。如果要检查计算器/二次求解器,该值将在附近1.25 +- 0.66I,但我得到了 3.00*I 和 -0.5*I。

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

int main() {

    cout << fixed << scientific << setprecision(5);
    cout << setfill('*') << setw(61) << '*' << endl;
    cout << "5Chan Christina Quadratic Equation Solver" << endl;
    cout << setfill('*') << setw(61) << '*' << endl;

    cout<< "Welcome to Quadratic Equation Solver, where we will input solutions from your inputs based on the form" << endl;
    cout << setw(31) << "Ax^2 + Bx + C = 0" << endl;
    cout << "Where A, B, and C are integers, and A is not equal to zero." << endl;
    cout << setfill('*') << setw(61) << '*' << endl;

    int A, B, C;


// discriminant program



// The following equations represent both
//parts of the quadratic equation, converted to double form


    cout << "Enter the input for A. " << endl;
    cin >> A;

    if (A == 0)
/* Here, when A == 0
The following statements are executed under such subconditions
*/
    {
        cout << "Warning, any more 0 input will not produce any equation!" << endl;
        cout << "Enter the input for B. " << endl;
        cin >> B;
        if (B == 0)
        {
            cout << "Invalid! Start over! You do not have an algebraic equation!" << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
            cout << "Thank you for using Quadratic Equation Solver!" << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
        }

        // tfw your input results in a linear equation...
        else if (B != 0)
        {
            cout << "Enter the input for C. " << endl;
            cin >> C;
            double lin_eq = static_cast<double>(-C)/B; 
            // linear equation form when b is NOT 0
            cout << "Solution is " << endl;
            cout << setw(50) << lin_eq << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
            cout << "Thank you for using Quadratic Equation Solver!" << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
        }
    }

else //basically, when A is NOT zero
{

    cout << "Enter the input for B. " << endl;
    cin >> B;   
    cout << "Enter the input for C. " << endl;
    cin >> C;

    double disc = static_cast<double>((B*B)-(4*A*C));
    double quad_1 = static_cast<double>((-B) + sqrt((B*B) - (4*A*C)))/(2*A);
    double quad_2 = static_cast<double>((-B) - sqrt((B*B) - (4*A*C)))/(2*A);


    if (disc > 0) // discriminant greater than 0
    // 2 solutions are printed 
    {
        cout << "The two real solutions are" << setw(31) << quad_1 << endl;
        cout << setw(20) << "and" << setw(51) << quad_2;
        cout << setfill('*') << setw(61) << '*' << endl;
        cout << "Thank you for using Quadratic Equation Solver!" << endl;
        cout << setfill('*') << setw(61) << '*' << endl;
    }

    else if (disc == 0) 
    {
            cout << "Solution is " << endl;
            cout << setw(50) << quad_2 << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
            cout << "Thank you for using 5Chan Christina Quadratic Equation Solver!" << endl;
            cout << setfill('*') << setw(61) << '*' << endl;



    }       

    else if (disc < 0) // This segment of code 
    // Prints imaginary values through discriminant abs value
    {

        double imag_help =  static_cast<double>(abs(disc));
      double imag_solution_1 = ((-B+imag_help)/(2*A));
      double imag_solution_2 = ((-B-imag_help)/(2*A));
      cout << "The two imaginary solutions ARE" << setw(31) << "x= " << imag_solution_1 << "*I" << endl;
      cout<< setw(15) << " AND "  << setw(31) << "x=" << imag_solution_2 << "*I" << endl;
      cout << setfill('*') << setw(61) << endl;
      cout << "Thank you for using Quadratic Equation Solver." << endl;
      cout << setfill('*') << setw(61) << endl;




    }




}

最佳答案

试试这个。你的问题是虚数。您正在添加/减去实部和虚部。你不能那样做。我在虚部中重复使用了你的两个变量。 imag_solution_1 现在是实部,imag_solution_2 现在是虚部。

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

int main() {

    cout << fixed << scientific << setprecision(5);
    cout << setfill('*') << setw(61) << '*' << endl;
    cout << "5Chan Christina Quadratic Equation Solver" << endl;
    cout << setfill('*') << setw(61) << '*' << endl;

    cout << "Welcome to Quadratic Equation Solver, where we will input solutions from your inputs based on the form" << endl;
    cout << setw(31) << "Ax^2 + Bx + C = 0" << endl;
    cout << "Where A, B, and C are integers, and A is not equal to zero." << endl;
    cout << setfill('*') << setw(61) << '*' << endl;

    int A, B, C;


    // discriminant program



    // The following equations represent both
    //parts of the quadratic equation, converted to double form


    cout << "Enter the input for A. " << endl;
    cin >> A;

    if (A == 0)
        /* Here, when A == 0
        The following statements are executed under such subconditions
        */
    {
        cout << "Warning, any more 0 input will not produce any equation!" << endl;
        cout << "Enter the input for B. " << endl;
        cin >> B;
        if (B == 0)
        {
            cout << "Invalid! Start over! You do not have an algebraic equation!" << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
            cout << "Thank you for using Quadratic Equation Solver!" << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
        }

        // tfw your input results in a linear equation...
        else if (B != 0)
        {
            cout << "Enter the input for C. " << endl;
            cin >> C;
            double lin_eq = static_cast<double>(-C) / B;
            // linear equation form when b is NOT 0
            cout << "Solution is " << endl;
            cout << setw(50) << lin_eq << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
            cout << "Thank you for using Quadratic Equation Solver!" << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
        }
    }

    else //basically, when A is NOT zero
    {

        cout << "Enter the input for B. " << endl;
        cin >> B;
        cout << "Enter the input for C. " << endl;
        cin >> C;

        double disc = static_cast<double>((B*B) - (4.0 * A*C));
        double quad_1 = static_cast<double>((-B) + sqrt((B*B) - (4.0 * A*C))) / (2.0 * A);
        double quad_2 = static_cast<double>((-B) - sqrt((B*B) - (4.0 * A*C))) / (2.0 * A);


        if (disc > 0) // discriminant greater than 0
                      // 2 solutions are printed 
        {
            cout << "The two real solutions are" << setw(31) << quad_1 << endl;
            cout << setw(20) << "and" << setw(51) << quad_2;
            cout << setfill('*') << setw(61) << '*' << endl;
            cout << "Thank you for using Quadratic Equation Solver!" << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
        }

        else if (disc == 0)
        {
            cout << "Solution is " << endl;
            cout << setw(50) << quad_2 << endl;
            cout << setfill('*') << setw(61) << '*' << endl;
            cout << "Thank you for using 5Chan Christina Quadratic Equation Solver!" << endl;
            cout << setfill('*') << setw(61) << '*' << endl;



        }
        else if (disc < 0) // This segment of code 
                           // Prints imaginary values through discriminant abs value
        {

            double imag_help = static_cast<double>(abs(disc));
            double imag_solution_1 = (-B / (2.0 * A));
            double imag_solution_2 = (sqrt(imag_help) / (2.0 * A));
            cout << "The two imaginary solutions ARE" << setw(31) << "x= " << imag_solution_1 << " + " << imag_solution_2 << "i" << endl;
            cout << setw(15) << " AND " << setw(31) << "x=" << imag_solution_1 << " - " << imag_solution_2 << "i" << endl;
            cout << setfill('*') << setw(61) << endl;
            cout << "Thank you for using Quadratic Equation Solver." << endl;
            cout << setfill('*') << setw(61) << endl;

        }

    }
}

关于C++ 二次方程求解器 - 不正确的虚数解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51228107/

相关文章:

C++错误不那么冗长

c++ - 使用 Visual Studio 2008 使用 C++ 连接到 MS Access 数据库

c++ - 无锁堆栈 - 这是 c++11 宽松原子的正确用法吗?可以证明吗?

algorithm - 我是否可以始终将仅可变算法转换为单赋值并且仍然有效?

c - 内存应该有效

c++ - Halide 在归一化互相关期间挂起

c++ - Lua 和 C++ 错误窗口

java - 链表中的节点类,特别是构造函数,并使用它来创建随机整数的链表

c++ - 如何修复 pimpl 实现中预期的主表达式编译错误?

c++ - 如何在 C++11 中为 OuterClass 制作复制粘贴友好的 typedef?