用堆栈和波兰表达式实现整数计算器

package stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

//波兰表达式
public class PolandCalcultor {
	public static void main(String[] args) {
		PolandCalcultor poland = new PolandCalcultor();
		List<String> list = poland.toFixExpressionList("40+50*3-2/1");
//		System.out.println(list);
		List<String> list1 = poland.parseSuffixExpression(list);
//		System.out.println(list1);
		System.out.println(poland.calculate(list1));
	}

	public List<String> toFixExpressionList(String s) {
		List<String> fixExpressionList = new ArrayList<String>();
		int i = 0;// 相当于指针
		String str;
		char c;
		do {
			c = s.charAt(i);
			if (c < 48 || c > 57) {
				fixExpressionList.add("" + c);
				i++;
			} else {
				str = "";
				while (i < s.length()) {
					c = s.charAt(i);
					if (c < 48 || c > 57) {
						break;
					}
					str += c;
					i++;

				}
				fixExpressionList.add(str);
			}

		} while (i < s.length());

		return fixExpressionList;
	}

//	首先需要分配2个栈,一个作为临时存储运算符的栈S1(含一个结束符号),一个作为输入逆波兰式的栈S2(空栈),S1栈可先放入优先级最低的运算符#,注意,中缀式应以此最低优先级的运算符结束。可指定其他字符,不一定非#不可。从中缀式的左端开始取字符,逐序进行如下步骤:
//	(1)若取出的字符是操作数,则分析出完整的运算数,该操作数直接送入S2栈
//	(2)若取出的字符是运算符,则将该运算符与S1栈栈顶元素比较,如果该运算符优先级(不包括括号运算符)大于S1栈栈顶运算符优先级,则将该运算符进S1栈,否则,将S1栈的栈顶运算符弹出,送入S2栈中,直至S1栈栈顶运算符低于(不包括等于)该运算符优先级,最后将该运算符送入S1栈。
//	(3)若取出的字符是“(”,则直接送入S1栈顶。
//	(4)若取出的字符是“)”,则将距离S1栈栈顶最近的“(”之间的运算符,逐个出栈,依次送入S2栈,此时抛弃“(”。
//	(5)重复上面的1~4步,直至处理完所有的输入字符
//	(6)若取出的字符是“#”,则将S1栈内所有运算符(不包括“#”),逐个出栈,依次送入S2栈。
//	完成以上步骤,S2栈便为逆波兰式输出结果。不过S2应做一下逆序处理。便可以按照逆波兰式的计算方法计算了!
	public List<String> parseSuffixExpression(List<String> list) {

		Stack<String> stack1 = new Stack<String>();
//		Stack<String> stack2 = new Stack<String>();
		List<String> stack2 = new ArrayList<String>();
		for (String str : list) {

			if (str.matches("\\d+")) {

				stack2.add(str);
			} else if (str.equals("(")) {
				stack1.push(str);
			} else if (str.equals(")")) {
				while (!stack1.peek().equals("(")) {
					stack2.add(stack1.pop());
				}
				stack1.pop();
			} else {

				while (stack1.size() != 0 && Operation.getOperation(stack1.peek()) >= Operation.getOperation(str)) {
					stack2.add(stack1.pop());

				}
				stack1.push(str);
			}
		}
		while (stack1.size() != 0) {
			stack2.add(stack1.pop());
		}

		return stack2;
	}

	public int calculate(List<String> list) {

		int result = 0;
		Stack<String> stack = new Stack<>();
		for (String item : list) {
			if (item.matches("\\d+")) {
				stack.push(item);

			} else {
				int num1 = Integer.parseInt(stack.pop());
				int num2 = Integer.parseInt(stack.pop());

				int res = 0;
				switch (item) {
				case "+":
					res = num2 + num1;
					break;
				case "-":
					res = num2 - num1;
					break;
				case "*":
					res = num1 * num2;
					break;
				case "/":
					res = num2 / num1;
					break;

				default:

					System.out.println("操作符有误");
					break;
				}

				stack.push("" + res);

			}
		}
		result = Integer.parseInt(stack.pop());
		return result;
	}
}

class Operation {
	private static int ADD = 1;
	private static int SUB = 1;
	private static int MUL = 2;
	private static int DIV = 2;

	public static int getOperation(String value) {
		switch (value) {
		case "*":
			return MUL;
		case "/":
			return DIV;
		case "+":
			return ADD;
		case "-":
			return SUB;
		default:
			return -1;
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值