Решение квадратичного уравнения на C ++ - неправильные мнимые решения

person1337 спросил: 13 июня 2018 в 11:32 в: c++

Как гласит название, всякий раз, когда я ввожу определенные числа в решатель квадратного уравнения, которые приводят к мнимым решениям, я не смог выразить правильные мнимые решения.

Некоторые вещи, которые я пробовал, включали создание новой литой переменной, которая в основном была абсолютной величиной дискриминанта, но как совершенно новая переменная.

Кроме того, я попытался использовать дискриминант сама переменная, как с функцией 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;    }}

}

1 ответ

Есть решение
Thomas ответил: 13 июня 2018 в 01:48

Попробуйте это. Проблема была в воображаемой математике. Вы добавляли / вычитали реальную и мнимую части. Вы не можете этого сделать. Я повторно использовал две переменные в воображаемой секции. 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;        }    }
}