主题:关于C的函数嵌套
书上说“在定义函数时,一个函数内不能包含另一个函数”
但是这段代码,标为红色的部分,不就是在定义函数的时候包含了另一个函数吗?
不太明白,请大虾指点!多谢!
#include<math.h>
float f(float x)
{
float y;
y=((x-5.0)*x+16.0)*x-80.0;
return(y);
}
float xpoint(float x1,floatx2)
{
float y;
[color=FF0000]y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));[/color]
return(y);
}
float root(float x1,float x2)
{
float x,y,y1;
[color=FF0000]y1=f(x1);[/color]
do
{
[color=FF0000]x=xpiont(x1,x2);
y=f(x);[/color]
if(y*y1>0)
{y1=y;
x1=x;}
else
x2=x;
}while(fabs(y)>=0.0001);
return(x);
}
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 equation is%8.4f",x);
}
但是这段代码,标为红色的部分,不就是在定义函数的时候包含了另一个函数吗?
不太明白,请大虾指点!多谢!
#include<math.h>
float f(float x)
{
float y;
y=((x-5.0)*x+16.0)*x-80.0;
return(y);
}
float xpoint(float x1,floatx2)
{
float y;
[color=FF0000]y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));[/color]
return(y);
}
float root(float x1,float x2)
{
float x,y,y1;
[color=FF0000]y1=f(x1);[/color]
do
{
[color=FF0000]x=xpiont(x1,x2);
y=f(x);[/color]
if(y*y1>0)
{y1=y;
x1=x;}
else
x2=x;
}while(fabs(y)>=0.0001);
return(x);
}
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 equation is%8.4f",x);
}