-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathquadratic.cpp
More file actions
56 lines (41 loc) · 1.28 KB
/
quadratic.cpp
File metadata and controls
56 lines (41 loc) · 1.28 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
51
52
53
54
55
56
#include <iostream>
#include <iomanip>
#include <cmath>
#include <utility>
void SolveQuadratic(int a, int b, int c) {
std::cout << std::setprecision(6);
if (a == 0 && b == 0 && c == 0) {
std::cout << "infinite solutions";
return;
}
if (a == 0 && b == 0 && c != 0) {
std::cout << "no solutions";
return;
}
if (a == 0) {
double root = -static_cast<double>(c) / b;
if (root == 0.0) root = 0.0;
std::cout << root;
return;
}
double discr = static_cast<double>(b) * b - 4.0 * a * c;
if (discr < 0) {
std::cout << "no solutions";
return;
}
if (discr == 0) {
double root = -static_cast<double>(b) / (2.0 * a);
if (root == 0.0) root = 0.0;
std::cout << root;
return;
}
double sqrt_d = std::sqrt(discr);
double root1 = (-static_cast<double>(b) - sqrt_d) / (2.0 * a);
double root2 = (-static_cast<double>(b) + sqrt_d) / (2.0 * a);
if (root1 > root2) {
std::swap(root1, root2);
}
if (root1 == 0.0) root1 = 0.0;
if (root2 == 0.0) root2 = 0.0;
std::cout << root1 << " " << root2;
}