练习14.44
编写一个简单的桌面计算器,使其能处理二元运算。
解答:
#include <iostream>
#include <functional>
#include <map>
using namespace std;
map<string, function<double(double, double)>> binops1 = {
{"+", plus<double>()},
{"-", minus<double>()},
{"/", divides<double>()},
{"*", multiplies<double>()}
};
map<string, function<int(int, int)>> binops2 ={
{"%", modulus<int>()}
};
int main(){
cout << binops1["+"](90.38, 89) << endl;
cout << binops1["-"](39, 90.39) << endl;
cout << binops1["/"](89.3, 39.2) << endl;
cout << binops1["*"](83.89, 28.2) << endl;
cout << binops2["%"](289, 389) << endl;
}
想要交互的话,可以再添一些交互的语句进去。