-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathE4-1.c
85 lines (78 loc) · 1.81 KB
/
E4-1.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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* FILENAME : E4-1.c
*
* DESCRIPTION :
* Asks for the 3 coefficients (a,b,c) of a quadratic equation
* and writes the solution to screen.
*
* NOTES :
* This program is the solution to the proposed exercise
* in the fourth week of the course
* 'Programación' (programming) of Burgos University.
*
* AUTHOR : Rodrigo Día START DATE : 8 Mar 2017
*
* CHANGES:
* 1.0 [10MAR2017] - Initial release
*/
#include <stdio.h>
#include <math.h>
void readCoefficients(float *, float *, float *);
int determineType(float, float, float);
void showResult(int, float, float, float);
int calculateRoot(float, float, float);
int main(){
float a, b, c;
readCoefficients(&a, &b, &c);
showResult(determineType(a, b, c), a, b, c);
return 0;
}
void readCoefficients(float *a, float *b, float *c){
printf("Insert the coefficients a, b, c separated by commas\n");
scanf("%f,%f,%f", &*a,&*b,&*c);
}
int determineType(float a, float b, float c){
int type=3;
if(a==0){
if(b==0){
type=1;
}else{
type=2;
}
}
return type;
}
void showResult(int type, float a, float b, float c){
float root;
if(type==1){
if(c==0){
printf("Zero polynomial");
}else{
printf("Degree 0 polynomial");
}
}else{
if(type==2){
printf("Degree 1 polynomial, solution x=%g",-c/b);
}else{
printf("Degree 2 polynomial, solution x=");
root=calculateRoot(a,b,c);
if(root==0){
printf("=%g\n",-b/pow(b,2));
}else{
if(root>0){
if(b==0){
printf("\u00B1%g",(-b+sqrt(root))/(2*a));
}else{
printf("%g, x=%g",(-b+sqrt(root))/(2*a),(b+sqrt(root))/(2*a));
}
}else{
printf("%.3g\u00B1%.3gi",-b/(2*a),sqrt(-root)/(2*a));
}
}
}
}
printf("\n");
}
int calculateRoot(float a, float b,float c){
return pow(b,2)-4*a*c;
}