forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.44.cpp
38 lines (32 loc) · 787 Bytes
/
14.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
#include <functional>
#include <iostream>
#include <map>
#include <string>
int add(int a, int b) {
return a + b;
}
struct multiple {
int operator()(int a, int b) { return a * b; }
};
auto divide = [](int a, int b) { return a / b; };
int main() {
std::map<std::string, std::function<int (int, int)>> ops = {
{ "+", add },
{ "-", std::minus<int>() },
{ "*", multiple() },
{ "/", divide },
{ "%", [](int a, int b) { return a % b; } }
};
int a, b;
std::string op;
do {
std::cout << "Enter expression: ";
std::cin >> a >> op >> b;
auto it = ops.find(op);
if (it != ops.end())
std::cout << it->second(a, b) << std::endl;
else
std::cout << "Unrecognized operator: " << op << std::endl;
} while (std::cin);
return 0;
}