Using Class to solve the quadratic equation.

I am trying to combine the equation part of this with this and am not sure how to do it. The class seems to work and give part of the output I am looking for now I just need it to see the math part of the program.

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;
//******************************************************************
// class Quadratic declaration
//******************************************************************
class Quadratic
{
private:

double a, b, c;

public:

Quadratic();


void set(const double& cdblA, const double& cdblB, const double& cdblC);
const double& get_a() const;
const double& get_b() const;
const double& get_c() const;
const double evaluate(const int& x) ;
friend Quadratic operator+(const Quadratic& q1, const Quadratic& q2);
friend Quadratic operator*(const double& r, const Quadratic& q );
void display(const string& strPrompt);
};
//******************************************************************
// class Quadratic implementation
//******************************************************************
Quadratic::Quadratic() : a(0), b(0), c(0) {}

void Quadratic::set(const double& cdblA, const double& cdblB, const double& cdblC)

{

a = cdblA;

b = cdblB;

c = cdblC;

}
const double& Quadratic::get_a() const {return a;}
const double& Quadratic::get_b() const {return b;}
const double& Quadratic::get_c() const {return c;}
const double Quadratic::evaluate(const int& x)

{

return (a * x * x) + (b * x) + c;

}
Quadratic operator+(const Quadratic& q1, const Quadratic& q2)

{

double

a = q1.get_a() + q2.get_a(),

b = q1.get_b() + q2.get_b(),

c = q1.get_c() + q2.get_c();

Quadratic q;

q.set(a, b, c);

return q;

}
Quadratic operator*(const double& r, const Quadratic& q )

{

double

a = r * q.get_a(),

b = r * q.get_b(),

c = r * q.get_c();

Quadratic q1;

q1.set(a, b, c);

return q1;

}
void Quadratic::display(const string& strPrompt)

{

cout << "Value of " << strPrompt << " " <<

setw(8) << setprecision(2) << fixed << a <<

setw(8) << setprecision(2) << fixed << b <<

setw(8) << setprecision(2) << fixed << c << endl << endl;

}
//******************************************************************
// Function prototypes for friend functions operator* and operator+
//******************************************************************
Quadratic operator*(const double& r, const Quadratic& q);
Quadratic operator+(const Quadratic& q1, const Quadratic& q2);

//***************************************************************
// Function: main
//***************************************************************
int main()
{

Quadratic s, t, u, v;

s.set(1, 1, 1);

s.display(" s ");

t.set(2, 2, 2);

t.display(" t ");

u = s + t;

u.display(" s + t ");

v = 5.0 * t;

v.display("5.0 * t ");

system("PAUSE");

return 0;

}
//***************************************************************
// Function: NOT WORKING
//***************************************************************
void quadratic()
{

float a = 0.0;
float b = 0.0;
float c = 0.0;
float x1 = 0.0;
float x2 = 0.0;
float x3 = 0.0;
float x4 = 0.0;

//this section gets user input and displays message
cout << "Enter the coefficients a , b , c for equation in the form ax^ + bx + c = 0:\n";
cout << endl;
cout << "Enter value for a:\n";
cin >> a;
cout << "Enter value for b:\n";
cin >> b;
cout << "Enter value for c:\n";
cin >> c;

//are all the coefficients 0? if so both roots are 0
if(a == 0 && b == 0 && c == 0)
{
x1 = 0;
x2 = 0;
cout << "The roots are:" "\n"
<< "x1 = " << x1 << " , " << "x2 = " << x2 << "\n";
}

//is c the only non-zero number? if so tell the user
if(a == 0 && b == 0 && c != 0)
{
c = c;
cout << "There are no roots" "\n"
<< "c = " << c << "\n";
}

//is a zero? if so solve the resulting linear equasion and notify user
if(a == 0 && b != 0 && c !=0)
{
cout << "The values entered do not make a quadratic expression" "\n"
<< "x = " << -c/b << "\n";
}

//if b is zero and c is zero tell user
if(a == 0 && b != 0 && c == 0)
{
x1 = 0;
x2 = 0;
cout << "The roots are:" "\n"
<< "x1 = " << x1 << " , " << "x2 = " << x2 << "\n";
}
//if b and c are equal to zero then ax^=0 and since a cannot be zero without
// x being zero also let user know
if(a != 0 && b == 0 && c == 0)
{
x1 = 0;
x2 = 0;
cout << "The values entered result in ax^= 0; so both roots are 0" "\n"
<< "x1 = " << x1 << " , " << "x2 = " << x2 << "\n";
}
//factor out x from ax^+bx=0 and either x = 0 or ax + b =0 then solve the
//linear equation
if(a != 0 && b != 0 && c == 0)
{
x1 = 0;
x2 = -b/a;
cout << "The roots are:" "\n"
<< "x1 = " << x1 << " , " << "x2 = " << x2 << "\n";
}
//now we get to use the square root function and let the user know they have
// some imaginary numbers to deal with
if(a < 0 && b == 0 && c < 0)
{
x1 = -b/(2*a);
x4 = (b*b)-(4*a*c);
x4 = -x4;
x2 = sqrt(x4)/(2*a);
x3 = -sqrt(x4)/(2*a);

cout << "The roots are not real numbers:" "\n"
<< "x1 =" << x1 << " + " << x2 << " * i " << "\n"
<< "x2 =" << x1 << " + " << x3 << " * i " << "\n";
}

if(a > 0 && b == 0 && c > 0)
{
x1 = -b/(2*a);
x4 = (b*b)-(4*a*c);
x4 = -x4;
x2 = sqrt(x4)/(2*a);
x3 = -sqrt(x4)/(2*a);

cout << "The roots are not real numbers:" "\n"
<< "x1 =" << x1 << " + " << x2 << " * i " << "\n"
<< "x2 =" << x1 << " + " << x3 << " * i " << "\n";
}
//now a and c are opposite signs so the answer will be real
if(a > 0 && b == 0 && c < 0)
{
x1 = (-b + (sqrt(pow(b,2)-(4*a*c))))/(2*a);
x2 = (-b - (sqrt(pow(b,2)-(4*a*c))))/(2*a);

cout << "The roots are:" "\n"
<< "x1 = "<< x1 << " , " << "x2 = "<< x2 << "\n";
}
if(a < 0 && b == 0 && c > 0)
{
x1 = (-b + (sqrt(pow(b,2)-(4*a*c))))/(2*a);
x2 = (-b - (sqrt(pow(b,2)-(4*a*c))))/(2*a);

cout << "The roots are:" "\n"
<< "x1 = "<< x1 << " , " << "x2 = "<< x2 << "\n";
}
//ok now if we end up not having to take the square root of a neg do the math
if(a != 0 && b != 0 && c != 0 && (4*a*c) <= pow(b,2))
{
x1 = (-b + (sqrt(pow(b,2)-(4*a*c))))/(2*a);
x2 = (-b - (sqrt(pow(b,2)-(4*a*c))))/(2*a);

cout << "The roots are:" "\n"
<< "x1 = "<< x1 << " , " << "x2 = " << x2 << "\n";
}
//here we have to deal with non x intercepts ie: sqrt(-1) alter the formula
// slightly to give correct output and let the user know
if(a != 0 && b != 0 && c != 0 && (4*a*c)> pow(b,2))
{
x1 = -b/(2*a);
x4 = (b*b)-(4*a*c);
x4 = -x4;
x2 = sqrt(x4)/(2*a);
x3 = -sqrt(x4)/(2*a);

cout << "The roots are not real numbers" "\n"
<< "x1 =" << x1 << " + " << x2 << " * i " << "\n"
<< "x2 =" << x1 << " + " << x3 << " * i " << "\n";
}


}
Last edited on
Topic archived. No new replies allowed.