-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
282 lines (199 loc) · 7.24 KB
/
main.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
const double EPSILON = 1e-10;
//----------------------------------------------
//! Solves a quadratic equation ax2+bx+c=0
//!
//! @param [in] a a-cofficient
//! @param [in] b b-cofficient
//! @param [in] c c-cofficient
//!
//! @param [out] x1 pointer to first root
//! @param [out] x2 pointer to second root
//!
//! @return number of roots
//!
//! @note in case of infinite number of roots
//! returns INFROOTS
//! in case of no roots returns NOROOT
//----------------------------------------------
int finding_quadratic_roots(double a , double b , double c , double* x1 , double* x2 ); // ax2 + bx + c = 0
//----------------------------------------------
//! Solves a lineal equation ax+b=0
//!
//! @param [in] a a-cofficient
//! @param [in] b b-cofficient
//!
//! @param [out] x pointer to first root
//!
//! @return number of roots
//!
//! @note in case of infinite number of roots
//! returns INFROOTS
//! in case of no roots returns NOROOT
//----------------------------------------------
int finding_lineal_roots (double a, double b, double* res); // x + ay + b = 0
//-------------------------------------------
//!
//! Checks if double is zero or not
//!
//! @param [in] a-number
//!
//! @return is zero or not
//!
//-------------------------------------------
bool is_zero (double value);
//-----------------------------------------
//!
//! unit tests for finding_quadratic_roots
//!
//-----------------------------------------
void check_quad();
enum nRoots {
NOROOT = 0,
ONEROOT = 1,
TWOROOTS =2,
INFROOTS = 3
} ;
int main () {
check_quad();
printf ("# Dolgodvorov.ev@phystech.edu\n"
"Solve of quadratic equation\n");
double a = 0, b = 0, c = 0;
double x1 = 0, x2 = 0;
char agreement[10];
char d;
while (true){
printf ("Enter a, b, c\n");
if ((scanf ("%lg%lg%lg%c", &a, &b, &c, &d) != 4) || (d !='\n' && d!=' ')){
printf ("enter isn`t correct\n");
fflush (stdin);
continue;
}
int number_of_roots = finding_quadratic_roots ( a, b, c, &x1, &x2);
switch (number_of_roots) {
case nRoots :: NOROOT:
printf("There is no root\n");
break;
case nRoots :: ONEROOT:
printf("There is only one root: %.3lg\n", is_zero(x1) ? 0.0 : x1);
break;
case nRoots :: TWOROOTS:
printf("There are two roots: %.3lg %.3lg\n", is_zero(x1) ? 0.0 : x1, is_zero(x2) ? 0.0 : x2);
break;
case nRoots :: INFROOTS:
printf("There are infinity number of roots\n");
break;
default:
printf("Something`s gone wrong\n");
}
while(true){
printf ("Do you want to solve one more equation? y/n\n");
scanf("%c", &agreement[0]);
agreement[1]='\n';
agreement[2]='\n'; // to catch symbols after yes and no words
agreement[3]='\n';
for ( int i = 1; agreement[i-1] != '\n' && agreement[i-1] != ' ' && i < 10 ; i++)
scanf("%c", &agreement[i]);
if (((agreement[0] == 'y' &&
agreement[1] == 'e' &&
agreement[2] == 's') ||
(agreement[0] == 'Y' &&
agreement[1] == 'e' &&
agreement[2] == 's') ||
(agreement[0] == 'Y' &&
agreement[1] == 'E' &&
agreement[2] == 'S'))&&
(agreement[3] == '\n' ||
agreement[3] == ' ') ||
((agreement[0] == 'y') ||
(agreement[0] == 'Y'))&&
(agreement[1] == '\n' ||
agreement[1] == ' ')
)
break;
else if (((agreement[0] == 'n') ||
(agreement[0] == 'N'))&&
(agreement[1] == '\n' ||
agreement[1] == ' ') ||
((agreement[0] == 'N' &&
agreement[1] == 'O') ||
(agreement[0] == 'N' &&
agreement[1] == 'o') ||
(agreement[0] == 'n' &&
agreement[1] == 'o'))&&
(agreement[2] == '\n' ||
agreement[2] == ' ')
)
return 0;
else {
printf ("Enter is not correct. ");
continue;
}
}
}
}
int finding_lineal_roots (double a, double b, double *x) {
if (is_zero(a)) {
if (is_zero(b))
return nRoots :: INFROOTS;
else /* c != 0 */
return nRoots ::NOROOT;
} else /* b != 0 */
*x = -b / a;
return nRoots:: ONEROOT;
}
int finding_quadratic_roots(double a , double b , double c , double *x1 , double *x2) {
if(is_zero(a)){
return finding_lineal_roots( b, c, x1);
}else /* not lineal equation */
{
double D = b * b - 4.0 * a * c;
if( D < 0 )
return nRoots :: NOROOT;
else {
double sqrt_D = sqrt(D);
if(is_zero(D)){
*x1= - b / (2.0 * a);
return nRoots :: ONEROOT;
}else { /* D>0 */
*x1 = (- b + sqrt_D) / (2.0 * a);
*x2 = (- b - sqrt_D) / (2.0 * a);
return nRoots :: TWOROOTS;
}
}
}
}
void check_quad() {
double i,j,k;
double x1,x2;
int nR,num=0;
for (i=-4; i<=4; i+=0.5) {
for (j=-4; j<=4; j+=0.5) {
for (k=-4; k<=4; k+= 0.5){
num++;
nR = finding_quadratic_roots (i, j, k, &x1, &x2);
switch (nR) {
case nRoots :: NOROOT:
( (j * j - 4 * i * k < 0) || ( fabs(i) < EPSILON && fabs(j) < EPSILON && fabs(k) >= EPSILON ))?printf ("") : printf ("incorrect\n" "Test %d: %lg %lg %lg %lg %lg ",num , i, j, k, x1, x2);
break;
case nRoots :: ONEROOT:
( fabs(i * x1 * x1 + j * x1 + k ) < EPSILON) ? printf ("") : printf ("incorrect\n" "Test %d: %lg %lg %lg %lg %lg ",num , i, j, k, x1, x2);
break;
case nRoots :: TWOROOTS:
( fabs(i * x1 * x1 + j * x1 + k ) < EPSILON && fabs (i * x2 * x2 + j * x2 + k ) < EPSILON ) ? printf ("") : printf ("incorrect\n" "Test %d: %lg %lg %lg %lg %lg ",num , i, j, k, x1, x2);
break;
case nRoots :: INFROOTS:
(fabs(i) < EPSILON && fabs(j) < EPSILON && fabs(k) < EPSILON) ? printf ("") : printf ("incorrect\n" "Test %d: %lg %lg %lg %lg %lg ",num , i, j, k, x1, x2);
break;
default:
printf("Something`s gone wrong\n");
}
}
}
}
}
bool is_zero (double value) {
return ( fabs(value) <= EPSILON);
}