?? listpostfixevaluator.java
字號:
//************************************************************// ListPostfixEvaluator.java Authors: Lewis/Chase// // Provides an evaluator for integer postfix expressions// presented as a list.//************************************************************import jss2.*;import java.util.Iterator;public class ListPostfixEvaluator{ /********************************************************** Constructor **********************************************************/ public ListPostfixEvaluator() { } /********************************************************** Returns the result of a postfix expression **********************************************************/ public int evaluate(ArrayUnorderedList<String> tokenList) { ArrayStack<Integer> inStack = new ArrayStack<Integer>(); int result, toPush=0,operand1=0, operand2=0; char tempChar; String tempToken; while (tokenList.size() > 0) { tempToken = tokenList.removeFirst(); /** operator of length greater than 1 */ if(tempToken.length()>1) inStack.push(new Integer(Integer.parseInt(tempToken))); else if(tempToken.length()==1) { tempChar = tempToken.charAt(0); /** if operator */ if(tempChar >= '0' && tempChar <= '9') inStack.push(new Integer(Integer.parseInt(tempToken))); /** if operand */ else if( tempToken.equals("*")|| tempToken.equals("/") || tempToken.equals("+")|| tempToken.equals("-") ) { /** get operator/operands for calculation */ operand2 = inStack.pop(); operand1 = inStack.pop(); tempChar = tempToken.charAt(0); /** calculate */ switch (tempChar) { case '*': toPush = operand1 * operand2; break; case '/': toPush = operand1 / operand2; break; case '+': toPush = operand1 + operand2; break; case '-': toPush = operand1 - operand2; break; } inStack.push(new Integer(toPush)); } } } return (inStack.pop()); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -