#include <iostream>
using namespace std;
double calculate(double x, double y, double (*pf)(double, double)); // 函数指针
double add(double x, double y); //加法函数
double sub(double x, double y); //减法函数
double mean(double x, double y); //平均函数
int main()
{
double a, b;
double (*pf[3])(double, double) = {add, sub, mean}; // 多个函数指针的使用
char * op[3] = {"add", "sub", "mean"};
cout << "Enter pairs of numbers (q to quit): ";
while (cin >> a >> b)
{
for (int i=0; i<3; i++)
{
cout << op[i] << ": " << a << " and " << b << " = "
<< calculate(a, b, pf[i]) << endl;
}
}
}
double calculate(double x, double y, double (*pf)(double, double))
{
return (*pf)(x, y); //具体的返回值
}
double add(double x, double y)
{
return x + y;
}
double sub(double x, double y)
{
return x - y;
}
double mean(double x, double y)
{
return (x + y) / 2.0;
}
using namespace std;
double calculate(double x, double y, double (*pf)(double, double)); // 函数指针
double add(double x, double y); //加法函数
double sub(double x, double y); //减法函数
double mean(double x, double y); //平均函数
int main()
{
double a, b;
double (*pf[3])(double, double) = {add, sub, mean}; // 多个函数指针的使用
char * op[3] = {"add", "sub", "mean"};
cout << "Enter pairs of numbers (q to quit): ";
while (cin >> a >> b)
{
for (int i=0; i<3; i++)
{
cout << op[i] << ": " << a << " and " << b << " = "
<< calculate(a, b, pf[i]) << endl;
}
}
}
double calculate(double x, double y, double (*pf)(double, double))
{
return (*pf)(x, y); //具体的返回值
}
double add(double x, double y)
{
return x + y;
}
double sub(double x, double y)
{
return x - y;
}
double mean(double x, double y)
{
return (x + y) / 2.0;
}