中缀转后缀、简单计算器的实现

package stack;

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

public class PolandNotation {
	public static void main(String[] args) {
		String expression = "1+((2+3)*4)-5";
		List<String> infixExpressionList = toInfixExpressionList(expression);
		System.out.println(infixExpressionList);
		List<String> parseSuffixExpressionList = parseSuffixExpressionList(infixExpressionList);
		System.out.println(parseSuffixExpressionList);
		
		System.out.printf("expression=%d",calculate(parseSuffixExpressionList));
		// 先定义逆波兰表达式
		// (3+4)*5-6--》3 4 + 5 * 6 -
		// String suffixExpression = "4 5 * 8 - 60 + 8 2 / +";
		// 先将suffixExpression放入ArrayList中
		// 将ArrayList传递给一个方法,配合栈完成计算
		// List<String> rpnList = getListString(suffixExpression);
		// int res=calculate(rpnList);
		// System.out.println(res);
	}

	public static List<String> parseSuffixExpressionList(List<String> ls) {
		Stack<String> s1 = new Stack<>();// 符号栈
		List<String> s2 = new ArrayList<>();// 中间结果
		// 遍历ls
		for (String item : ls) {
			// 如果是一个数,加入到s2
			if (item.matches("\\d+")) {
				s2.add(item);
			} else if (item.equals("(")) {
				s1.push(item);
			} else if (item.equals(")")) {
				while (!s1.peek().equals("(")) {
					s2.add(s1.pop());
				}
				s1.pop();// 将左括号弹出s1
			} else {
				// 当s1栈顶的运算符优先级小于等于栈顶运算符的优先级
				while (s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)) {
					s2.add(s1.pop());
				}
				// 将item压入栈中
				s1.push(item);
			}
		}
		//将s1剩余的运算符加入s2
		while(s1.size()!=0) {
			s2.add(s1.pop());
		}
		return s2;
	}


	// 将中缀表达式转换成对应的List
	public static List<String> toInfixExpressionList(String s) {
		// 定义一个List,存放中缀表达式对应的内容
		List<String> ls = new ArrayList<>();
		int i = 0;// 指针
		String str;// 对多位数的拼接
		char c;// 每遍历到一个字符就放入到c中
		do {
			// 如果c是一个非数字,需要加入到Ls
			if ((c = s.charAt(i)) < 48 || (c = s.charAt(i)) > 57) {
				ls.add("" + c);
				i++;
			} else {// 如果是一个数,需要考虑多位数
				str = "";
				while (i < s.length() && (c = s.charAt(i)) >= 48 && (c = s.charAt(i)) <= 57) {
					str += c;// 拼接
					i++;
				}
				ls.add(str);
			}
		} while (i < s.length());
		return ls;
	}

	public static List<String> getListString(String suffixExpression) {
		String[] split = suffixExpression.split(" ");
		List<String> list = new ArrayList<String>();
		for (String ele : split) {
			list.add(ele);
		}
		return list;
	}

	public static int calculate(List<String> ls) {
		// 创建给栈,只需要一个栈即可
		Stack<String> stack = new Stack<String>();
		// 遍历list
		for (String item : ls) {
			// 这里使用正则表达式取出数
			if (item.matches("\\d+")) {
				// 匹配的是多位数,入栈
				stack.push(item);
			} else {
				// pop出两个数,并运算,再入栈
				int num2 = Integer.parseInt(stack.pop());
				int num1 = Integer.parseInt(stack.pop());
				int res = 0;
				if (item.equals("+")) {
					res = num1 + num2;
				} else if (item.equals("-")) {
					res = num1 - num2;
				} else if (item.equals("*")) {
					res = num1 * num2;
				} else if (item.equals("/")) {
					res = num1 / num2;
				}
				// 把res入栈
				stack.push(res + "");
			}
		}
		// 最后留在stack中的数据就是运算结果
		return Integer.parseInt(stack.pop());
	}

}

// 编写一个类 Operation 可以返回一个运算符对应的优先级
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 getValue(String operation) {
		int result = 0;
		switch (operation) {
		case "+":
			result = ADD;
			break;
		case "-":
			result = SUB;
			break;
		case "*":
			result = MUL;
			break;
		case "/":
			result = DIV;
			break;
			
		case "(":
			break;
		default:
			
			System.out.println("不存在该运算符");
			break;
		}
		return result;
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值