-
Notifications
You must be signed in to change notification settings - Fork 0
/
equation.c
103 lines (94 loc) · 1.92 KB
/
equation.c
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
#include <stdio.h>
#include <math.h>
#include <float.h>
#define NO_ROOT 0
#define ONE_ROOT 1
#define TWO_ROOTS 2
#define ENDLESS_ROOTS 3
#define IsZero(x) (fabs(x) <= DBL_EPSILON)
#define IsMoreZero(x) (x > DBL_EPSILON)
#define IsLessZero(x) (x < -DBL_EPSILON)
/**
* Function for finding roots
*
* Function finds all possible roots of equation and returns the number of them
*
* @param A coefficient
* @param B coefficient
* @param C coefficient
* @param first root pointer; if it is not exist, result is undefined
* @param second root pointer; if it is not exist, result is undefined
* @return number of roots
*/
int SolveEquation (double a, double b, double c, double *x1, double *x2)
{
if (IsZero (a))
{
//linear equation
if (IsZero (b))
{
//constant
if (IsZero (c))
{
//equation 0=0
return ENDLESS_ROOTS;
}
else
{
//equation c=0, c!=0
return NO_ROOT;
}
}
else
{
//equation bx+c=0, b!=0
*x1 = -c / b;
return ONE_ROOT;
}
}
else
{
//quadratic equation
double d = b*b - (4*a*c);
if (IsZero (d))
{
*x1 = -b / (2*a);
return ONE_ROOT;
}
if (IsMoreZero (d))
{
double sqrt_d;
sqrt_d = sqrt (d);
*x1 = (-b - sqrt_d) / (2*a);
*x2 = (-b + sqrt_d) / (2*a);
return TWO_ROOTS;
}
if (IsLessZero (d))
{
return NO_ROOT;
}
}
return -1; //never happens
}
int main (int argc, char *argv[])
{
double a, b, c, x1, x2;
puts ("Type the quadratic equation coefficients (a, b, c): ");
scanf ("%lf %lf %lf", &a, &b, &c);
switch (SolveEquation (a, b, c, &x1, &x2))
{
case NO_ROOT:
puts ("this equation has no roots");
break;
case ONE_ROOT:
printf ("this equation has 1 root: x = %lf\n", x1);
break;
case TWO_ROOTS:
printf ("this equation has 2 roots: x1 = %lf x2 = %lf\n", x1, x2);
break;
case ENDLESS_ROOTS:
puts ("this equation has infinitely many roots");
break;
}
return 0;
}