您的位置:首页 > 编程语言 > C语言/C++

c++ 函数指针使用实例

2017-01-10 17:57 871 查看
#include <iostream>
double add(double x , double y)
{
return x + y;
}
double sub(double x , double y)
{
return x - y;
}
double acc(double x , double y)
{
return x * y;
}
double calculate(double x , double y , double (*fun)(double ,double))
{
return (*fun)(x , y);
}
int main()
{
using namespace std;
double (*fun[3])(double , double) = {add , sub , acc};
const char (*guugle[3]) = {"sum" , "diff" , "accumlate"};
double a , b;
cout << "Enter pairs of numbers (q to quit):" << endl;
int i;
while(cin >> a >> b)
{
for(i = 0 ; i < 3 ; i++)
{
cout << guugle[i] << " = " << calculate(a , b , fun[i]) << endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: