forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostfix.java
More file actions
128 lines (113 loc) · 3.78 KB
/
postfix.java
File metadata and controls
128 lines (113 loc) · 3.78 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
/**
* Book: Data Structures and Algorithms in Java, by Robert LaFore
* Chapter 4:
* postfix.java
* parses postfix arithmetic expressions
* to compile this code: javac postfix.java
* to run this program: java PostfixApp
* Examples for testing:
* 5+7 -> 57+ -> 12
* 2+3*4 -> 234*+ -> 14
* 3*(4+5)-6/(1+2) -> 345+*612+/- -> 25
*/
import java.io.*; // for I/O
class StackX {
private int maxSize;
private int[] stackArray;
private int top;
public StackX(int s) { // constructor
maxSize = s;
stackArray = new int[maxSize];
top = -1;
}
public void push(int j) { // put item on top of stack
stackArray[++top] = j;
}
public int pop() { // take item from top of stack
return stackArray[top--];
}
public int peek() { // peek at top of stack
return stackArray[top];
}
public boolean isEmpty() { // true if stack is empty
return (top == -1);
}
public int size() { // return size
return top+1;
}
public int peekN(int n) { // return item at index n
return stackArray[n];
}
public void displayStack(String s) {
System.out.print(s);
System.out.print("Stack (bottom-->top): ");
for (int j=0; j<size(); j++) {
System.out.print(peekN(j));
System.out.print(' ');
}
System.out.println("");
}
} // end class StackX
class ParsePost {
private StackX theStack;
private String input;
public ParsePost(String s) {
input = s;
}
public int doParse() {
theStack = new StackX(20); // make new stack
int j, num1, num2, interAns;
char ch;
for (j=0; j<input.length(); j++) { // for each char,
ch = input.charAt(j); // read from input
theStack.displayStack(""+ch+" "); // *diagnostic*
if (ch >= '0' && ch <= '9') // if it's a number
theStack.push((int)(ch-'0')); // push it
else { // it's an operator
num2 = theStack.pop(); // pop operands
num1 = theStack.pop();
switch(ch) { // do arithmetic
case '+':
interAns = num1 + num2;
break;
case '-':
interAns = num1 - num2;
break;
case '*':
interAns = num1 * num2;
break;
case '/':
interAns = num1 / num2;
break;
default:
interAns = 0;
}
theStack.push(interAns); // push result
}
}
interAns = theStack.pop(); // get answer
return interAns;
}
} // end class ParsePost
class PostfixApp {
public static void main(String[] args) throws IOException {
String input;
int output;
while(true) {
System.out.print("Enter postfix: ");
System.out.flush();
input = getString(); // read a string from kbd
if (input.equals("")) // quit if [Enter]
break;
// make a parser
ParsePost aParser = new ParsePost(input);
output = aParser.doParse(); // do the evaluation
System.out.println("Evaluates to " + output);
}
}
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
return br.readLine();
}
}