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
|
#include <iostream>
#include <cmath>
using namespace std;
void findCoeff();
void equSolver(double a, double b, double c, double d);
double discr(double a, double b, double c);
void outResults(double root1, double root2, double a, double b, double c, double d);
void findCoeff(){
double a;
double b;
double c;
cout << "\nEnter coefficient a:\n";
cin >> a;
cout << "\nEnter coefficient b:\n";
cin >> b;
cout << "\nEnter coefficient c:\n";
cin >> c;
}
double discr(double a, double b, double c){
double d;
d = pow(b, 2) - (4 * a * c);
return d;
}
void equSolver(double a, double b, double c, double d){
double root1 = ((-1 * b) + sqrt(d)) / (2 * a);
double root2 = ((-1 * b) - sqrt(d)) / (2 * a);
}
void outResults(double root1, double root2, double a, double b, double c, double d){
if( std::isnan(root1) || std::isnan(root2)){
cout << "Quadratic equation with the following coefficients: \n";
cout << "a: " << a << "; b: " << b << "; c: " << c << "\n";
cout << "has no roots in the real domain\n";
}
else {
cout << "Quadratic equation with the following coefficients: \n";
cout << "a: " << a << "; b:" << b << "; c:" << "\n";
cout << "has the following roots\n";
cout << "Root1: " << root1 << "; Root2: " << root2 << "\n";
}
}
int main(){
findCoeff();
equSolver(a, b, c, d);
outResults(root1, root2, a, b, c, d);
}
|