#include <stdio.h>
#include <conio.h>
#include <math.h>
#define ESP 0.0001
#define F(x) x^3-2.5x^2-1.8x+2.356
void main()
{
float x0,x1,x2,f1,f2,f0;
int count=0;
do
{
printf("\nEnter the value of x0: ");
scanf("%f",&x0);
}while(F(x0) > 0);
do
{
printf("\nEnter the value of x1: ");
scanf("%f",&x1);
}while(F(x1) < 0);
printf("\n__________________________________________________________\n");
printf("\n x0\t x1\t x2\t f0\t f1\t f2");
printf("\n__________________________________________________________\n");
do
{
f0=F(x0);
f1=F(x1);
x2=x0-((f0*(x1-x0))/(f1-f0));
f2=F(x2);
printf("\n%f %f %f %f %f %f",x0,x1,x2,f0,f1,f2);
if(f0*f2<0)
{
x1=x2;
}
else
{
x0 = x2;
}
}while(fabs(f2)>ESP);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",x2);
getch();
}
该程序似乎无法将
#define F(x) x^3-2.5x^2-1.8x+2.356
读取为函数,但是当我使用#define F(x) 3*(x) - 1 - cos(x)
时没有错误。我尝试了
#define F(x) (x)^3-2.5(x)^2-1.8(x)+2.356
,没有运气。 最佳答案
F(x)x ^ 3-2.5x ^ 2-1.8x + 2.356
^不是C中的幂运算符,使用pow()和powf()函数(但在这种情况下,不需要它)
2.5x必须是2.5 * x-购买有关C的书
不要#define函数使它们成为“真实”函数
不要在浮点表达式中使用双字面量
float F(float x)
{
return x * x * x + 2.5f * x * x + 1.8f * x + 2.356f;
}
关于c - 错误:浮点常量的后缀“x”无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45074746/