中缀表达式转后缀表达式
Posted 番茄疯了
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了中缀表达式转后缀表达式相关的知识,希望对你有一定的参考价值。
规则:相同等级的运算符,栈内高于栈外 栈内的要出栈
站外的左括号优先级最高 入栈
入栈后左括号的优先级最低
站外的右括号优先级最低 低到和栈内的左括号优先级相同
//站的应用 /*中缀表达式转换成后缀表达式 中缀表达式strMid:2+3*5-4*(5-3) 后缀表达式strMid:235*+453-*- * * * * */ package com.tulun; import java.util.Arrays; public class TestMl3 { class Constant { /** * 表示加法 */ public static final int OPERATORS_PRIO_PLUS_IN = 4; //栈内加法 public static final int OPERATORS_PRIO_SUB_IN = 4; //栈内减法 public static final int OPERATORS_PRIO_MULTY_IN = 2; //栈内乘法 public static final int OPERATORS_PRIO_DIV_IN = 2 ; //栈内除法 public static final int OPERATORS_PRIO_LEFT_BRAK_IN = 10; //栈内左括号 public static final int OPERATORS_PRIO_PLUS_OUT = 5 ; //栈外加法 public static final int OPERATORS_PRIO_SUB_OUT = 5; //栈外减法 public static final int OPERATORS_PRIO_MULTY_OUT = 3; //栈外乘法 public static final int OPERATORS_PRIO_DIV_OUT = 3; //栈外除法 public static final int OPERATORS_PRIO_LEFT_BRAK_OUT = 1; //栈外左括号 public static final int OPERATORS_PRIO_RIGHT_BRAK_OUT = 10; //栈外右括号 public static final int OPERATORS_PRIO_ERROR = -1; } public static int getPrio(char opera,boolean instack){ int prio = Constant.OPERATORS_PRIO_ERROR; if(instack){ switch(opera) { case ‘+‘: prio = Constant.OPERATORS_PRIO_PLUS_IN; break; case ‘-‘: prio = Constant.OPERATORS_PRIO_SUB_IN; break; case ‘*‘: prio = Constant.OPERATORS_PRIO_MULTY_IN; break; case ‘/‘: prio = Constant.OPERATORS_PRIO_DIV_IN; break; case ‘(‘: prio = Constant.OPERATORS_PRIO_LEFT_BRAK_IN; break; default: prio = Constant.OPERATORS_PRIO_ERROR; break; } }else{ switch(opera){ case ‘+‘: prio = Constant.OPERATORS_PRIO_PLUS_OUT; break; case ‘-‘: prio = Constant.OPERATORS_PRIO_SUB_OUT; break; case ‘*‘: prio = Constant.OPERATORS_PRIO_MULTY_OUT; break; case ‘/‘: prio = Constant.OPERATORS_PRIO_DIV_OUT; break; case ‘(‘: prio = Constant.OPERATORS_PRIO_LEFT_BRAK_OUT; break; case ‘)‘: prio = Constant.OPERATORS_PRIO_RIGHT_BRAK_OUT; break; default: prio = Constant.OPERATORS_PRIO_ERROR; break; } } return prio; } public static void strMidToLast(String strMid,char[]strLast){ //模拟栈 char[]stack = new char[strMid.length()]; int top=0; int len=strMid.length(); int i=0;//计数 用来计strMid的下标 int j=0;//计数 用来表示strMid的下标 int prioIn;//栈内的优先级 int prioOut;//栈外的优先级 //遍历strMid while(i!=len){ if(Character.isDigit(strMid.charAt(i))){ strLast[j]=strMid.charAt(i); j++; i++; }else{ if(top==0){//栈为空 stack[top]=strMid.charAt(i); top++; i++; }else{ prioIn=getPrio(stack[top-1],true); prioOut=getPrio(strMid.charAt(i),false); if(prioIn<prioOut){ strLast[j++]=stack[--top]; } else if(prioIn==prioOut){ top--; i++; }else{ stack[top++]=strMid.charAt(i); i++; } } } } while(top>0){ strLast[j++]=stack[--top]; } } public static void show(char[]strLast){ for(int i=0;i<strLast.length;i++){ System.out.println(strLast[i]); } System.out.println(); } public static void main(String[] args) { // TODO Auto-generated method stub String strMid="2+3*5-4*(5-3)"; char[]strLast=new char[strMid.length()]; strMidToLast(strMid,strLast); System.out.println(Arrays.toString(strLast)); } }
以上是关于中缀表达式转后缀表达式的主要内容,如果未能解决你的问题,请参考以下文章