请c语言高手帮忙改错

来源:百度知道 编辑:UC知道 时间:2024/05/24 18:46:49
题目是要用2分法求下面方程在(-10,10)之间的根;
2*x*x*x-4*x*x+3*x-6=0
我程序是这样写的:
#include <stdio.h>
#include <math.h>
#define f(x) 2*pow(x,3)-4*pow(x,2)+3*x-6
main()
{float g,h,x1=-10,x2=10,x,k;
g=f(x1);
h=f(x2);
if(g*h>0) printf("it's a wrong area\n"); /*检查区间*/
if(g*h==0)
{if(g==0) printf("the right number is %f\n",x1);
else printf("the right number is %f\n",x2);
}
else
{while(fabs(x1-x2)>1e-5)
{x=(x1+x2)/2;
k=f(x);
if(k*g>=0)
{x1=x;
g=f(x1);
}
else x2=x;
printf("%5.3f %5.3f\n",x1,x2);
}

}
getch();
}
结果报错,我用的是WIN-TC。请高手帮忙看下错在哪里

#include <stdio.h>
#include <math.h>
#define f(x) 2*pow((x),3)-4*pow((x),2)+3*(x)-6 //参数打括号

main()
{float g,h,x1=-10,x2=10,x,k;
g=f(x1);
h=f(x2);
if(g*h>0) printf("it's a wrong area\n"); /*检查区间*/
if(g*h==0)//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~英文括号
{if(g==0) printf("the right number is %f\n",x1);
else printf("the right number is %f\n",x2);
}
else
{while(fabs(x1-x2)>1e-5)
{x=(x1+x2)/2;
k=f(x);
if(k*g>=0)
{x1=x;
g=f(x1);
}
else x2=x;
printf("%5.3f %5.3f\n",x1,x2);
}

}

}

给你改完了。。。。。

if(g*h==0)括号改成半角

#include <stdio.h>
#include <math.h>
#define f(x) 2*pow(x,3)-4*pow(x,2)+3*x-6
main()
{float g,h,x1=-10,x2=10,x,k;
g=f(x1);
h=f(x2);
if(g*h>0) printf("it's a wrong area\