-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuadraticEquation.cpp
45 lines (40 loc) · 1.13 KB
/
QuadraticEquation.cpp
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
#include "QuadraticEquation.h"
const char* nameOfValue(const char* name)
{
return name;
}
double getValue(const char* varName,
std::function<const char*(const char*)> fPtr)
{
std::cout << fPtr(varName) << " = ";
double input{};
std::cin >> input;
return input;
}
void printRoots(double a, double b, double c)
{
// declare the roots
double x1{};
double x2{};
// declare the discriminant
double discriminant{ (b * b) - (4 * a * c) };
if (discriminant > 0) {
std::cout << "The roots are real and distinct.\n";
x1 = (-b + sqrt(discriminant)) / (2 * a);
x2 = (-b - sqrt(discriminant)) / (2 * a);
std::cout << "x1 = " << x1 << ", x2 = " << x2 << '\n';
}
else if(discriminant == 0) {
std::cout << "The roots are real and equal.\n";
x2 = x1 = -b / (2 * a);
std::cout << "x1 = " << x1 << ", x2 = " << x2 << '\n';
}
else {
std::cout << "The roots are imaginary.\n";
double real = -b / (2 * a);
double imaginary = sqrt((4 * a * c) - (b * b)) / (2 * a);
std::cout << "x1 = " << real << " + "
<< imaginary << "i\n" << "x2 = "
<< real << " - " << imaginary << "i\n";
}
}