JAVA 字符表达式 解析

尝试着,用JAVA进行字符串表达式解析计算。

居然没有得出个更好的写法,甚是郁闷。

鼓捣出下面一个类,只能求解不带括弧的表达式,看来数据结构中的关于表达式的算法,当初是没好好实现,到现在也没整出个比较好的方案。

package test.java;

import java.util.Stack;

public class MathUtil {

	public static final char add = '+';
	public static final char substract = '-';
	public static final char divide = '/';
	public static final char multiply = '*';

	public static final char leftParenthesis = '(';
	public static final char rightParenthesis = ')';

	public static final int isParenthesis = 2;

	public static final int isOperator = 1;

	public static final int isNotOperator = 0;

	public static String exec(String expression) {
		Stack<String> operatorStack = new Stack<String>();
		Stack<String> operandStack = new Stack<String>();
		int i = 0;

		StringBuffer operandBuffer = new StringBuffer();
		while (i < expression.length()) {
			char ch = expression.charAt(i);
			int result = checkOperator(ch);

			if (Character.isDigit(ch)) {
				operandBuffer.append(ch);
				// 运算数入栈
				if (i + 1 == expression.length()) {
					operandStack.push(operandBuffer.toString());
					operandBuffer.delete(0, operandBuffer.length());
				}

			} else if (result == isOperator) {
				// 运算数入栈
				if (operandBuffer.length() > 0) {
					operandStack.push(operandBuffer.toString());
					operandBuffer.delete(0, operandBuffer.length());
				}

				processStack(operatorStack, operandStack);

				// 运算符入栈
				operatorStack.push(Character.toString(ch));

			} else if (result == isParenthesis) {
				// 运算数入栈
				if (operandBuffer.length() > 0) {
					operandStack.push(operandBuffer.toString());
					operandBuffer.delete(0, operandBuffer.length());
				} 
				// 括弧入栈
				operatorStack.push(Character.toString(ch));
			}

			i++;
		}

		// 末尾栈处理
		processStack(operatorStack, operandStack);

		// 出空栈
		while (!operatorStack.isEmpty()) {
			processEndStack(operatorStack, operandStack);
		}
		return operandStack.pop();
	}

	public static void processEndStack(Stack<String> operatorStack,
			Stack<String> operandStack) {

		if (operatorStack.isEmpty())
			return;

		char operator = operatorStack.pop().charAt(0);
		int opearand1 = Integer.parseInt(operandStack.pop());
		int opearand2 = Integer.parseInt(operandStack.pop());
		String previewOperator = "@";
		if (!operatorStack.isEmpty()) {
			previewOperator = operatorStack.pop();
		}
		if (previewOperator.charAt(0) == MathUtil.substract) {
			opearand2 = -opearand2;
			operatorStack.push(Character.toString(MathUtil.add));
		} else if (!previewOperator.equals("@")) {
			operatorStack.push(previewOperator);
		}
		if (operator == MathUtil.add) {
			opearand1 = opearand2 + opearand1;

		} else if (operator == MathUtil.substract) {

			opearand1 = opearand2 - opearand1;
		} else {
			// 恢复栈
			operandStack.push(Integer.toString(opearand2));
			operatorStack.push(Character.toString(operator));
		}
		// 结果入栈
		operandStack.push(Integer.toString(opearand1));
	}

	public static void processStack(Stack<String> operatorStack,
			Stack<String> operandStack) {
		if (operatorStack.isEmpty())
			return;

		char operator = operatorStack.pop().charAt(0);
		int opearand1 = Integer.parseInt(operandStack.pop());
		int opearand2 = Integer.parseInt(operandStack.pop());
		if (operator == MathUtil.divide) {
			opearand1 = opearand2 / opearand1;

		} else if (operator == MathUtil.multiply) {
			opearand1 = opearand1 * opearand2;

		} else {
			// 恢复栈
			operandStack.push(Integer.toString(opearand2));
			operatorStack.push(Character.toString(operator));
		}
		// 结果入栈
		operandStack.push(Integer.toString(opearand1));

	}

	public static int checkOperator(char operator) {

		if (MathUtil.add == operator) {
			return MathUtil.isOperator;
		} else if (MathUtil.substract == operator) {
			return MathUtil.isOperator;
		} else if (MathUtil.divide == operator) {
			return MathUtil.isOperator;
		} else if (MathUtil.multiply == operator) {
			return MathUtil.isOperator;
		} else if (MathUtil.leftParenthesis == operator
				|| MathUtil.rightParenthesis == operator) {
			return MathUtil.isParenthesis;
		} else {
			return MathUtil.isNotOperator;
		}

	}
}

你可能感兴趣的:(java,数据结构,算法)