-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex7.cpp
71 lines (56 loc) · 1.39 KB
/
ex7.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;
void printGreeting() {
cout << "Welcome to MyCalculator!" << endl;
cout << "App developed by António Bezerra." << endl;
}
char getOperator() {
cout << "Input an operator (+, -, *, /) - m to use last result - x to quit: ";
char op;
cin >> op;
return op;
}
double getNum() {
double num;
cout << "Please input a number: ";
cin >> num;
return num;
}
double calculateResult(double num1, double num2, char op) {
double result;
if (op == '+') {
result = num1 + num2;
} else if (op == '-') {
result = num1 - num2;
} else if (op == '*') {
result = num1 * num2;
} else if (op == '/') {
if (num2 == 0) {
cout << "Invalid operation!" << endl;
return -1;
}
result = num1 / num2;
}
return result;
}
int main() {
printGreeting();
double last_result = 0;
while (true) {
char op = getOperator();
double num1;
if (op == 'x') {
break;
} else if (op == 'm') {
num1 = last_result;
op = getOperator();
} else {
num1 = getNum();
}
double num2 = getNum();
double result = calculateResult(num1, num2, op);
cout << "Result: " << result << endl;
last_result = result;
}
return 0;
}