-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.cpp
129 lines (105 loc) · 3.06 KB
/
Functions.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
#include "Kva.h"
void Enter(double* const a, double* const b, double* const c)
{
assert(a != NULL && "a is NULL");
assert(b != NULL && "b is NULL");
assert(c != NULL && "c is NULL");
printf("Quadratic Equation format: aX^2 + bX + c = 0 \n");
printf("Enter the coefficient a \n");
while(scanf("%lg", a) != 1)
printf("Input Error. Try again.");
while(getchar() != '\n'){};
printf("Enter the coefficient b \n");
while(scanf("%lg", b) != 1)
printf("Input Error. Try again.");
while(getchar() != '\n'){};
printf("Enter the coefficient c \n");
while(scanf("%lg", c) != 1)
printf("Input Error. Try again.");
while(getchar() != '\n'){};
}
int Is_zero(const double n)
{
assert(isfinite(n));
const double Epsilon = 1E-10;
return(!(fabs(n) < Epsilon));
}
enum Root_flags Sol_lineal(const double b,const double c, double* const x1)
{
assert(isfinite(b));
assert(isfinite(c));
assert(x1 != NULL && "x1 is NULL");
if (Is_zero(c) == 0)
{
if (Is_zero(b) == 0)
return INF_Roots;
else *x1=0;
return ONE_Root;
}
else
{
if (Is_zero(b) == 0)
return NULL_Roots;
else
*x1 = -c/b;
return ONE_Root;
}
}
enum Root_flags Sol_square(const double a, const double b, const double c, double* const x1, double* const x2)
{
assert(isfinite(a));
assert(isfinite(b));
assert(isfinite(c));
assert(x1 != NULL && "x1 is NULL");
assert(x2 != NULL && "x2 is NULL");
assert(x1 != x2);
double D = (b * b) - ( 4 * a * c);
if(Is_zero(D) == 0)
{
*x1 = -b/(2 * a);
return ONE_Root;
}
else if(D < 0)
return NULL_Roots;
else
{
double sqrtD = sqrt(D);
*x1 = (-b - sqrtD)/(2 * a);
*x2 = (-b + sqrtD)/(2 * a);
return TWO_Roots;
}
// }
}
void Output(enum Root_flags nRoots, const double x1, const double x2)
{
switch(nRoots)
{
case NULL_Roots:
printf("There are no roots");
break;
case ONE_Root:
printf("One real solution: %f", x1);
break;
case TWO_Roots:
printf("There two real solutions: %f, %f", x1, x2);
break;
case INF_Roots:
printf("There are an infinite number of solutions");
break;
}
}
enum Root_flags Eq_solution(const double a, const double b, const double c, double* const x1, double* const x2)
{
assert(isfinite(a));
assert(isfinite(b));
assert(isfinite(c));
assert(x1 != NULL && "x1 is NULL");
assert(x2 != NULL && "x2 is NULL");
assert(x1 != x2);
enum Root_flags nRoots = NULL_Roots;
if(Is_zero(a) == 0)
nRoots = Sol_lineal(b, c, x1);
else
nRoots = Sol_square(a, b, c, x1, x2);
return nRoots;
}