/**********************************************************
* Define a new data type to represent a quadratic equation*
***********************************************************/
struct Quadratic_Equation
{
int a;
int b;
int c;
int discriminant; //b^2 - 4ac
};
/*****************************
* Function declarations *
******************************/
//Read three integers from cin and store them in e
void get_quadratic_equation(Quadratic_Equation& e);
//Return true, if equation e is found in array V
//V stores n quadratic equations
bool find_equation(const Quadratic_Equation V[], const Quadratic_Equation& e, int n);
//Read all equations from cin
//Equation with one root are stored in V1 and k1 is the number of equations stored in V1
//Equation with two roots are stored in V2 and k2 is the number of equations stored in V2
//Equation with complex roots are stored in Vc and kc is the number of equations stored in Vc
//At most n equation can be stored in the arrays
//No repeated equations are store in the arrays
void read_equations(Quadratic_Equation V1[], int& k1,
Quadratic_Equation V2[], int& k2,
Quadratic_Equation Vc[], int& kc, int n);
//Return true, if equation e1 should be displayed before equation e2
bool less_than(const Quadratic_Equation& e1, const Quadratic_Equation& e2);
void sort_equations(Quadratic_Equation V[], int n);
//Display quadratic equation e to cout
//e's roots are also displayed
void display_equation(const Quadratic_Equation& e);
//Display to cout all quadratic equations stored in array V
//V stores n quadratic equations
void display_equations(const Quadratic_Equation V[], int n);
/******************************
* MAIN *
******************************/
int main()
{
const int SIZE = 100;
Quadratic_Equation two_roots[SIZE];
int howMany_two = 0;
Quadratic_Equation one_root[SIZE];
int howMany_one = 0;
Quadratic_Equation complex_roots[SIZE];
int howMany_complex = 0;
cout << "** Enter Quadratic Equations **" << endl;
/*************************************
* Function definitions *
**************************************/
//Read three integers from cin and store them in e
void get_quadratic_equation(Quadratic_Equation& e)
{
cin >> e.a >> e.b >> e.c;
//Calculate discriminant
e. discriminant = e.b*e.b - 4*e.a*e.c;
}
//Return true, if equation e is found in array V
//V stores n quadratic equations
bool find_equation(const Quadratic_Equation V[], const Quadratic_Equation& e, int n)
{
for(int i = 0; i < n; ++i)
{
if (e.a == V[i].a && e.b == V[i].b && e.c == V[i].c)
return true;
}
return false;
}
//Read all equations from cin
//Equation with one root are stored in V1 and k1 is the number of equations stored in V1
//Equation with two roots are stored in V2 and k2 is the number of equations stored in V2
//Equation with complex roots are stored in Vc and kc is the number of equations stored in Vc
//At most n equation can be stored in the arrays
//No repeated equations are store in the arrays
void read_equations(Quadratic_Equation V1[], int& k1,
Quadratic_Equation V2[], int& k2,
Quadratic_Equation Vc[], int& kc, int n)
{
Quadratic_Equation e;
k1 = k2 = kc = 0;
get_quadratic_equation(e); //read first equation
while ( e.a != STOP )
{
if ( e.discriminant == 0 ) //e has one root
{
if ( !find_equation(V1, e, k1) ) //test if user gave a repeated equation
{
V1[k1] = e;
++k1;
}
}
else if ( e.discriminant > 0 ) //e has two roots
{
if ( !find_equation(V2, e, k2) ) //test if user gave a repeated equation
{
V2[k2] = e;
++k2;
}
}
else //e has complex roots
{
if ( !find_equation(Vc, e, kc) ) //test if user gave a repeated equation
{
Vc[kc] = e;
++kc;
}
}
if ( (k1+k2+kc) == n ) break; //at most n equations can be read
get_quadratic_equation(e); //read next equation
}
}
//Return true, if equation e1 should be displayed before equation e2
bool less_than(const Quadratic_Equation& e1, const Quadratic_Equation& e2)
{
if ( e1.a < e2.a )
return true;
else if ( (e1.a == e2.a) && (e1.b < e2.b) )
return true;
else if ( (e1.a == e2.a) && (e1.b == e2.b) && (e1.c < e2.c) )
return true;
return false;
}
void sort_equations(Quadratic_Equation V[], int n)
{
for ( int pass = 1; pass <= n - 1; ++pass ) // passes
for (int i = 0; i < n - 1; ++i ) // one pass
{
if ( less_than(V[i+1], V[i]) ) // compare a pair of adjacent equations
{
swap(V[i+1], V[i]);
}
}
}
//Display quadratic equation e to cout
//e's roots are also displayed
void display_equation(const Quadratic_Equation& e)
{
cout << e.a << "x^2";
//Display to cout all quadratic equations stored in array V
//V stores n quadratic equations
void display_equations(const Quadratic_Equation V[], int n)
{
for(int i = 0; i < n; ++i)
{
display_equation(V[i]);