-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathquadratic.cpp
More file actions
43 lines (37 loc) · 1.17 KB
/
quadratic.cpp
File metadata and controls
43 lines (37 loc) · 1.17 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 <iostream>
#include <iomanip>
#include <cmath>
void SolveQuadratic(int a, int b, int c) {
constexpr const int precision { 6 };
// Сохраняем текущее состояние потока
std::ios oldState(nullptr);
oldState.copyfmt(std::cout);
std::cout << std::setprecision(precision);
if (a == 0) {
if (b == 0) {
if (c == 0) {
std::cout << "infinite solutions";
} else {
std::cout << "no solutions";
}
} else {
std::cout << -static_cast<double>(c) / b;
}
} else {
const int d { b * b - 4 * a * c };
if (d < 0) {
std::cout << "no solutions";
} else if (d == 0) {
if (b == 0) {
std::cout << "0";
} else {
std::cout << -static_cast<double>(b) / (2 * a);
}
} else {
const double sqrt_d { std::sqrt(d) };
std::cout << (-b - sqrt_d) / (2 * a) << ' ' << (-b + sqrt_d) / (2 * a);
}
}
// Восстанавливаем состояние потока
std::cout.copyfmt(oldState);
}