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
|
#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c, d, x, x2, y;
double rl, img;
char choice;
int min, max;
int t;
puts("A quadratic formula is in the form of ax^2 + bx + c = 0");
puts("Please enter a: ");
scanf_s("%f", &a);
while(a > 10000000 || a < -1000000)
{
puts("Range exceeded. Please enter a number between -1,000,000 and 1,000,000 for a.");
scanf_s("%f", a);
}
puts("Please enter b: ");
scanf_s("%f", &b);
while(b > 10000000 || b < -1000000)
{
puts("Range exceeded. Please enter a number between -1,000,000 and 1,000,000 for b.");
scanf_s("%f", b);
}
puts("Please enter c: ");
scanf_s("%f", &c);
while(c > 10000000 || c < -1000000)
{
puts("Range exceeded. Please enter a number between -1,000,000 and 1,000,000 for c.");
scanf_s("%f", c);
}
d=b*b-4*a*c;
if(d > 0)
{
x=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("Roots: %f, %f\n", x, x2);
}
else if(d < 0)
{
rl= -b/(2*a);
img=sqrt(-d)/(2*a);
printf("Roots: %.3f+%.3fi, %.3f+%.3fi\n", rl, img, rl, img);
}
else
{
x=-b/(2*a);
printf("Root: %f\n", x);
}
puts("Would you like to have a table of values displayed?");
puts("Enter Y for yes, and N for no.");
scanf_s("%c", &choice);
if(choice == 'y' || choice == 'Y')
{
puts("Enter the minimum x value for your table.");
scanf_s("%d", &min);
while (min >=100 || min <= 100)
puts("Please enter a value between -100 and 100");
puts("Enter the maximum x value for your table.");
scanf_s("%d", &max);
while (max >=100 || max <= 100)
puts("Please enter a value between -100 and 100");
for(t = min;t <= max;t++)
{
y=a*t*t+b*t+c;
printf("x |y");
printf("---------------");
printf("%d %f", t,y);
}
}
else if(choice == 'n' || choice == 'N')
{
puts("Goodbye");
}
else
{
while(choice!='y'||choice!='Y'||choice!='n'||choice!='N')
{
puts("Error. Please enter a Y or an N: ");
scanf_s("%c", choice);
}
}
return 0;
}
|