使用函数指针
#include <iostream>
#include <string>
#include <map>
#include <functional>
using namespace std;
enum cal_type {addop, minusop, multiop, divop};
map<const string, double(*)(int,int)> cal;
map<const string, cal_type> switcher;
double add_method(int a, int b) {
return a+b;
}
double Minus_method(int a, int b) {
return a-b;
}
double multi_method(int a, int b) {
return a*b;
}
double div_method(int a, int b) {
return a/b;
}
int main() {
cal.insert(pair<const string, double(*)(int, int)>("add", add_method));
cal.insert(pair<const string, double(*)(int, int)>("minus", Minus_method));
cal.insert(pair<const string, double(*)(int, int)>("multi", multi_method));
cal.insert(pair<const string, double(*)(int, int)>("div", div_method));
switcher.insert(pair<const string, cal_type>("add", cal_type::addop));
switcher.insert(pair<const string, cal_type>("minus", cal_type::minusop));
switcher.insert(pair<const string, cal_type>("multi", cal_type::multiop));
switcher.insert(pair<const string, cal_type>("div", cal_type::divop));
string ret;
int first, second;
cout << "Enter the method and the values" << endl;
while (cin >> ret >> first >> second) {
switch (switcher[ret])
{
case cal_type::addop:
cout << cal[ret](first, second) << endl;
break;
case cal_type::minusop:
cout << cal[ret](first, second) << endl;
break;
case cal_type::multiop:
cout << cal[ret](first, second) << endl;
break;
case cal_type::divop:
cout << cal[ret](first, second) << endl;
break;
default:
cout << "No match operator" << endl;
break;
}
}
return 0;
}
注意map cal中的second是函数指针,有*,而不是直接double(int, int),这是一个函数类型,而不是函数指针类型。
使用function:
#include <iostream>
#include <string>
#include <map>
#include <functional>
using namespace std;
enum cal_type {addop, minusop, multiop, divop};
using func = function<double(int,int)>;
map<const string, func> cal;
map<const string, cal_type> switcher;
double add_method(int a, int b) {
return a+b;
}
double Minus_method(int a, int b) {
return a-b;
}
double multi_method(int a, int b) {
return a*b;
}
double div_method(int a, int b) {
return a/b;
}
int main() {
cal.insert(pair<const string, func>("add", add_method));
cal.insert(pair<const string, func>("minus", Minus_method));
cal.insert(pair<const string, func>("multi", multi_method));
cal.insert(pair<const string, func>("div", div_method));
switcher.insert(pair<const string, cal_type>("add", cal_type::addop));
switcher.insert(pair<const string, cal_type>("minus", cal_type::minusop));
switcher.insert(pair<const string, cal_type>("multi", cal_type::multiop));
switcher.insert(pair<const string, cal_type>("div", cal_type::divop));
string ret;
int first, second;
cout << "Enter the method and the values" << endl;
while (cin >> ret >> first >> second) {
switch (switcher[ret])
{
case cal_type::addop:
cout << cal[ret](first, second) << endl;
break;
case cal_type::minusop:
cout << cal[ret](first, second) << endl;
break;
case cal_type::multiop:
cout << cal[ret](first, second) << endl;
break;
case cal_type::divop:
cout << cal[ret](first, second) << endl;
break;
default:
cout << "No match operator" << endl;
break;
}
}
return 0;
}