-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
171 lines (153 loc) · 5.25 KB
/
Calculator.java
File metadata and controls
171 lines (153 loc) · 5.25 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.yijie.stack;
public class Calculator {
//Stack based calculator
public static void main(String[] args) {
String expression = "111+2*3-2";
//Create a number stack and an operator stack for storing numbers and operators respectively
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(10);
//define the relevant variables
int index = 0;//for scanning
int num1 = 0;
int num2 = 0;
int oper = 0;
int res = 0;
char ch = ' ';
String keepNum = "";
// while loop to scan the expression
while (true) {
ch = expression.substring(index, index + 1).charAt(0);
//execute the corresponding response according to ch
if (operStack.isOper(ch)) {
if (!operStack.isEmpty()) {
//If current element is an operator and its priority level lower than or equal to the operand in the stack,
//pop last two operands from number stack, apply operator and push the result back to the stack
if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
//push the calculation result to the number stack
numStack.push(res);
operStack.push(ch);
} else {
operStack.push(ch);
}
} else {
operStack.push(ch);
}
} else {
//if number, push to the numStack
//handling the multi-digit numbers
keepNum += ch;
if (index == expression.length() - 1) {
numStack.push(Integer.parseInt(keepNum));
} else {
if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
numStack.push(Integer.parseInt(keepNum));
//RESET keepNum!!!
keepNum = "";
}
}
}
//index + 1, determine if to the last element of the expression
index++;
if (index >= expression.length()) {
break;
}
}
//if the scan of the expression is over,
// pop the items sequentially from the number stack and operator stack, execute the calculation
while (true) {
//if the operator stack is empty, then there will be only one number in the number stack
if (operStack.isEmpty()) {
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1, num2, oper);
numStack.push(res);
}
System.out.printf("expression %s=%d\n", expression, numStack.pop());
}
}
//create a stack
class ArrayStack2 {
private int maxSize;//size of the stack
private int[] stack;//stack
private int top = -1;//initialize top to -1
//constructor
public ArrayStack2(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//return the item at the top of the stack but not pop
public int peek() {
return stack[top];
}
//determine if the stack is full
public boolean isFull() {
return top == maxSize - 1;
}
//determine if the stack is empty
public boolean isEmpty() {
return top == -1;
}
//push
public void push(int value) {
if (isFull()) {
System.out.println("the stack is full!");
return;
}
top++;
stack[top] = value;
}
//pop
public int pop() {
if (isEmpty()) {
//throw an exception
throw new RuntimeException("stack is empty!");
}
int value = stack[top];
top--;
return value;
}
//return the priority levels of the operators, the priority levels are expressed with the numbers
//the greater the number, the higher the priority
public int priority(int oper) {
if (oper == '*' || oper == '/') {
return 1;
} else if (oper == '+' || oper == '-') {
return 0;
} else {
return -1;//suppose we only have +, -, * /
}
}
//determine if it is an operator
public boolean isOper(char val) {
return val == '+' || val == '-' || val == '*' || val == '/';
}
//calculating methodology
public int cal(int num1, int num2, int oper) {
//store the calculating result
int res = 0;
switch (oper) {
case '+':
res = num1 + num2;
break;
case '-':
res = num2 - num1;
break;
case '*':
res = num1 * num2;
break;
case '/':
res = num2 / num1;
break;
default:
break;
}
return res;
}
}