-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path44.cpp
48 lines (42 loc) · 1.01 KB
/
44.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <functional>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <exception>
using std::runtime_error;
#include <string>
using std::string;
#include <map>
using std::map;
int add(int i, int j){
return i+j;
}
struct divide{
int operator()(int i, int j){
try{
int ret = i/j;
return ret;
} catch(std::exception err){
cout << err.what() << "\n try again !" << endl;
int i,j;
cin >> i >> j;
return operator()(i,j);
}
}
};
auto mod = [](int i, int j){ return i % j; };
int main(){
map<string,std::function<int(int,int)>> binops = {
{"+", add},
{"/", divide()},
{"%", mod},
{"-", std::minus<int>()},
{"*", [](int i, int j){ return i*j; } }
};
cout << binops["+"](5,10) << endl;
cout << binops["/"](10,3) << endl;
cout << binops["-"](10,3) << endl;
cout << binops["*"](10,3) << endl;
cout << binops["%"](10,3) << endl;
}