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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream>
#include <cmath>
using namespace std;
// This function asks for the leading coefficients of the equation,
// and stores the numbers into variables available to the entire program.
void ask(double & aCoeff, double & bCoeff, double & cCoeff)
{
cout << "What is the leading coefficient of X^2? : ";
cin >> aCoeff;
// The quadratic formula doesn't work for first order equations.
while (aCoeff == 0)
{
cout << "It may not equal zero, please try again. :";
cin >> aCoeff;
}
cout << "What is the leading coefficient of X^1? : ";
cin >> bCoeff;
cout << "What is the leading coefficient of X^0? : ";
cin >> cCoeff;
}
// This function displays the equation in an easy to read format.
void displayEquation(double aCoeff, double bCoeff, double cCoeff)
{
// For the first term, if the variable equals one do not display the value.
// Because of the while statement in ask(), aCoeff can not equal zero.
cout << "\nYou entered: ";
if (aCoeff == 1.0)
cout << "X^2";
else if (aCoeff == -1.0)
cout << "-X^2";
else
cout << aCoeff << "X^2";
// For the second term, if the variable equals one do not display the value.
// If the term equals zero, the variable will not apply to any of the if statements.
if (bCoeff == 1.0)
cout << " + X";
else if (bCoeff == -1.0)
cout << " - X";
else if (bCoeff > 0)
cout << " + " << bCoeff << "X";
else if (bCoeff < 0)
cout << " - " << fabs(bCoeff) << "X";
// For the final term it doesn't matter if it equals one,
// only that if it equals zero, it doesn't display the term.
if (cCoeff > 0)
cout << " + " << cCoeff;
else if (cCoeff < 0)
cout << " - " << fabs(cCoeff);
cout << " = 0" << endl;;
}
// This function calculates the roots of the equation using the quadratic formula.
void quad(double a, double b, double c)
{
// The value of discriminant determines if there is an answer, and if there is how many.
double discriminant = (pow(b,2) - 4*a*c);
// Displaying the discrimiant for trouble shooting purposes.
cout << "discriminant = " << discriminant << endl;
if (discriminant < 0)
{
cout << "\nThere are no real roots for that expression." << endl;
return;
}
double posRoot = (-b + sqrt(discriminant)) / (2*a);
double negRoot = (-b - sqrt(discriminant)) / (2*a);
// If the discriminant equals zero there is only one answer.
if (discriminant == 0)
cout << "\nThe root is " << posRoot << endl;
else
cout << "\nThe roots are " << negRoot << " and " << posRoot << endl;
}
int main()
{
cout << "***QUADRATIC ROOT FINDER VER 0.4***\n\n";
double a, b, c;
ask(a, b, c);
displayEquation(a, b, c);
quad(a, b, c);
// Get rid of any character in the input stream, so that it doesn't close automatically.
cin.ignore();
cin.ignore();
return 0;
}
|