// **********************************************************************

// * Project 1                                                          *

// *                                                                    *

// * This program will find the solutions to quadratic equations given  *

// * integer coefficients                                               *

// *                                                                    *

// * Author: Matthew Fricke                                             *

// * Date: June 8th, 2003                                               *

// *                                                                    *

// **********************************************************************

 

// include iostream for input and output

#include <iostream>

 

// cmath for the square root function sqrt()

#include <cmath>

 

using namespace std;

 

int main()

{

      // Declare the variables to be used

      int a = 0, b = 0, c = 0;

      float discriminant = 0.0, real = 0.0, imaginary = 0.0; 

 

      // Print the output common to all cases

      cout << "SOLVING QUADRATIC EQUATIONS" << endl;

      cout << "For an equation of the form ax^2 + bx + c = 0" << endl;

 

      // Ask the user for input

      cout << "enter the coefficients a, b, c, (separated by spaces): ";

 

      // recieve input from the user

      cin >> a >> b >> c;

 

      // Find the solutions and print the appropriate text

 

      // Calculate the discriminant

      discriminant = b*b - 4*a*c;

 

      // Figure out which case we have

 

      // is the discriminant positive 

      if ( discriminant > 0 )

      {

            // Solutions must be rational or irrational

 

            // yes its a perfect square so the solutions must be rational

            cout << "There are two";

 

            // is the discriminant a perfect square and rational?

            if ( floor(sqrt(discriminant)) == sqrt(discriminant) )

            {

                  cout << " rational ";

            }

            else // it must be irrational

            {

                  cout << " irrational ";

            }

 

            cout << "solutions to the quadratic equation\n";

 

            // Print the equation

            cout << a << "x^2 + " << b << "x" << " + " << c << endl;

 

            // Print the solutions

            cout << "They are:";

            cout << (-b - sqrt( b*b - 4*a*c ))/(2*a) << " and ";

            cout << (-b + sqrt( b*b - 4*a*c ))/(2*a) << endl;

 

      }

      // or zero

      else if ( discriminant == 0 )

      {

            cout << "There is one real solution to the equation"<<endl;

 

            // Print the equation

            cout << a << "x^2 + " << b << "x" << " + " << c << endl;

 

            // Print the solutions

            cout << "It is";

            cout << (-b - sqrt( b*b - 4*a*c ))/(2*a) << endl;

      }

      // or negative

      else

      {

            // The solutions must be complex numbers

 

            // separate the quadratic formula into the real and imaginary part

 

            // real

            real = (-b)/(2.0*a);

           

            // imaginary part with the sqrt(-1) factored out

            imaginary = sqrt( -(b*b - 4*a*c) )/(2*a);

 

            cout << "There are two complex solutions to the equation"<<endl;

 

            // Print the equation

            cout << a << "x^2 + " << b << "x" << " + " << c << endl;

 

            // Print the solutions

            cout << "They are: "<<endl;

            cout << real << " - " << imaginary << "i" << " and ";

            cout << real << " + " << imaginary << "i" << endl;

      }

 

// Tell the OS that the program is exiting without any errors

      return 0;

}