请看下面的代码。
#include <stdio.h>
int main()
{
int a, b, c, d;
int p2, p1, p0;
printf("Enter a:");
scanf("%d",&a);
printf("Enter b:");
scanf("%d",&b);
printf("Enter c:");
scanf("%d",&c);
printf("Enter d:");
scanf("%d",&d);
p2 = a*c;
p1 = a*d + b*c;
p0 = b*d;
printf("The product is: %dx^2+%dx+%d\n",p2,p1,p0);
return 0;
}
输出如下:
Enter a:1.5
Enter b:Enter c:Enter d:The product is 3x^2+0x+0
谁能解释一下?一旦我们给出 1.5,它就不会接受剩余系数的值。我们如何获得该输出?
最佳答案
声明
将所有参数声明为double
而不是int
。 (a
, b
, c
, d
, p0
, p1
, p2
)
双格式
当使用 %d
时,需要一个 int
。
在处理 double 时使用 %lf
。
这里指的是 scanf
和 printf
。
代码:
#include <stdio.h>
int main()
{
double a, b, c, d;
double p2, p1, p0;
printf("Enter a:");
scanf("%lf",&a);
printf("Enter b:");
scanf("%lf",&b);
printf("Enter c:");
scanf("%lf",&c);
printf("Enter d:");
scanf("%lf",&d);
p2 = a*c;
p1 = a*d + b*c;
p0 = b*d;
printf("The product is: %lfx^2+%lfx+%lf\n",p2,p1,p0);
return 0;
}
关于c - 多项式乘法 (ax+b)*(cx+d)。任何系数为 float 时的奇怪输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49789206/