Stack练习:: 中缀-后缀表达式

package Stack.Calculate;

import java.util.List;

public class calculation {
	private String expression = null;

	private InfixToPostfix inf = null;

	private PostfixEval pos = null;

	public calculation(String s) {
		expression = s;
		expression += "@";
	}

	public int calculat() {
		int result = 0;
		// 转化成后缀表达式
		inf = new InfixToPostfix(expression);
		List<String> list = inf.toPostfix();

		// 后缀表达式求值
		pos = new PostfixEval(list);
		result = pos.evaluate();
		return result;
	}

	public static void main(String[] args) {
		calculation c = new calculation("(300 + 200) / 100");
		System.out.println(c.calculat()); // 5
	}

}


中缀转化成后缀表达式
package Stack.Calculate;

import java.util.*;

public class InfixToPostfix {

	private String infixExp = null;

	private List<String> postfixExp = new ArrayList<String>();

	private Stack<Character> operStack = new Stack<Character>();

	private StringBuffer numBuf = new StringBuffer();

	private int priority(char op) {
		switch (op) {
		case '+':
		case '-':
			return 1;
		case '*':
		case '/':
			return 2;
		case '(':
		case '@':
			return 0;
		}
		return -1;
	}

	public InfixToPostfix(String s) {
		infixExp = s;
	}

	public List<String> toPostfix() {
		char ch, preChar;
		operStack.push('@');

		for (int i = 0; i < infixExp.length();) {
			ch = infixExp.charAt(i);
			switch (ch) {
			case '+':
			case '-':
			case '*':
			case '/':
				preChar = operStack.peek();
				while (priority(preChar) >= priority(ch)) {
					postfixExp.add("" + preChar);
					operStack.pop();
					preChar = operStack.peek();
				}
				operStack.push(ch);
				i++;
				break;
			case '(':
				operStack.push(ch);
				i++;
				break;
			case ')':
				char c = operStack.pop();
				while (c != '(') {
					postfixExp.add("" + c);
					c = operStack.pop();
				}
				i++;
				break;
			case '@':
				char c1;
				while (!operStack.isEmpty()) {
					c1 = operStack.pop();
					if (c1 != '@')
						postfixExp.add("" + c1);
				}
				i++;
				break;
			case ' ':
			case '\t':
				i++;
				break;
			default:
				if (Character.isDigit(ch)) {
					while (Character.isDigit(ch)) {
						numBuf.append(ch);
						ch = infixExp.charAt(++i);
					}
					postfixExp.add("" + numBuf);
					numBuf = new StringBuffer();
				} else {
					System.err.println("Illegal operator");
				}

			} // switch end~
		}

		return postfixExp;
	} // toPostfix end~

	/*
	 * public static void main(String[] args) { InfixToPostfix in = new
	 * InfixToPostfix("50 + 2 * 3@"); List<String> list = in.toPostfix();
	 * System.out.println(list); }
	 */

}


后缀表达式求值
package Stack.Calculate;

import java.util.*;

public class PostfixEval {
	private Stack<String> operandStack;

	private List<String> list;

	private boolean isOperator(String s) {
		return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/");
	}

	private int getOperand() {
		/*
		 * if (operandStack.isEmpty()) throw new
		 * ArithmeticException("PostfixEval: Too many operators");
		 */

		return Integer.parseInt(operandStack.pop());
	}

	private int compute(int left, int right, String item) {
		if (item.equals("+"))
			return left + right;
		else if (item.equals("-"))
			return left - right;
		else if (item.equals("*"))
			return left * right;
		else {
			if (right == 0)
				throw new ArithmeticException("PostfixEval: divide by 0");
			return left / right;
		}
	}

	public PostfixEval(List<String> list) {
		this.list = list;
		this.operandStack = new Stack<String>();
	}

	public int evaluate() {
		int left, right, expValue = 0;
		String item;
		for (int i = 0; i < list.size(); i++) {
			item = list.get(i);
			if (isOperator(item)) {
				right = getOperand();
				left = getOperand();
				expValue = compute(left, right, item);
				operandStack.push("" + new Integer(expValue));
			} else {
				operandStack.push(item);
			}
		}

		/*
		 * if (!operandStack.isEmpty()) throw new
		 * ArithmeticException("PostfixEval: Too many operands");
		 */
		return Integer.parseInt(operandStack.pop());
	}

}

你可能感兴趣的:(C++,c,C#)