C++中子函数如何相互调用?源程序如下:
#include<iostream>#include<cmath>usingnamespacestd;main(){floatx1,x2,x,f1,f2;floatroo...
#include<iostream>
#include<cmath>
using namespace std;
main()
{float x1,x2,x,f1,f2;
float root(float x1, float x2);
float f(float x);
float p(float x1,float x2);
do{
cout<<"请输入两个数:";
cin>>x1>>x2;
f1=f(x1);f2=f(x2);
}while(f1*f2>0);
x=root(x1,x2);
cout<<x;
return 0;}
float root(float x1,float x2)
{float x,y,y1;
y1=f(x1);
do
{x=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
y=f(x);
if(y*y1>0)
{y1=y;x1=x;}
else
x2=x;}
while(fabs(y)>=0.0001);
return (x);}
float f(float x)
{float y;
y=x*x*x-7.8*x*x+18.5*x-11.2;
return (y);} 展开
#include<cmath>
using namespace std;
main()
{float x1,x2,x,f1,f2;
float root(float x1, float x2);
float f(float x);
float p(float x1,float x2);
do{
cout<<"请输入两个数:";
cin>>x1>>x2;
f1=f(x1);f2=f(x2);
}while(f1*f2>0);
x=root(x1,x2);
cout<<x;
return 0;}
float root(float x1,float x2)
{float x,y,y1;
y1=f(x1);
do
{x=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
y=f(x);
if(y*y1>0)
{y1=y;x1=x;}
else
x2=x;}
while(fabs(y)>=0.0001);
return (x);}
float f(float x)
{float y;
y=x*x*x-7.8*x*x+18.5*x-11.2;
return (y);} 展开
2个回答
展开全部
整理一下,就差不多看出来了:
#include<iostream>
#include<cmath>
using namespace std;
main()
{ float x1,x2,x,f1,f2;
float root(float x1, float x2);
float f(float x);
float p(float x1,float x2);
do
{
cout<<"请输入两个数:";
cin>>x1>>x2;
f1=f(x1);f2=f(x2);
}
while(f1*f2>0);
x=root(x1,x2);
cout<<x;
return 0;
}
float root(float x1,float x2)
{ float x,y,y1;
y1=f(x1);
do
{
x=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
y=f(x);
if(y*y1>0)
{y1=y;x1=x;}
else
x2=x;
}
while(fabs(y)>=0.0001);
return (x);
}
函数p调用函数f;
函数root也调用函数f;
建议把f放在main函数之前定义;
float f(float x)
{
float y;
y=x*x*x-7.8*x*x+18.5*x-11.2;
return (y);
}
#include<iostream>
#include<cmath>
using namespace std;
main()
{ float x1,x2,x,f1,f2;
float root(float x1, float x2);
float f(float x);
float p(float x1,float x2);
do
{
cout<<"请输入两个数:";
cin>>x1>>x2;
f1=f(x1);f2=f(x2);
}
while(f1*f2>0);
x=root(x1,x2);
cout<<x;
return 0;
}
float root(float x1,float x2)
{ float x,y,y1;
y1=f(x1);
do
{
x=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
y=f(x);
if(y*y1>0)
{y1=y;x1=x;}
else
x2=x;
}
while(fabs(y)>=0.0001);
return (x);
}
函数p调用函数f;
函数root也调用函数f;
建议把f放在main函数之前定义;
float f(float x)
{
float y;
y=x*x*x-7.8*x*x+18.5*x-11.2;
return (y);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询