-
Notifications
You must be signed in to change notification settings - Fork 0
/
slide_ex2_ponteiros.c
42 lines (34 loc) · 1.03 KB
/
slide_ex2_ponteiros.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int raizes (float a, float b, float c, float *p1, float *p2);
int main () {
float a, b, c, x1, x2;
printf ("Digite o coeficiente A: ");
scanf ("%f", &a);
printf ("Digite o coeficiente B: ");
scanf ("%f", &b);
printf ("Digite o coeficiente C: ");
scanf ("%f", &c);
switch (raizes (a, b, c, &x1, &x2)) {
case -1:
printf ("Nao eh equacao de 2o grau!\n");
break;
case 0:
printf ("Nao ha raizes reais!\n");
break;
case 1:
printf ("As raizes sao %.3f e %.3f\n", x1, x2);
}
return 0;
}
int raizes (float a, float b, float c, float *p1, float *p2) {
if (a == 0)
return -1; // Não é equação de 2o grau.
float delta = b*b - 4*a*c;
if (delta < 0)
return 0; // Não há raízes reais.
*p1 = (-b + sqrt(delta)) / (2 * a);
*p1 = (-b + sqrt(delta)) / (2 * a);
return 1; // Há raízes reais.
}