-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsimple_calculator.mj
111 lines (105 loc) · 2.53 KB
/
simple_calculator.mj
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
program SimpleCalculator
const char SPACE = ' ';
const char EQUALS = '=';
class Calculator {
int operand1;
int operand2;
int result;
{
void setOperand1(int operand) {
this.operand1 = operand;
}
void setOperand2(int operand) {
this.operand2 = operand;
}
int generateResult() {
return 0;
}
void calculate() {
this.result = this.generateResult();
}
int getOperand1() {
return this.operand1;
}
int getOperand2() {
return this.operand2;
}
int getResult() {
return this.result;
}
}
}
class AdditionCalculator extends Calculator {
{
int generateResult() {
return this.operand1 + this.operand2;
}
}
}
class SubtractionCalculator extends Calculator {
{
int generateResult() {
return this.operand1 - this.operand2;
}
}
}
class MultiplicationCalculator extends Calculator {
{
int generateResult() {
return this.operand1 * this.operand2;
}
}
}
class DivisionCalculator extends Calculator {
{
int generateResult() {
return this.operand1 / this.operand2;
}
}
}
{
void main()
int operand1, operand2;
char operator;
int numExpressions;
Calculator calculator;
AdditionCalculator additionCalculator;
SubtractionCalculator subtractionCalculator;
MultiplicationCalculator multiplicationCalculator;
DivisionCalculator divisionCalculator;
{
numExpressions = 4;
additionCalculator = new AdditionCalculator;
subtractionCalculator = new SubtractionCalculator;
multiplicationCalculator = new MultiplicationCalculator;
divisionCalculator = new DivisionCalculator;
do {
read(operand1);
read(operator);
read(operand2);
if (operator == '+') {
calculator = additionCalculator;
} else if (operator == '-') {
calculator = subtractionCalculator;
} else if (operator == '*') {
calculator = multiplicationCalculator;
} else if (operator == '/') {
calculator = divisionCalculator;
}
calculator.setOperand1(operand1);
calculator.setOperand2(operand2);
calculator.calculate();
print(operand1);
print(SPACE);
print(operator);
print(SPACE);
print(operand2);
print(SPACE);
print(EQUALS);
print(SPACE);
print(calculator.getResult());
print(eol);
numExpressions--;
} while (numExpressions > 0);
}
}