You are on page 1of 1

package ics202.

lab04;

import ics202.StackAsLinkedList;

public class Postfix extends StackAsLinkedList {

StackAsLinkedList s = new StackAsLinkedList();

public void input(Object obj) {


String f = (String) obj;
int x;
int y;
for (int i = 0; i < f.length(); i++) {
if (Character.isDigit(f.charAt(i))) {
s.push(Character.getNumericValue(f.charAt(i)));
} else {
switch (f.charAt(i)) {
case '+':
x = (int) s.pop();

y = (int) s.pop();

s.push(x + y);
break;
case '-':
x = (int) s.pop();

y = (int) s.pop();

s.push(y - x);
break;
case '/':
x = (int) s.pop();

y = (int) s.pop();

s.push(y / x);
break;
case '*':
x = (int) s.pop();

y = (int) s.pop();

s.push(x * y);
break;

}
}

}
System.out.println(s.getTop());
}

You might also like