-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
192 lines (156 loc) · 5.66 KB
/
Copy pathParser.java
File metadata and controls
192 lines (156 loc) · 5.66 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.util.ArrayList;
import java.util.Stack;
import java.lang.Exception;
/**
* Class for calculating simple math expressions.
* Supported operators are +, -, *, /, also brackets are available.
*
* @author pbsphp
*/
public class Parser {
/**
* Constructor of Parser. Gets expression as string and remembers it.
* Not calculates it in constructor.
*
* @param expression
*/
public Parser(String expression) {
this.expression = expression;
}
/**
* Calculates expression and returns result as number.
*
* @return calculated expression as number
*
* @throws UnsupportedOperationException if expresison
* contains unsupported things, e.g. functions.
* @throws ArithmeticException if expression is invalid
* (zero division or unclosed brackets).
*/
public double calculate() throws Exception {
ArrayList<Token> tokensList = getTokensAsList();
ArrayList<Token> tokensStack = backPolishNotation(tokensList);
Stack<Double> operandsStack = new Stack<Double>();
for (Token operand : tokensStack) {
if (operand.isNumber()) {
operandsStack.push(operand.toNumber());
}
else if (operand.isOperator()) {
double result = calculateForOperator(operand, operandsStack);
operandsStack.push(result);
}
}
return operandsStack.pop();
}
ArrayList<Token> getTokensAsList() {
boolean inIdentifier = false;
String currentToken = new String();
ArrayList<Token> tokens = new ArrayList<Token>();
for (char c : this.expression.toCharArray()) {
if (Character.isWhitespace(c)) {
inIdentifier = false;
}
else if (Character.isAlphabetic(c) || Character.isDigit(c)) {
inIdentifier = true;
currentToken += c;
}
else {
inIdentifier = false;
if (!currentToken.isEmpty()) {
tokens.add(new Token(currentToken));
currentToken = new String();
}
currentToken += c;
tokens.add(new Token(currentToken));
currentToken = new String();
}
}
if (!currentToken.isEmpty()) {
tokens.add(new Token(currentToken));
}
return tokens;
}
ArrayList<Token> backPolishNotation(ArrayList<Token> arrayList) throws Exception {
ArrayList<Token> outputQueue = new ArrayList<Token>();
Stack<Token> bufferStack = new Stack<Token>();
for (Token token : arrayList) {
if (token.isNumber()) {
outputQueue.add(token);
}
else if (token.isFunction()) {
throw new UnsupportedOperationException("Functions are not supported");
}
else if (token.isSeparator()) {
throw new UnsupportedOperationException("Functions are not supported");
}
else if (token.isOperator()) {
Token op1 = token;
while (!bufferStack.empty() && bufferStack.peek().isOperator()) {
Token op2 = bufferStack.peek();
if (op1.isLeftAssoc() && op1.getPriority() <= op2.getPriority()
|| op1.isRightAssoc() && op1.getPriority() < op2.getPriority()) {
outputQueue.add(bufferStack.pop());
}
else {
break;
}
}
bufferStack.push(token);
}
else if (token.isLeftBracket()) {
bufferStack.push(token);
}
else if (token.isRightBracket()) {
while (!bufferStack.empty() && !bufferStack.peek().isLeftBracket()) {
outputQueue.add(bufferStack.pop());
}
if (bufferStack.empty() || !bufferStack.peek().isLeftBracket()) {
throw new ArithmeticException("Invalid brackets");
}
bufferStack.pop();
}
}
while (!bufferStack.empty()) {
Token token = bufferStack.pop();
if (token.isRightBracket() || token.isLeftBracket()) {
throw new ArithmeticException("Invalid brackets");
}
outputQueue.add(token);
}
return outputQueue;
}
private double calculateForOperator(Token operator, Stack<Double> arguments) throws Exception {
double resultNumber = 0;
double operand1 = 0;
double operand2 = 0;
switch (operator.toString()) {
case "+":
operand2 = arguments.pop();
operand1 = arguments.pop();
resultNumber = operand1 + operand2;
break;
case "-":
operand2 = arguments.pop();
operand1 = arguments.pop();
resultNumber = operand1 - operand2;
break;
case "*":
operand2 = arguments.pop();
operand1 = arguments.pop();
resultNumber = operand1 * operand2;
break;
case "/":
operand2 = arguments.pop();
operand1 = arguments.pop();
if (operand2 == 0.0) {
throw new ArithmeticException("Zero division");
}
resultNumber = operand1 / operand2;
break;
default:
throw new UnsupportedOperationException("Operator is not supported");
}
return resultNumber;
}
String expression;
}