![]() ![]() ![]() | coding-style/variabel-navne/hungarian.c - Hungarian notation - forkortede typenavne anvendt som prefix på alle variable. | Lektion 6 - slide 17 : 21 Program 5 |
#include <stdio.h> #include <math.h> /* Hungarian notation - abbreviated type names attached to all variables */ void solve_quadratic_equation(double d_a, double d_b, double d_c, int *pi_number_of_roots, double *pd_root1, double *pd_root2){ double d_discriminant; d_discriminant = d_b * d_b - 4 * d_a * d_c; if (d_discriminant < 0){ *pi_number_of_roots = 0; } else if (d_discriminant == 0){ *pi_number_of_roots = 1; *pd_root1 = -d_b/(2*d_a); } else{ *pi_number_of_roots = 2; *pd_root1 = (-d_b + sqrt(d_discriminant))/(2*d_a); *pd_root2 = (-d_b - sqrt(d_discriminant))/(2*d_a); } } int main(void) { double d_a, d_b, d_c, d_firstRoot, d_secondRoot; int i_number_of_roots; printf("Enter coeficients a, b, and c: "); scanf("%lf %lf %lf", &d_a, &d_b, &d_c); if (d_a != 0){ solve_quadratic_equation(d_a, d_b, d_c, &i_number_of_roots, &d_firstRoot, &d_secondRoot); if (i_number_of_roots == 0) printf("No roots\n"); else if (i_number_of_roots == 1) printf("One root: %f\n", d_firstRoot); else printf("Two roots: %f and %f\n", d_firstRoot, d_secondRoot); } else printf("The coeficient a must be non-zero"); return 0; }