package esimene; import java.util.Stack; public class RpnCalculator { Stack st=new Stack(); public void setAccumulator(int i) { if(st.size()>0){st.pop();} st.push(i); } public void plus() { st.push(popFromStack()+ popFromStack()); // enter(); } public void enter() { st.push(getAccumulator()); } public int getAccumulator() { if(st.size()>0){return st.peek();} return 0; } private int popFromStack(){ if(st.size()>0){return st.pop();} return 0; } public void multiply() { st.push(popFromStack() * popFromStack()); //enter(); } public void calculate(String data) { for (String element : data.split("\\s")) { if ("+".equals(element)) { plus(); enter(); } else if ("*".equals(element)) { multiply(); enter(); } else { setAccumulator(Integer.parseInt(element)); enter(); } } } } /* public class RpnCalculator { Stack st=new Stack(); int acc; public void setAccumulator(int i) { acc=i; } public void plus() { setAccumulator(st.pop()+ getAccumulator()); } public void enter() { st.push(acc); } public int getAccumulator() { return acc; } } */