Postfix Expression Evaluator
来源:互联网 发布:下载格式化工厂软件 编辑:程序博客网 时间:2024/05/16 07:22
- /**
- * filename: PostfixEvaluator.java
- * package:
- * author: Li Ma
- * email: nick.ma85@yahoo.com
- * date: Oct 3, 2008 (created)
- * Nov 2, 2008 (modified)
- * description: this class evaluates postfix expression.
- */
- import java.util.*;
- public class PostfixEvaluator {
- /** the stack stores operands */
- private Stack<Integer> operandStack;
- /** each operator(char) has a integer value of priority */
- private static final Map<Character, Integer> OPERATORS;
- /** postfix expression */
- private String postfix;
- static {
- // initialize the static field
- OPERATORS = new HashMap<Character, Integer>();
- OPERATORS.put('+', 1);
- OPERATORS.put('-', 1);
- OPERATORS.put('*', 2);
- OPERATORS.put('/', 2);
- }
- /**
- * description: the constructor with fields
- */
- public PostfixEvaluator(String postfix) {
- // TODO Auto-generated constructor stub
- operandStack = new Stack<Integer>();
- this.postfix = postfix;
- }
- /**
- * description: determine whether the character is an operator
- * @param ch - the character to be tested
- * @return true if ch is an operator
- */
- private boolean isOperator(char ch) {
- return OPERATORS.containsKey(ch);
- }
- /**
- * description: evaluate the current operation, poping the two operands off
- * the operand stack and applying the operator.
- * @param op - a character representing the operator
- * @return the result of applying the operator
- */
- private int evalOp(char op) {
- // pop the two operands off the stack
- int rhs = operandStack.pop();
- int lhs = operandStack.pop();
- int result = 0;
- // evaluate the operator
- switch(op) {
- case '+':
- result = lhs + rhs;
- break;
- case '-':
- result = lhs - rhs;
- break;
- case '*':
- result = lhs * rhs;
- break;
- case '/':
- result = lhs / rhs;
- break;
- }
- return result;
- }
- /**
- * description: evaluate the whole infix expression
- * @return the value of the infix expression
- * @throws SyntaxErrorException
- */
- public int eval() throws SyntaxErrorException {
- // process each token
- StringTokenizer tokens = new StringTokenizer(this.postfix);
- try {
- while(tokens.hasMoreTokens()) {
- String next = tokens.nextToken();
- if(Character.isDigit(next.charAt(0))) {
- int value = Integer.parseInt(next);
- // push value onto operand stack
- operandStack.push(value);
- } else if(isOperator(next.charAt(0))) {
- // it is an operator
- // evaluate the operator
- int result = evalOp(next.charAt(0));
- operandStack.push(result);
- } else {
- throw new SyntaxErrorException("Invalid character encountered");
- }
- }
- // no more tokens, pop result from operand stack
- int answer = operandStack.pop();
- if(operandStack.empty()) {
- return answer;
- } else {
- // indicate syntax error
- throw new SyntaxErrorException("Stack should be empty");
- }
- } catch(EmptyStackException e) {
- throw new SyntaxErrorException("the stack is empty");
- }
- }
- }
- /**
- * filename: SyntaxErrorException.java
- * package:
- * author: Li Ma
- * email: nick.ma85@yahoo.com
- * date: Oct 3, 2008
- * description: this exception shows a syntax error.
- */
- public class SyntaxErrorException extends Exception {
- private static final long serialVersionUID = 1L;
- public SyntaxErrorException(final String message) {
- super(message);
- }
- }
- Postfix Expression Evaluator
- Infix Expression Evaluator
- Runtime C# Expression Evaluator
- An expression evaluator
- POJ-3337 Expression Evaluator-表达式求值
- Internal error in the expression evaluator
- Java数学表达式计算(Expression Evaluator)
- Evaluate Postfix Expression
- Evaluate Postfix Expression
- 4-5-Evaluate Postfix Expression
- [DS][3-19]Evaluate a postfix expression
- Technique 1000. Infix, Prefix, Postfix Expression.
- (字符串的处理4.7.22)POJ 3337 Expression Evaluator(解析C风格的字符串)
- .NET平台开源项目速览(8)Expression Evaluator表达式计算组件使用
- Parsing a postfix expression in C#, my naive approach
- 前缀式转中缀式Convert prefix to postfix expression
- Uncaught ReferenceError: Invalid left-hand side expression in postfix operation
- evaluator.html
- 由获取”贪食蛇“的资源得出一个vc调试的小技巧
- 谁动了你的cookie
- Linux做代理
- 网站制作设计时图像的设计
- 网站设计诀窍和几点小体会
- Postfix Expression Evaluator
- 重返西安
- 程序人员和前台美工设计的配合经验谈
- Binary Tree
- 用ENTER键切换控件焦点,并模拟按键
- 主页制作动态效果技术小结
- 网页重构、标准、布局研究分析
- 关于xml
- 子网掩码划分