-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic Calculator II.java
More file actions
126 lines (104 loc) · 3.71 KB
/
Basic Calculator II.java
File metadata and controls
126 lines (104 loc) · 3.71 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
class Solution {
public int calculate(String s) {
//remove unneeded spaces
s = s.replaceAll("\\s+","");
//convert to postfix notation
String[] postfixS = infixToPostfix(s).split(" ");
//evaluate Reverse polish notation
int result = evalRPN(postfixS);
//return result
return result;
}
//operator precedence
public int Prec(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
return -1;
}
public String infixToPostfix(String exp)
{
// initializing empty String for result
StringBuffer result = new StringBuffer();
// initializing empty stack
Stack<Character> stack = new Stack<>();
//problem here
for (int i = 0, length = exp.length(); i<length; i++)
{
char c = exp.charAt(i);
// If the scanned character is an
// operand, add it to output.
if (Character.isDigit(c))
{
//append " " inorder to split the reverse polish notation string and store it in charArray later
if(!stack.isEmpty() && !Character.isDigit(exp.charAt(i-1))){
// result+=" ";
result.append(" ");
}
// result += c;
result.append(c);
}
else // an operator is encountered
{
while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek())){
result.append(" ");
result.append(stack.pop());
}
// result = result + " " + stack.pop();
stack.push(c);
}
}
// pop all the operators from the stack
while (!stack.isEmpty()){
result.append(" ");
result.append(stack.pop());
}
// result = result + " " + stack.pop();
return result.toString();
}
public int evalRPN(String[] tokens)
{
//stack to store the operands
Stack<Integer>operands = new Stack<>();
boolean isOperatorFound = false;
//iterate through the string array and push numbers on the stack, if the given string is an operator then pop two numbers from the stack and calculate the result then push it back on the stack, iterate until we get to end of the string array
for(int i=0; i<tokens.length; i++)
{
if(tokens[i].equals("+") || tokens[i].equals("-") || tokens[i].equals("*") || tokens[i].equals("/") )
{
isOperatorFound = true;
int b = operands.pop();
int a = operands.pop();
char opr = tokens[i].charAt(0);
switch(opr)
{
case '+':
operands.push(a+b);
break;
case '-':
operands.push(a-b);
break;
case '*':
operands.push(a*b);
break;
case '/':
operands.push(a/b);
break;
}
}
else
operands.push(Integer.parseInt(tokens[i]));
}
//store the final result
int result = operands.pop();
//return the final result
return result;
}
}