-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathquadratic.cpp
More file actions
36 lines (33 loc) · 875 Bytes
/
quadratic.cpp
File metadata and controls
36 lines (33 loc) · 875 Bytes
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
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <cmath>
void SolveQuadratic(int a, int b, int c) {
using std::cout, std::setprecision;
if(a == 0 && b == 0 && c == 0) {
cout << "infinite solutions";
return;
}
if(a == 0 && b == 0 && c != 0) {
cout << "no solutions";
return;
}
if(a != 0) { // two roots
int discriminant = b * b - 4 * a * c;
cout << setprecision(6);
if(discriminant > 0) {
double discriminantRoot = sqrt(discriminant);
double x1 = (-b - discriminantRoot) / (2.0*a);
double x2 = (-b + discriminantRoot) / (2.0*a);
cout << x1 << ' ' << x2;
} else if(discriminant == 0){
double x1 = (-b) / (2.0*a);
cout << x1;
} else {
cout << "no solutions";
}
} else { // only one root
double x1 = static_cast<double>(-c) / b;
cout << x1;
}
}