算法 中缀表达式转后缀表达式的方法:
遇到操作数:直接输出(添加到后缀表达式中)
栈为空时,遇到运算符,直接入栈
遇到左括号:将其入栈
遇到右括号:执行出栈操作,并将出栈的元素输出,直到弹出栈的是左括号,左括号不输出。
遇到其他运算符:加减乘除:弹出所有优先级大于或者等于该运算符的栈顶元素【栈内的栈顶运算符>=遇到的运算符,就弹出】,然后将该运算符入栈
最终将栈中的元素依次出栈,输出。
实现中缀表达式转换为后缀表达式主要包含三个类,一个主函数,一个自定义栈的类,还有一个就是核心类,实现其转换。
Java 输入的表达式为:A(B+C)-D/(E+F) 执行的结果如下: 请输入算术表达式: A (B+C)-D/(E+F) for A Stack (bottom–>top): for Stack (bottom–>top): for ( Stack (bottom–>top): for B Stack (bottom–>top): ( for + Stack (bottom–>top): ( for C Stack (bottom–>top): ( + for ) Stack (bottom–>top): ( + for - Stack (bottom–>top): for D Stack (bottom–>top): - for / Stack (bottom–>top): - for ( Stack (bottom–>top): - / for E Stack (bottom–>top): - / ( for + Stack (bottom–>top): - / ( for F Stack (bottom–>top): - / ( + for ) Stack (bottom–>top): - / ( + While Stack (bottom–>top): - / While Stack (bottom–>top): - end while!! Stack (bottom–>top): Profix is ABC+ DEF+/-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 public class MyStack { private int maxSize; private char [] ch; private int top; public MyStack (int s) { maxSize = s; ch = new char [s]; top = -1 ; } public void push (char c) { ch[++top] = c; } public char pop () { return ch[top--]; } public char peek () { return ch[top]; } public boolean isEmpty () { return top == -1 ; } public boolean isFull () { return top == (maxSize - 1 ); } public int size () { return top + 1 ; } public char get (int index) { return ch[index]; } public void display (String str) { System.out.print(str); System.out.print(" Stack (bottom-->top): " ); for (int i = 0 ; i < size(); i++) { System.out.print(get(i)+" " ); } System.out.println(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 public class InToPost { private MyStack ms; private String input; private String output="" ; public InToPost (String input) { this .input = input; int size = input.length(); ms = new MyStack(size); } public String doTrans () { for (int i = 0 ; i < input.length(); i++) { char ch = input.charAt(i); ms.display("for " + ch + " " ); switch (ch) { case '+' : case '-' : getOper(ch, 1 ); break ; case '*' : case '/' : getOper(ch, 2 ); break ; case '(' : ms.push(ch); break ; case ')' : getParent(ch); break ; default : output = output + ch; break ; } } while (!ms.isEmpty()){ ms.display("While " ); output=output+ms.pop(); } ms.display("end while!!" ); return output; } public void getParent (char ch) { while (!ms.isEmpty()){ char chx=ms.pop(); if (chx=='(' ){ break ; }else { output=output+chx; } } } public void getOper (char ch, int prec1) { while (!ms.isEmpty()) { char operTop = ms.pop(); if (operTop == '(' ) { ms.push(operTop); break ; } else { int prec2; if (operTop == '+' || operTop == '-' ) { prec2 = 1 ; } else { prec2 = 2 ; } if (prec2 <prec1) { ms.push(operTop); break ; } else { output = output + operTop; } } } ms.push(ch); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class InfixMain { public static void main (String[] args) { String input, output; while (true ) { input = getString(); if ("" .equals(input) || input == null ) { break ; } InToPost itp = new InToPost(input); output = itp.doTrans(); System.out.println("Profix is " + output + "\n" ); } } public static String getString () { String output = "" ; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("请输入算术表达式:" ); output = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return output; } }