-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostfix.java
172 lines (158 loc) · 3.38 KB
/
Postfix.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
//import java.util.Stack;
import java.util.Scanner;
public class Postfix {
//this will only work with single digit numbers
public StackAsList stack = new StackAsList();
//public Stack<Double> stack = new Stack();
public double rhs;
public double lhs;
public char operator;
public static void main(String[] args) throws StringNotWellFormedException {
Postfix post = new Postfix();
String done = post.infixToPostfix("(1+2)*3+(4^(5-6))");
System.out.println(done);
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()) {
String input = scan.nextLine();
post.calculateRPN(input);
}
}
public double evaluate (String pfx) {
double result = 0;
for(int i = 0; i < pfx.length(); i++) {
char c = pfx.charAt(i);
if(Character.isDigit(c)) {
double number = Character.getNumericValue(c);
stack.push(number);
} else {
rhs = (double) stack.pop(); // cast to double
lhs = (double) stack.pop();
if(c == '+') {
result = lhs + rhs;
stack.push(result);
}
else if(c == '-') {
result = lhs - rhs;
stack.push(result);
}
else if(c == '*') {
result = lhs * rhs;
stack.push(result);
}
else if(c == '/') {
result = lhs / rhs;
stack.push(result);
}
else if(c == '^') {
result = Math.pow(lhs, rhs);
stack.push(result);
}
}
}
return (double) stack.pop(); //cast to double
}
public char topToChar() {
String s = "" + stack.peek();
char c = s.charAt(0);
return c;
}
public boolean stringContains(String s) {
if(s.contains(" ") ||
s.contains(":") ||
s.contains(",") ||
s.contains(".") ||
s.contains(";") ||
s.contains("=")) {
return false;
} else {
return true;
}
}
public void calculateRPN(String s) throws StringNotWellFormedException {
String r = infixToPostfix(s);
double d = evaluate(r);
System.out.println(d);
}
public String infixToPostfix (String ifx) throws StringNotWellFormedException {
if(stringContains(ifx)) {
String r = "";
for(int i = 0; i < ifx.length(); i++) {
char c = ifx.charAt(i);
if(Character.isDigit(c)) {
r = r + c;
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while(topToChar() != '(') {
r = r + stack.peek();
stack.pop();
}
stack.pop();
} else {
while(checkPrecedence(c, topToChar())) {
r = r + stack.peek();
stack.pop();
}
stack.push(c);
}
}
while(!(stack.isEmpty())) {
r = r + stack.pop();
}
return r;
} else {
throw new StringNotWellFormedException ("Your String is not well formed!");
}
}
public boolean checkPrecedence(char s, char d) {
boolean prec = false;
switch(d) {
case '+':
if(s == '-' || s == '+') {
prec = true;
} else {
prec = false;
}
break;
case '-':
if(s == '-' || s == '+') {
prec = true;
} else {
prec = false;
}
break;
case '*':
if(s == '*' ||
s == '/' ||
s == '+' ||
s == '-') {
prec = true;
} else {
prec = false;
}
break;
case '/':
if(s== '/' ||
s == '*' ||
s == '+' ||
s == '-' ) {
prec = true;
} else {
prec = false;
}
break;
case '^':
if(s == '^' ||
s == '/' ||
s == '*' ||
s == '+' ||
s == '-' ) {
prec = true;
} else {
prec = false;
}
break;
}
return prec;
}
}