You are on page 1of 2

#include <iostream>

#include <string>
#include <cmath>
using namespace std;

int Get_coefficients(double* ,double* , double* );


int Calc_discriminant(double, double, double, double* );
int Calc_root(char , double , double ,double , double );

int main()
{
double a,b,c, discriminant;
char type;
bool loop = true ;
string answer;
while(loop)
{
Get_coefficients( &a, &b, &c ) ;
type = Calc_discriminant( a, b, c, &discriminant) ;
Calc_root( type, a, b, c, discriminant) ;
cout<<"Would you like to coninue? (enter yes or no) : ";
cin>> answer;

if(answer == "yes")
{
loop = true ;
}
else if(answer == "no")
{
loop = false ;
cout<<" Goodbye." << endl;
}
else
{
loop = false;
cout<<"You have entered the wrong input!\n" <<"Goodbye " <<endl;
}
}

return 0;
}

int Get_coefficients(double *a_ptr,double *b_ptr, double *c_ptr)


{
cout<< "Enter the first coefficient: ";
cin>> *a_ptr;
cout<< "Enter the second coefficient: ";
cin>> *b_ptr;
cout<<"Enter the third corfficient: ";
cin>> *c_ptr;
cout<<endl;
return 0;
}

int Calc_discriminant(double a, double b, double c, double *discriminant)


{
char check;
double d;
*discriminant = pow(b,2) - (4 *a *c) ;
*discriminant = d;
if (d > 0)
{
check = 'r' ;
cout<<"The roots are real and different" <<endl;
}
else if (d < 0)
{
check = 'i' ;
cout<<"The roots are imaginery" <<endl;
}
else
{
check = 's' ;
cout<<"The roots are same"<<endl;
}
return check;
}

int Calc_root(char check, double a, double b,double c, double discriminant)


{
double root_1 = ((-b) + sqrt(discriminant)) / 2*a ;
double root_2 = ((-b) - sqrt(discriminant)) / 2*a ;
double real = -b/ (2*a) ;
double imaginery = sqrt(-discriminant) / (2*a) ;

if (check == 'r')
{
cout << "x1 = "<< root_1 <<endl << "x2 = " << root_2 << endl;
}
else if (check == 'i')
{
cout<< "x1 = "<< real <<"+ " << imaginery << "i" <<endl;
cout<< "x2 = "<< real <<"- " << imaginery << "i" <<endl;
}
else
{
cout << "x1 = "<< root_1 <<endl << "x2 = " << root_2 << endl;
}
return 0;
}

You might also like