C语言编程问题,找出其中的错误,并说明理由

来源:百度知道 编辑:UC知道 时间:2024/06/25 16:57:51
#include<stdio.h>
#include<stdio.h>
float f(float x) /*定义函数f,以实现f(x)=((x-5.0)*x+16.0)*x-80.0*/
{
float y;
y=((x-5.0)*x+16.0)*x-80.0;
return(y);
}
float xpoint(float x1,float x2) /*定义xpoint函数,求出弦与x轴交点*/
{
float y;
y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
return(y);
}
float root(float x1;float x2) /*定义root函数,求近视根*/
{
float x,y,y1;
y1=f(x1);
do
{
x=xpoint(x1,x2);
y=f(x);
if(y*y1>0) /*f(x)与f(x1)同符号*/
{
y1=y;
x1=x;
}
else
x2=x;
} while(fabs(y)>=0.0001);
return(x);
}
void main() /*主函数*/
{
float x1,x2,f1,f2,x;
do
{
printf("input x1,x2:\n");
scanf("%f,%f",&x1,&x2);
f1=f(x1);
f2=f(x2);
}
while(f1*f2>=0);
x=root(x1,x2);
printf("A root of equatio

一共两个错误:
1. 需要在开始#include<math.h>,因为后面的fabs库函数的声明包含在这个头文件中
2.
float root(float x1;float x2) /*定义root函数,求近视根*/
参数用,分隔,不是用;,所以改为:
float root(float x1,float x2) /*定义root函数,求近视根*/

下面是修改后的代码:
#include<stdio.h>
#include<math.h>

float f(float x) /*定义函数f,以实现f(x)=((x-5.0)*x+16.0)*x-80.0*/
{
float y;
y=((x-5.0)*x+16.0)*x-80.0;
return(y);
}
float xpoint(float x1,float x2) /*定义xpoint函数,求出弦与x轴交点*/
{
float y;
y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
return(y);
}
float root(float x1,float x2) /*定义root函数,求近视根*/ //Error1: ;->,
{
float x,y,y1;
y1=f(x1);
do
{
x=xpoint(x1,x2);
y=f(x);
if(y*y1>0) /*f(x)与f(x1)同符号*/
{
y1=y;
x1=x;
}
else
x2=x;
} while(fabs(y)>=0.0001);
return(x);
}
void main()