Quadratic Solver

steventaal 0 Tallied Votes 247 Views Share

I have written this for my math class, as it saved me an accumulated time of three hours over a week of homework that involved the quads.
I a posting it here for anyone who wants to use, modify or otherwise use to go ahead and do so. Standard Quadratic Form applies here, Ax2 * Bx * C, and sometimes Ax2 * Bx = C, etc.

Thanks, And enjoy,

-Stev Täal

#include <iostream>
#include <windows.h>
#include <math.h>

using namespace std;

int main()
{
    system("title Quadratic Solver");
    system("color 1F");
    long double a, b, c, posans, negans;
    begin:
    cout << "Enter a: ";
    cin >> a;
    cout << "Enter b: " ;
    cin >> b;
    cout << "Enter c: ";
    cin >> c;
    cout << "b squared is " << b * b << ",\n4ac evaluates to " << (4 * (a) * (c)) << ",\nAnd the sqaure root of b squared - 4ac is " << sqrt((b * b) - 4 * (a * c)) << ".\n2a evaluates to " << 2 * a << ". Thus, we get:\n"; 
    //
    posans = ((b * -1) + sqrt((b * b) - (4 * (a) * (c)))) / (2 * a);
    /*
    -----------------------------------------------------------------------|
    */
    negans = ((b * -1) - sqrt((b * b) - (4 * (a) * (c)))) / (2 * a);    
    //    
    cout << "\n                   _____________\n";
    {
    cout << "As X =    " << (b * -1) << " +/- \\/" << (b * b) << " + " << (4 * (a) * (c));
}
    else
    {
    cout << "As X =    " << (b * -1) << " +/- \\/" << (b * b) << " + " << (4 * (a) * (c)) * -1 << "";
}    
    cout << "\n       ------------------------\n";
    cout << "                   " << 2 * a << endl << endl << endl;
    cout << "   X =          " << (b * -1) << " +/- " << sqrt((b * b) - (4 * (a) * (c)));
    cout << "\n       ------------------------\n";
    cout << "                   " << 2 * a << endl << endl << endl;
    cout << "\nX can be either " << posans << " (+) or " << negans << " (-)";
    cout << endl << "Again or exit? (A/E) ";
    while (!GetAsyncKeyState(0x41))
    {
          Sleep(10);
          if (GetAsyncKeyState(0x45))
          {
                                     return 0;
                                     }
          
          }
    system("cls");
    FreeConsole();
    AllocConsole();
    system("color 1F");
    system("title Quadratic Solver ");
    goto begin;
    system("pause>nul");
}
anuragcoder -6 Junior Poster in Training

says error:

expected primary expression before else
expected ; before else

i believe you have a problem with braces.

steventaal 0 Newbie Poster

I apologize, and I have updated the algor

steventaal 0 Newbie Poster

Try this instead, as I've updated the algorithms for solving to be more accurate (nesting issues before) and added new inserts:

#include <iostream>
#include <windows.h>
#include <math.h>

using namespace std;

int main()
{
    system("title Quadratic Solver - By Stephen Talarman");
    int i = 4; //replace to 6
    while (i > 0)
    {
          if (i == 0, 2, 4,6)
          {
                system("color 3A");
                cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t        Quad Solver";
    cout << "\n\t\t\t   By: Stephen Talarman";
    Sleep(250);
    system("cls");
 i--; 
             }
             if (i == 1, 3, 5) {
                  system("color B1");
                  cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t        Quad Solver";
    cout << "\n\t\t\t   By: Stephen Talarman";
    Sleep(250);
    system("cls");
 i--; 
                  }  
}
system("color 1F");
    long double a, b, c, posans, negans;
    begin:
    cout << "Enter a: ";
    cin >> a;
    cout << "Enter b: " ;
    cin >> b;
    cout << "Enter c: ";
    cin >> c;
    cout << "b squared is " << b * b << ",\n4ac evaluates to " << ((4) * (a) * (c)) << ",\nAnd the sqaure root of b squared - 4ac is " << sqrt((b * b) - 4 * (a * c)) << ".\n2a evaluates to " << 2 * a << ". Thus, we get:\n"; 
    //
    posans = ((b * -1) + sqrt((b * b) - ((4) * (a) * (c)))) / (2 * a);
    /*
    -----------------------------------------------------------------------|
    */
    negans = ((b * -1) - sqrt((b * b) - (4 * (a) * (c)))) / (2 * a); 
  
    //    
    cout << "\n                   ____________\n"; //fix sqrt() problems under the graphical radical
    if ( 4 * (a * c) > 0)
    {
    cout << "As X =    " << (b * -1) << " +/- \\/" << (b * b) << " + " << (4 * (a) * (c));
}
    else
    {
    cout << "As X =    " << (b * -1) << " +/- \\/" << (b * b) << " + " << (4 * (a) * (c)) * -1 << "";
}    
    cout << "\n       ------------------------\n";
    cout << "                   " << 2 * a << endl << endl << endl;
    cout << "   X =          " << (b * -1) << " +/- " << sqrt((b * b) - (4 * (a) * (c)));
    cout << "\n       ------------------------\n";
    cout << "                   " << 2 * a << endl << endl << endl;
    cout << "\n   X =             " << (b * -1) << "       " << sqrt((b * b) - (4 * (a) * (c))) << "         " << (b * -1) << "       " << sqrt((b * b) - (4 * (a) * (c)));
    cout << "\n                 ----- + -----  or  ----- - -----";
    cout << "\n                   " << 2 * a << "       " << 2 * a << "          " << 2 * a << "       " << 2 * a;
    cout << "\n\n                   " << (b * -1) + sqrt((b * b) - (4 * (a) * (c))) << "       " << (b * -1) - sqrt((b * b) - (4 * (a) * (c)));
    cout << "\n             =>  ----- or -----";
    cout << "\n                   " << 2 * a << "        " << 2 * a;
    cout << "\n\n----------------------------------------";
    cout << "\n   X =             " << posans << " (+) or " << negans << " (-)";
    cout << "\n----------------------------------------\n";
    cout << endl << "Again or exit? (A/E) ";
    while (!GetAsyncKeyState(0x41))
    {
          Sleep(10);
          if (GetAsyncKeyState(0x45))
          {
                                     return 0;
                                     }
          
          }
    system("cls");
    FreeConsole();
    AllocConsole();
    system("color 1F");
    system("title Quadratic Solver - By Stephen Talarman");
    goto begin;
    system("pause>nul");
}
anuragcoder -6 Junior Poster in Training

Nice work........
Appears a little too confusing but at the end
no errors....

Thanks a lot for the code.

steventaal 0 Newbie Poster

at which part are you referring to with the difficulties? and feel free to change it to your comforts.

anuragcoder -6 Junior Poster in Training

the symbols and all that. A layman who knows only how to read numbers might not
understand all that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.