7.10
#include
using namespace std;
double add(double a,double b){return a+b;};//为了简化,这三个函数我直接在这里写了。
double dev(double a,double b){return a-b;};
double mul(double a,double b){return a*b;};
double calculate(double a,double b,double (*pf[])(double x,double y));
void main710()
{
double (*pf[3])(double,double)={add,dev,mul};
double result=calculate(2.5,4.0,pf);
cout<<”\nresult=”<<result;
system(“pause”);
}
double calculate(double a,double b,double (*pf[])(double x,double y))
{
int size=sizeof(pf);//获取里面有多少个函数
cout<<”size=”<<size<<endl;//测试到size=4,可能是最后一个‘\0’
cout<<”The result of “<<a<<” and b:”<<endl;//这里少了个<<b,请自行加上去—-2015.01.20
double sum=0;
for(int i=0;i<size-1;i++)
{
//cout<<”now i=”<<i<<endl;
//cout<<”The result of “<<a<<” and b:”<<endl;
cout<<pf[i](a,b)<<endl;
sum+=pf[i](a,b);
}
return sum;
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/cprimerplus第六版课后编程题答案710/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!