繁体   English   中英

任意多项式:C代码

[英]an arbitrary Polynomial : C code

我被要求编写一个计算任意多项式的C代码,我做了这个,但是似乎某处有一个错误。

这是我的代码:

#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;
}

这是我的输入,经过多次尝试,我发现代码正在输出用户输入的第一件事的值。

我编辑了代码以向我展示实际情况:

enter a0, a1, a2, a3. and your x value in the mentioned order,
make a single space between your inputs
44 4 4 4 4
0.000000 + 0.000000 + 0.000000 + 44.000000 = f(x) = 44.000000

那就是输出!

格式和输入不一致,请更改格式或输入方式,

对于格式:尝试在格式" " 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM