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
|
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(int argc, char** argv) {
float a = 0.0, b = 0.0, c = 0.0, x1 = 0.0, x2 = 0.0;
int verbose = 1;
char ch = 0;
/* Get values to use: */
printf("Enter a, b and c, space separated, followed by ENTER.\n"
"Currently only integer numbers work.\n\n:"
);
scanf("%f %f %f", &a, &b, &c);
fflush(stdin);
printf("\nVerbose output? [y/N]: ");
if ((ch = getchar()) == 'Y' || ch == 'y')
verbose = 1;
else
verbose = 0;
putchar('\n');
if (verbose == 1) {
/* Print lots of output */
printf("a = %f\nb = %f\nc = %f\n\n"
"-b = %f\nb^2 = %f\n4ac = %f\n2a = %f\n\n",
a, b, c,
-b, b * b, 4.0 * a * c, 2.0 * a
);
printf("x = -b +- sqrt(b^2 - 4ac)\n"
" =====================\n"
" 2a\n\n"
"x = (%f + sqrt(%f - %f)) / %f OR\n"
"x = (%f - sqrt(%f - %f)) / %f\n\n",
-b, b * b, 4.0 * a * c, 2.0 * a,
-b, b * b, 4.0 * a * c, 2.0 * a
);
}
/* The calculations are very simple using the quadratic formula. */
x1 = (-b + sqrt((b * b) - (4 * a * c))) / (2 * a);
x2 = (-b - sqrt((b * b) - (4 * a * c))) / (2 * a);
/* Probably a computer would be faster at factorizing, or completing the square
* but this is a simple example, not a super-optimized production-quality
* microsoft program (ha!) </ms bashing>.
*/
printf("x = %f, %f\n", x1, x2);
fflush(stdout);
return 0;
}
| |