The roots of the quadratic equation ax2 bx c 0 a notequal
Solution
PROGRAM CODE:
/*
* quadratic.cpp
*
* Created on: 15-Nov-2016
* Author: Kaju
*/
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>
using namespace std;
class quadraticEq
{
public:
int a,b,c;
float discriminant;
float realRoots[2];
string complexRoots[2];
bool isComplex;
quadraticEq()
{
a = 0;
b = 0;
c = 0;
discriminant = 0;
isComplex = false;
}
quadraticEq(int in_a, int in_b, int in_c)
{
a = in_a;
b = in_b;
c = in_c;
discriminant = calcDiscriminant();
}
double calcDiscriminant()
{
//cout<<sqrt(pow(b,2) - (4*a*c));
float val = pow(b,2) - (4*a*c);
//cout<<val;
return val;
}
int numberOfroots()
{
if(discriminant == 0)
return 1;
else
return 2;
}
void calcRoots()
{
if(numberOfroots() == 1)
{
// realRoots = new double[1];
realRoots[0] = (-1*b)/(2*a);
}
else
{
if(discriminant < 0)
{
// complexRoots = new string[2];
int discri = discriminant * -1;
float val1 = (-1.0*b)/(2*a) ;
//cout<<\"val1: \"<<val1;
float val2 = sqrt(discri)/(2*a);
complexRoots[0] = to_string(val1) + \" \" ;
if(val2>0)
complexRoots[0] = complexRoots[0] + \"+ \";
complexRoots[0] = complexRoots[0] + to_string(val2) + \"i\";
complexRoots[1] = to_string(val1) + \" \" ;
val2 = -1*val2;
if(val2>0)
complexRoots[1] = complexRoots[1] + \"+ \";
complexRoots[1] = complexRoots[1] + to_string(val2) + \"i\";
isComplex = true;
}
else
{
// realRoots = new double[2];
float squareroot = sqrt(discriminant);
float root1 = ((-1*b) + squareroot) /(2*a);
realRoots[0] = root1;
float root2 = ((-1*b) - squareroot)/(2*a);
realRoots[1] = root2;
}
}
}
quadraticEq operator +(const quadraticEq &eq)
{
quadraticEq equation;
equation.a = this->a + eq.a;
equation.b = this->b + eq.b;
equation.c = this->c + eq.c;
equation.discriminant = calcDiscriminant();
return equation;
}
quadraticEq operator -(const quadraticEq &eq)
{
quadraticEq equation;
equation.a = this->a - eq.a;
equation.b = this->b - eq.b;
equation.c = this->c - eq.c;
equation.discriminant = calcDiscriminant();
return equation;
}
bool operator ==(const quadraticEq &eq)
{
if(this->a == eq.a && this->b == eq.b && this->c == eq.c)
return true;
else
return false;
}
bool operator !=(const quadraticEq &eq)
{
if(this->a != eq.a || this->b != eq.b || this->c != eq.c)
return true;
else
return false;
}
friend ostream &operator<<( ostream &output, const quadraticEq &eq )
{
output<<\"\ The equation is\\t\";
output << eq.a<< \"x^2 + \" << eq.b<<\"x + \" << eq.c <<\" = 0\"<<endl;
if(!eq.isComplex)
{
int length = (sizeof(eq.realRoots)/sizeof(*eq.realRoots));
//output<<fixed;
output<<\"The roots are \\t\";
for(int i=0; i<length; i++)
{
output<<\"Root\"<<i+1<<\": \" <<eq.realRoots[i] << \"\\t\";
}
}
else
{
int length = (sizeof(eq.complexRoots)/sizeof(*eq.complexRoots));
for(int i=0; i<length; i++)
{
output<<\"Root : \" <<eq.complexRoots[i] << \"\\t\";
}
}
return output;
}
friend istream &operator>>( istream &input, quadraticEq &eq )
{
input >> eq.a >>eq.b >>eq.c;
eq.discriminant = eq.calcDiscriminant();
return input;
}
};
int main()
{
quadraticEq eq1, eq2(1,1,1), eq3(1,5,6);
cout<<\"Enter the coefficients for equation1: \";
cin>>eq1;
eq1.calcRoots();
cout<<eq1;
cout<<\"\ \ Equation 2: \"<<endl;
eq2.calcRoots();
cout<<eq2;
cout<<\"\ \ Adding equation 1 and equation 3\"<<endl;
quadraticEq eq4 = eq1 + eq3;
eq4.calcRoots();
cout<<eq4;
cout<<\"\ \ Subtracting equation 1 and equation 3\"<<endl;
quadraticEq eq5 = eq1 - eq3;
eq5.calcRoots();
cout<<eq5;
}
OUTPUT:
Enter the coefficients for equation1: 5 7 9
The equation is 5x^2 + 7x + 9 = 0
Root : -0.700000 + 1.144552i Root : -0.700000 -1.144552i
Equation 2:
The equation is 1x^2 + 1x + 1 = 0
Root : -0.500000 + 0.866025i Root : -0.500000 -0.866025i
Adding equation 1 and equation 3
The equation is 6x^2 + 12x + 15 = 0
Root : -1.000000 + 0.953794i Root : -1.000000 -0.953794i
Subtracting equation 1 and equation 3
The equation is 4x^2 + 2x + 3 = 0
Root : -0.250000 + 1.430690i Root : -0.250000 -1.430690i





