简体   繁体   English

C ++二次方程式求解器-错误的虚解

[英]C++ Quadratic Equation Solver - Incorrect Imaginary Solutions

As the title says, whenever I input certain numbers in the quadratic equation solver that lead to imaginary solutions, I have been unable to express the proper imaginary solutions. 就像标题所说,每当我在二次方程求解器中输入导致虚解的某些数字时,我就无法表达适当的虚解。

Some things that I tried included creating a new casted variable that was basically the absolute value of the discriminant, but as a totally new variable. 我尝试过的一些事情包括创建一个新的强制转换变量,该变量基本上是判别式的绝对值,但它是一个全新的变量。

In addition, I tried using the discriminant variable itself, both with the abs() function and negative times the absolute value. 另外,我尝试使用判别变量本身,同时使用abs()函数和负值乘以绝对值。

I appreciate any input. 感谢您的投入。 In case you guys need it, here is my code. 如果你们需要它,这是我的代码。

Edit: In case you wondered some of the values I inputted, I inputted 2, -5, and 4 as A, B, and C. The value, if one were to check on calculator/quadratic solver, would be around 1.25 +- 0.66I, but I got 3.00*I and -0.5*I. 编辑:如果您想知道我输入的某些值,则我将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;




    }




}

} }

Try this. 尝试这个。 You problem was in the imaginary math. 您的问题在于假想数学。 You were adding/subtracting the real and imaginary parts. 您正在添加/减去实部和虚部。 You can't do that. 你不能那样做。 I reused your two variables in the imaginary section. 我在虚构部分重用了您的两个变量。 imag_solution_1 is now the real part and imag_solution_2 is now the imaginary part. 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;

        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM