-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathquadratic.cpp
More file actions
50 lines (44 loc) · 1.18 KB
/
quadratic.cpp
File metadata and controls
50 lines (44 loc) · 1.18 KB
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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdexcept>
void SolveQuadratic(int a, int b, int c) {
std::cout << std::defaultfloat << std::setprecision(6);
if (a == 0 && b == 0 && c == 0) {
std::cout << "infinite solutions";
return;
}
if (a == 0) {
if (b == 0) {
std::cout << "no solutions";
}
else {
double x = -static_cast<double>(c) / b;
if (x == -0) {
x = 0;
}
std::cout << x;
}
return;
}
double discriminant = static_cast<double>(b) * b - 4.0 * a * c;
if (discriminant < 0) {
std::cout << "no solutions";
}
else if (discriminant == 0) {
double x = -static_cast<double>(b) / (2.0 * a);
if (x == -0) {
x = 0;
}
std::cout << x;
}
else {
double sqrt_d = std::sqrt(discriminant);
double x1 = (-static_cast<double>(b) - sqrt_d) / (2.0 * a);
double x2 = (-static_cast<double>(b) + sqrt_d) / (2.0 * a);
if (x1 > x2) {
std::swap(x1, x2);
}
std::cout << x1 << " " << x2;
}
}