这是我正在尝试修复的程序。我输入了 1,5,6,应该有 2 个解决方案,它说只有 1 个解决方案存在。我也试图让它显示十进制值(我应该使用双精度值吗?)。下面是我的代码,我做错了什么?
#include <stdio.h>
#include <math.h>
int main(void)
{
int inputs[3], a, b, c, d, x, x1, x2, i, lastDigit;
char *os, *noSol = "No solution\n", *cont = 'y';
while (cont == 'Y' || cont == 'y')
{
printf("This program solves a quadratic equation\n");
for (i = 1; i <= 3; i++)
{
lastDigit = i % 10;
if (i >= 4 && i <= 20)
os = "th";
if (i == 1 || lastDigit == 1)
os = "st";
else if (i == 2 || lastDigit == 2)
os = "nd";
else if (i == 3 || lastDigit == 3)
os = "rd";
else
os = "th";
printf("Enter your %d%s number: ", i, os);
scanf("%d", &inputs[i - 1]);
}
a = inputs[0];
b = inputs[1];
c = inputs[2];
while (1)
{
if (a == 0)
{
if (b == 0)
{
printf(noSol);
break;
}
else
{
x = -c / b;
printf("The equation is not quadratic and the solution is %d\n", x);
break;
}
}
else
{
d = pow(b, 2) - 4 * a * c;
if (d < 0)
{
printf(noSol);
break;
}
else if (d == 0)
{
x1 = -b / 2 * a;
printf("One solution: %d\n", x1);
break;
}
else if (d > 0)
{
x1 = (-b + sqrt(d)) / 2 * a;
x2 = (-b - sqrt(d)) / 2 * a;
printf("Two solutions: %d and %d\n", x1, x2);
break;
}
}
}
printf("Run program second time? ( Y / N )\n");
scanf("%s", &cont);
}
getch();
}