前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >中缀表达式转后缀表达式并求值

中缀表达式转后缀表达式并求值

作者头像
shengjk1
发布2020-02-21 11:42:35
4960
发布2020-02-21 11:42:35
举报
文章被收录于专栏:码字搬砖码字搬砖

1.什么是中缀表达式?

中缀表达式示例

2.什么是后缀表达式?

后缀表达式示例

3.代码

package xmht.datastructuresandalgorithms.datastructure.stack;

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

/**
 * @author shengjk1
 * @date 2020/2/16
 */
/*
仅仅适用于 int 类型计算
 */
public class PolandNotation {
	public static void main(String[] args) {
		//中缀表达式转后缀表达式
		String expression = "1+((2+3*2/1+2-1)*4)-5";
		//将中缀表达式对应的list转化为后缀表达式对应的list
		List<String> parseSuffixExpression = parseSuffixExpression(toInFiExpressionList(expression));
		System.out.println("parseSuffixExpression: " + parseSuffixExpression);
		
		System.out.println(calculate(parseSuffixExpression));
		
	}
	
	/*
	中缀表达式转后缀表达式
1) 初始化两个栈,运算符栈 S1 存储中间结果栈 S2
2)从左至右扫描中缀表达式
3)当遇到操作数,将其压入 S2 栈
4)如果遇到运算符,比较其与 S1 栈顶的优先级
1.如果S1为空,或栈顶运算符为"(",则直接将此运算符入栈
2.否则,若优先级比栈顶运算符高,也将其压入 S1 栈
3.否则,将 S1 栈顶的运算符弹出并压入到 S2 中,
再次转到 4.1 与 S1 中新的栈顶运算符相比较
5)遇到括号时:
1.如果是左括号"(",则直接压入 S1
2.如果是右括号")“,则依次弹出 S1 栈顶的运算符,并压入 S2,直到遇到左括号为止,此时将这一对括号丢弃
6)重复步骤2至5,直到表达式的最右边
7)将S1中剩余的运算符依次弹出并压入 S2
8)依次弹出S2中元素并输出,结果的逆序即为中缀表达式对应的后缀表达式
	 */
	public static List<String> parseSuffixExpression(List<String> ls) {
		//定义两个栈
		Stack<String> s1 = new Stack<>();
		//因为 S2 这个栈在整个转换过程中,没有 pop 操作,而且后面我们还需要逆序输出,会比较麻烦,因此这里用 list
		ArrayList<String> s2 = new ArrayList<>();
		
		for (String item : ls) {
			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();
			} else {
				//当 s1 栈顶的运算符优先级大于等 item 的优先级,将 s1 栈顶弹出并加入 s2
				while (s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)) {
					s2.add(s1.pop());
				}
				s1.push(item);
			}
		}
		
		//将 s1 中剩余的运算符依次弹出加入 s2 中
		while (s1.size() != 0) {
			s2.add(s1.pop());
		}
		
		return s2;
	}
	
	
	//将中缀表达式转化成对应的list
	public static List<String> toInFiExpressionList(String s) {
		ArrayList<String> ls = new ArrayList<>();
		int i = 0;
		//多位数拼接
		String str;
		char c;
		do {
			//非数字
			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;
	}
	
	
	//将逆波兰表达式转化为list
	public static List<String> getListString(String suffixExpression) {
		String[] split = suffixExpression.split(" ");
		ArrayList<String> list = new ArrayList<>();
		
		for (String s : split) {
			list.add(s);
		}
		return list;
	}
	
	
	/**
	 * 逆波兰表达式 求值
	 * <p>
	 * 从左至右扫描表达式,
	 * 遇到数字,将数字压入堆栈,
	 * 遇到运算符,弹出栈顶的两个数,
	 * 用运算符对它们做相应的计算(次栈顶元素和栈顶元素),并将结果入栈。
	 * 重复上述过程直到表达式最右端,最后运算得出的值即为表达式的结果。
	 *
	 * @param s
	 * @return
	 */
	public static int calculate(List<String> s) {
		Stack<String> stack = new Stack<>();
		for (String item : s) {
			if (item.matches("\\d+")) {
				stack.push(item);
			} else {
				int num1 = Integer.parseInt(stack.pop());
				int num2 = Integer.parseInt(stack.pop());
				int res = 0;
				if (item.equals("+")) {
					res = num2 + num1;
				} else if (item.equals("-")) {
					res = num2 - num1;
				} else if (item.equals("*")) {
					res = num2 * num1;
				} else if (item.equals("/")) {
					res = num2 / num1;
				} else {
					throw new RuntimeException("运算符有误");
				}
				stack.push("" + res);
			}
		}
		return Integer.parseInt(stack.pop());
	}
}


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;
			default:
				System.out.println("不存在运算符");
				break;
		}
		return result;
	}
	
}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档