-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathquadratic.cpp
More file actions
43 lines (41 loc) · 1.22 KB
/
quadratic.cpp
File metadata and controls
43 lines (41 loc) · 1.22 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
#include <stdexcept>
#include <cmath>
#include <iomanip>
void SolveQuadratic(int a, int b, int c) {
if (a == 0)
{
if (b == 0) {
if (c == 0) {
std::cout << "infinite solutions";
}
else {
std::cout << "no solutions";
}
}
else {
double sol = -static_cast<double>(c) / static_cast<double>(b);
std::cout << std::setprecision(6) << sol;
}
}
else {
// Êâàäðàòíîå óðàâíåíèå
double discriminant = static_cast<double>(b) * static_cast<double>(b) - 4.0 * static_cast<double>(a) * static_cast<double>(c);
if (discriminant < 0) {
std::cout << "no solutions";
}
else if (discriminant == 0) {
double sol = -b / (2.0 * a);
std::cout << std::setprecision(6) << sol;
}
else {
double sqr = std::sqrt(discriminant);
double x1 = (-b - sqr) / (2.0 * a);
double x2 = (-b + sqr) / (2.0 * a);
// Îáåñïå÷èâàåì ïîðÿäîê x1 < x2
if (x1 > x2) {
std::swap(x1, x2);
}
std::cout << std::setprecision(6) << x1 << " " << x2;
}
}
}