7.10(1)
#include<iostream>
using namespace std;
double calculate(double aa, double bb, double(*pf)(double x ,double y));
double add(double x, double y);
double Minus(double x, double y);
int main()
{
double q = calculate(2.5, 10.4, add);
cout << q << endl;
double a, b;
while (cin >> a >> b)
{
char choice;
cout << "A for add , M for minus";
cin >> choice;
switch (choice)
{
case 'A':
case 'a':cout << calculate(a, b, add) << endl; break;
case'M':
case'm':cout << calculate(a, b, Minus) << endl; break;
default:cin >> choice;
}
}
}
double calculate(double aa, double bb, double(*pf)(double x, double y))
{
return (*pf)(aa , bb) ;
}
double add(double x, double y)
{
return x + y;
}
double Minus(double x, double y)
{
return x - y;
}
7.10(2)
#include<iostream>
using namespace std;
double add(double x, double y);
int main()
{
double(*pf[3])(double , double );
int i;
double a, b;
for (i = 0; i < 3; i++)
{
cin >> a >> b;
pf[i] = add;
cout << pf[i](a, b);
}
return 0;
}
double add(double x, double y)
{
return x + y;
}