-
Notifications
You must be signed in to change notification settings - Fork 246
/
6.55.cpp
52 lines (43 loc) · 1.04 KB
/
6.55.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
49
50
51
52
#include <vector>
#include <iostream>
#include <stdexcept>
int add(int, int);
int subtract(int, int);
int multiply(int, int);
int divide(int, int);
int main() {
std::vector<int (*)(int, int)> vf;
vf.push_back(add);
vf.push_back(subtract);
vf.push_back(multiply);
vf.push_back(divide);
int a, b;
std::cin >> a >> b;
for (const auto &e : vf) {
std::cout << e(a, b) << std::endl;
}
return 0;
}
int add(int a, int b) {
std::cout << "Called add(" << a << ", " << b << ")" << std::endl;
return a + b;
}
int subtract(int a, int b) {
std::cout << "Called subtract(" << a << ", " << b << ")" << std::endl;
return a - b;
}
int multiply(int a, int b) {
std::cout << "Called multiply(" << a << ", " << b << ")" << std::endl;
return a * b;
}
int divide(int a, int b) {
std::cout << "Called divide(" << a << ", " << b << ")" << std::endl;
try {
if (b == 0)
throw std::runtime_error("Divide by 0.");
return a / b;
} catch (std::runtime_error err) {
std::cerr << err.what() << std::endl;
return 0;
}
}