Created
February 4, 2019 16:07
-
-
Save gxespino/8fb570676e6640bf988ec5da04938789 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
class QuadraticCalculator { | |
public: | |
string numberOfSolutions(); | |
void printSolutions(); | |
QuadraticCalculator(double, double, double); | |
private: | |
/* Quotients */ | |
double a; | |
double b; | |
double c; | |
double discriminant(); | |
bool hasInfinitySolutions(); | |
bool hasNoSolution(); | |
bool hasNoRealSolution(); | |
bool hasOneSolution(); | |
bool hasTwoSolutions(); | |
}; | |
QuadraticCalculator::QuadraticCalculator(double varA, double varB, double varC) { | |
a = varA; | |
b = varB; | |
c = varC; | |
} | |
double QuadraticCalculator::discriminant() { | |
return (b*b) - (4*(a*c)); | |
} | |
bool QuadraticCalculator::hasInfinitySolutions() { | |
return (a == 0) && (b == 0) && (c == 0); | |
} | |
bool QuadraticCalculator::hasNoSolution() { | |
return (a == 0) && (b == 0) && (c != 0); | |
} | |
bool QuadraticCalculator::hasNoRealSolution() { | |
return (discriminant() < 0); | |
} | |
bool QuadraticCalculator::hasOneSolution() { | |
return (discriminant() == 0); | |
} | |
bool QuadraticCalculator::hasTwoSolutions() { | |
return (discriminant() > 0); | |
} | |
string QuadraticCalculator::numberOfSolutions() { | |
const string INFINITE_SOLUTIONS = "an infinite number of solutions"; | |
const string NO_SOLUTION = "no solution"; | |
const string NO_REAL_SOLUTION = "no real solution"; | |
const string ONE_SOLUTION = "a single real solution"; | |
const string TWO_SOLUTIONS = "two real solutions"; | |
string result; | |
if (hasInfinitySolutions()) { | |
result = INFINITE_SOLUTIONS; | |
} else if (hasNoSolution()) { | |
result = NO_SOLUTION; | |
} else if (hasNoRealSolution()) { | |
result = NO_REAL_SOLUTION; | |
} else if (hasOneSolution()) { | |
result = ONE_SOLUTION; | |
} else if (hasTwoSolutions()) { | |
result = TWO_SOLUTIONS; | |
} | |
return result; | |
} | |
void QuadraticCalculator::printSolutions() { | |
double x1; | |
double x2; | |
if (hasNoSolution() || hasInfinitySolutions() || hasNoRealSolution()) { | |
cout << endl; | |
} else { | |
if (hasTwoSolutions()) { | |
x1 = (-b + sqrt(discriminant())) / 2*a; | |
x2 = (-b - sqrt(discriminant())) / 2*a; | |
cout << " x1=" << x1 << " and " << "x2=" << x2 << endl; | |
} else if (hasOneSolution()) { | |
x1 = -b / 2*a; | |
cout << " x=" << x1 << endl; | |
} | |
} | |
} | |
int main() { | |
double a; | |
double b; | |
double c; | |
cout << "Please enter value of a: "; | |
cin >> a; | |
cout << "Please enter value of b: "; | |
cin >> b; | |
cout << "Please enter value of c: "; | |
cin >> c; | |
QuadraticCalculator calc(a, b, c); | |
cout << "The equation has " << calc.numberOfSolutions(); | |
calc.printSolutions(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment