forked from taniadovzhenko/CppPracticum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbiquadequation.cpp
More file actions
108 lines (99 loc) · 2.16 KB
/
biquadequation.cpp
File metadata and controls
108 lines (99 loc) · 2.16 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <cmath>
#include <cfloat>
#include <cstdbool>
using namespace std;
bool isNearlyZero(double x)
{
return (fabs(x) < 2 * DBL_EPSILON);
}
bool isNearlyEqual(double x, double y)
{
return (fabs(x - y) < 2 * DBL_EPSILON);
}
void quadEquation(double a, double b, double c)
{
if (isNearlyZero(a))
{
if (isNearlyZero(b))
{
if (isNearlyZero(c)) {cout << "Infinity of solutions" << endl;}
else { cout << "No solutions" << endl; }
}
else
{
double x = -c / b;
if (x > 0)
{
double x_res = sqrt(x);
cout << "x1 = " << x_res << endl;
cout << "x2 = " << -x_res << endl;
}
else if (isNearlyZero(x))
{
cout << "x = " << 0 << endl;
}
else
{
cout << "No solutions" << endl;
}
}
}
else
{
double D = b * b - 4 * a * c;
cout << "D = " << D << endl;
if (isNearlyZero(D))
{
double result1 = -b / (2 * a);
cout << "result1 = " << result1 << endl;
if (result1 > 0)
{
double x_res_D = sqrt(result1);
cout << "x1 = " << x_res_D << endl;
cout << "x2 = " << -x_res_D << endl;
}
else if (isNearlyZero(result1))
{
cout << "x = " << 0 << endl;
}
else { cout << "No solutions" << endl; }
}
else if (D>0)
{
double x1 = (-b + sqrt(D)) / (2 * a);
double x2 = (-b - sqrt(D)) / (2 * a);
cout << "X1 = " << x1 << endl;
cout << "X2 = " << x2 << endl;
if (x1 >= 0 && x2 >= 0)
{
cout << "x1 = " << sqrt(x1) << endl;
cout << "x2 = " << -sqrt(x1) << endl;
cout << "x3 = " << sqrt(x2) << endl;
cout << "x4 = " << -sqrt(x2) << endl;
}
else if (x1 >= 0 && x2 < 0)
{
cout << "x1 = " << sqrt(x1) << endl;
cout << "x2 = " << -sqrt(x1) << endl;
}
else if (x1 < 0 && x2 >= 0)
{
cout << "x1 = " << sqrt(x2) << endl;
cout << "x2 = " << -sqrt(x2) << endl;
}
else if (x1 < 0 && x2 < 0)
{
cout << "No real solutions" << endl;
}
}
else { cout << "No real solutions" << endl; }
}
}
int main()
{
double a, b, c;
cout << "Input coefficients of equation: ";
cin >> a >> b >> c;
quadEquation(a, b, c);
}