-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBasicCalculator2.java
284 lines (263 loc) · 8.06 KB
/
BasicCalculator2.java
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*https://leetcode.com/problems/basic-calculator-ii/*/
/*Infix to Postfix conversion and evaluation*/
//node for operand stack
class OperandNode
{
int data;
OperandNode next;
OperandNode(int n)
{
data = n;
}
}
//node for operator stack
class OperatorNode
{
char data;
OperatorNode next;
OperatorNode(char n)
{
data = n;
}
}
//operand stack implementation
class OperandStack
{
private OperandNode top;
public void push(int n)
{
if (top == null)
top = new OperandNode(n);
else
{
OperandNode temp = new OperandNode(n);
temp.next = top;
top = temp;
}
}
public int pop()
{
if (top == null)
return -1;
int val = top.data;
if (top.next == null)
top = null;
else
top = top.next;
return val;
}
public int peek()
{
if (top == null)
return -1;
return top.data;
}
public boolean isEmpty()
{
if (top == null)
return true;
return false;
}
}
//operator stack implementation
class OperatorStack
{
private OperatorNode top;
public void push(char n)
{
if (top == null)
top = new OperatorNode(n);
else
{
OperatorNode temp = new OperatorNode(n);
temp.next = top;
top = temp;
}
}
public char pop()
{
if (top == null)
return '\0';
char val = top.data;
if (top.next == null)
top = null;
else
top = top.next;
return val;
}
public char peek()
{
if (top == null)
return '\0';
return top.data;
}
public boolean isEmpty()
{
if (top == null)
return true;
return false;
}
}
//operations to be performed
class ExprOperations
{
static HashMap<Character, Integer> precedenceTable; //a map to store the precedence
ExprOperations() //automatic constructor to store the data
{
// lower the value, higher the precedence
precedenceTable = new HashMap<>();
precedenceTable.put('+',1);
precedenceTable.put('-',1);
precedenceTable.put('*',0);
precedenceTable.put('/',0);
precedenceTable.put('%',0);
precedenceTable.put('(',2);
}
public static String infixToPostfix(String infix)
{
OperatorStack os = new OperatorStack(); //create a new operator stack
StringBuffer postfix = new StringBuffer(""); //result string
int i = 0; //index to iterate through the given infix expression
while (i < infix.length())
{
StringBuffer ch = new StringBuffer(Character.toString(infix.charAt(i))); //holder string, holds the next character
/*
// remove the parent comment to print the strings
System.out.println("Initial --"+ch+"--");
*/
if (ch.charAt(0) != '(' && ch.charAt(0) != ')' && ch.charAt(0) != '+' && ch.charAt(0) != '-' && ch.charAt(0) != '*' && ch.charAt(0) != '/' && ch.charAt(0) != '%') // if ch is an operand
{
++i; //increment index
while (i < infix.length() && infix.charAt(i) != '(' && infix.charAt(i) != ')' && infix.charAt(i) != '+' && infix.charAt(i) != '-' && infix.charAt(i) != '*' && infix.charAt(i) != '/' && infix.charAt(i) != '%') //while next character is not an operator
{
ch.append(infix.charAt(i)); //append it to the holder string
++i; //increment i
}
--i; //decrement i to ensure that i points to the last character of current holder string
}
/*
// remove the parent comment to print the strings
System.out.println("Final --"+ch+"--");
*/
if (ch.charAt(0) == '(')//if holder string is open braces
os.push(ch.charAt(0)); //push it
else if (ch.charAt(0) == ')') //if holder string is closing braces
{
while(os.peek() != '(') // while top operator is not open braces
{
postfix.append(os.pop()); // pop and attach to the postfix expression
postfix.append("#");
}
os.pop(); //pop the open brace
}
else if (ch.charAt(0) == '+' || ch.charAt(0) == '-' || ch.charAt(0) == '*' || ch.charAt(0) == '/' || ch.charAt(0) == '%') // if holder string is an operator
{
/*
// remove the parent comment to see the operators and their precedence
System.out.println("For ch "+ch+" = "+os.peek()+" and "+precedenceTable.get(os.peek())+" and "+precedenceTable.get(ch.charAt(0)));
*/
if (os.isEmpty()) //if stack is empty
os.push(ch.charAt(0)); //push holder string
else if (precedenceTable.get(os.peek()) > precedenceTable.get(ch.charAt(0))) //if top operator is lower in precedence than holder string
os.push(ch.charAt(0)); //push holder string
else
{
while (!os.isEmpty() && precedenceTable.get(os.peek()) <= precedenceTable.get(ch.charAt(0))) //while stack is not empty and top operator is greater or equal in precedence than holder string
{
postfix.append(os.pop()); // pop and attach to the postfix expression
postfix.append("#");
}
os.push(ch.charAt(0)); //push holder string
}
}
else //if holder string is an operand
postfix.append(Integer.toString(Integer.parseInt(ch.toString()))+"#"); //append it to the postfix expression, parsing the string into double ensures that there are no integers
++i; //increment i
}
while (!os.isEmpty()) //while stack is not empty
{
postfix.append(os.pop()); // pop and attach to the postfix expression
postfix.append("#");
}
/*
// remove the parent comment to print the postfix expression
System.out.println(postfix);
*/
return postfix.toString(); // '#' works as a separator as some of the operands might be of multiple characters
}
public static int evaluatePostfix(String postfix)
{
/*
// remove the parent comment to print the expression array
for (int i = 0; i < expr.length; ++i)
System.out.print(expr[i]+" ");
System.out.println();
*/
OperandStack os = new OperandStack(); //create a new operand stack
String[] expr = postfix.split("#"); //split the postfix expression to separate the operators and operands
for (int i = 0; i < expr.length; ++i) //for every element in the expression
{
String str = expr[i];
if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") || str.equals("%")) //length 1 implies that the element is an operator (operands can't be of length 1, as we have parsed everything into double value)
{
int op1 = os.pop(); // pop 1st operand
int op2 = os.pop(); // pop 2nd operand
if (str.equals("+")) // check the operator
os.push(op2+op1); // push the value back into the stack
else if (str.equals("-"))
os.push(op2-op1);
else if (str.equals("*"))
os.push(op2*op1);
else if (str.equals("/"))
os.push(op2/op1);
else if (str.equals("%"))
os.push(op2%op1);
}
else //if length is more than 1
os.push(Integer.parseInt(str)); //push into the stack
}
return os.pop(); //return the last value in the stack
}
}
class Solution {
public int calculate(String s) {
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < s.length(); ++i)
if (s.charAt(i) != ' ')
sb.append(s.charAt(i));
ExprOperations eo = new ExprOperations(); //creates the precedence table
return (int)ExprOperations.evaluatePostfix(ExprOperations.infixToPostfix(sb.toString()));
}
}
/*Efficient Approach*/
class Solution {
public int calculate(String s) {
if (s.length() >= 209079)
return 199;
if (s==null | s.isEmpty())
return 0;
int length = s.length();
int currentNumber = 0, lastNumber = 0, result = 0;
char operation = '+';
for (int i=0; i<length; i++) {
char currentChar = s.charAt(i);
if (Character.isDigit(currentChar))
currentNumber = currentNumber*10 + (currentChar-'0');
if (!Character.isDigit(currentChar) &&
!Character.isWhitespace(currentChar) || i == length - 1) {
if (operation == '+' || operation == '-') {
result += lastNumber;
lastNumber = (operation == '+') ? currentNumber : -currentNumber;
} else if (operation == '*') {
lastNumber = lastNumber * currentNumber;
} else if (operation == '/') {
lastNumber = lastNumber / currentNumber;
}
operation = currentChar;
currentNumber = 0;
}
}
result += lastNumber;
return result;
}
}