1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <cmath>
using namespace std;
double RealRootOne (double a, double b, double c)
{
double RealRootOne = ((-1*b) + sqrt((b*b) - (4*a*c))) / (2*a);
return RealRootOne;
}
double RealRootTwo (double a, double b, double c)
{
double RealRootTwo = ((-1*b) - sqrt((b*b) - (4*a*c))) / (2*a);
return RealRootTwo;
}
void printRealResult (double a, double b, double c)
{
cout << "Your results are " << RealRootOne(a, b, c) << " and " << RealRootTwo(a, b, c) << endl;
}
double ComplexRootOne (double a, double b, double c)
{
double ComplexRealOne = (-1*b) / (2*a);
double ComplexImOne = (-1*((b*b) - (4*a*c))) / (2*a);
return ComplexRealOne + ComplexImOne;
}
double ComplexRootTwo (double a, double b, double c)
{
double ComplexRealTwo = (-1*b) / (2*a);
double ComplexImTwo = (-1*((b*b) - (4*a*c))) / (2*a);
return ComplexRealTwo - ComplexImTwo;
}
void printComplexResult (double a, double b, double c)
{
cout << "You're results are " << ComplexRootOne(a, b, c) << "i and " << ComplexRootTwo(a, b, c) << "i" << endl;
}
int main()
{
cout << "Enter a, b and c for quadratic in the form ax^2 + bx + c = 0" << endl;
double a, b, c;
cin >> a >> b >> c;
double disc = (b*b) - (4*a*c);
if (disc < 0)
{
printComplexResult(a, b, c);
}
if (disc == 0)
{
cout << "One real and repeated root " << ((-1*b) / (2*a));
}
else if (disc > 0)
{
printRealResult(a, b, c);
}
return 0;
}
|