任意多项式:C代码
我被要求编写一个计算任意多项式的C代码,我做了这个,但似乎有一个bug 这是我的代码:任意多项式:C代码,c,C,我被要求编写一个计算任意多项式的C代码,我做了这个,但似乎有一个bug 这是我的代码: #include <stdio.h> #include <math.h> int main (void) { //initialize the variables float x=0; float a1=0; float a2=0; float a3=0; float a0=0; float fx=0; printf("enter a0, a1, a2, a3. and your x
#include <stdio.h>
#include <math.h>
int main (void) {
//initialize the variables
float x=0;
float a1=0;
float a2=0;
float a3=0;
float a0=0;
float fx=0;
printf("enter a0, a1, a2, a3. and your x value in the meintioned order,\nmake a single space between your inputs\n");
scanf("%f, %f, %f, %f, %f", &a0, &a1, &a2, &a3, &x); //reading the values of a1, a2, a3, a0, and x
fx= ((a3 * pow (x, 3)) + (a2 * pow (x, 2)) + (a1 * pow (x, 1)) + (a0 * pow (x, 0)));
printf("f(x) = %lf", fx);
return 0;
}
这就是输出 格式和输入不一致,请更改格式或输入方式 对于格式:尝试在格式
“
qoutes”中不使用,
逗号
scanf("%f %f %f %f %f", &a0, &a1, &a2, &a3, &x); //reading the values of a1, a2, a3, a0, and x
对于输入:将输入更改为44,4,4,4,4
,它将以当前方式工作
scanf("%f,%f,%f,%f,%f", &a0, &a1, &a2, &a3, &x); //reading the values of a1, a2, a3, a0, and x
始终从
scanf
检查返回值。您需要5个数字,因此返回值应为5。如果选中,您会发现scanf
返回1,这意味着只读取了第一个数字。您的代码不会创建显示的输出。最后一行的第一部分打印在哪里?0.000000+0.000000+0.000000+44.000000=是否隐藏了更多的不一致?
scanf("%f,%f,%f,%f,%f", &a0, &a1, &a2, &a3, &x); //reading the values of a1, a2, a3, a0, and x