Java实现中序表达式转换成后序表达式+后序表达式求值实现

后序表达式的概念:又称后缀表达式,不含括号的表达式,他的运算符写在前面,操作数写在后面,也没有算术优先级问题,又称“逆波兰式”

优点:计算机从左到右扫描,不需要考虑算术优先级问题

PS:原创,如有错误,请指出,感谢指教!


(一)、中序表达式转后序表达式

步骤:

 1、先将中序表达式转换成字符数组

 2、依次取得字符数组的每一个元素,直至取完

    若该元素为数字,则添加到可变字符串str中

    若该元素为左括号,则入ops栈(运算符栈)

    若该元素为右括号,则依次取出ops栈中元素,添加到str中,直至遇到左括号,左括号出栈

    若该元素为运算符:

        当栈顶为空时,或者取出元素优先级高于或等于栈顶元素时,或者栈顶为左括号时,该元素入栈

        反之,栈顶元素出栈,添加到str中,直至满足上一条语句条件

 3、当栈顶不为空时,依次取出栈顶元素,添加到str中,直至栈顶为空

 4、最后,得到前序表达式

代码:

 1 package 表达式求值;
 2 
 3 import java.util.Stack;
 4 
 5 public class CenterToBack {
 6 
 7     public static String transform(String s){
 8         StringBuilder str = new StringBuilder(s);
 9         StringBuilder st = new StringBuilder();
10         Stack<Character> ops = new Stack<>();    //运算符栈
11         for(int i = 0; i < str.length(); i++){
12             char ch = str.charAt(i);
13             if(ch >= '0' && ch <= '9'){
14                 st.append(ch);
15             }
16             else if(ch == '('){
17                 ops.push(ch);
18             }
19             else if(ch == ')'){
20                 while(true){
21                     ch = ops.pop();
22                     if(ch == '('){
23                         break;
24                     }
25                     else{
26                         st.append(ch);
27                     }
28                 }
29             }
30             else{
31                 while(true){
32                     if(ops.isEmpty()){
33                         ops.push(ch);
34                         break;
35                     }
36                     char c = ops.peek();
37                     if(ops.peek() == '(' || ((c == '+' || c == '-') || (ch == '*' || ch == '/'))){
38                         ops.push(ch);
39                         break;
40                     }
41                     st.append(ops.pop());
42                 }
43             }
44         }
45         while(!ops.isEmpty()){
46             st.append(ops.pop());
47         }
48         return st.toString();
49     }
50     
51     public static void main(String[] args) {
52         System.out.println(transform("((1+2)*3)*5+4+(2+3)*3"));
53     }
54 }

(二)、后序表达式求值

(1)步骤:

  1、先将前序表达式转成字符数组

  2、依次取得字符数组的每一个元素(从最后一个取到第一个),直至取完

    若该元素为数字,则入num栈,

    若该元素为运算符,则将栈顶元素和下一个元素出栈,并按该运算符依次计算,将结果入num栈。

  3、最后返回num栈顶元素,该元素即为前序表达式运算结果

(2)代码:

 1 package 表达式求值;
 2 
 3 import java.util.Stack;
 4 
 5 /*
 6  * 后序表达式求值实现
 7  */
 8 public class BackExpression {
 9     public static double evaluate(String s){
10         s = CenterToBack.transform(s);    //中序表达式转换为后序表达式
11         char [] ch = s.toCharArray();
12         Stack<Double> num = new Stack<>();
13         for(int i = 0; i < ch.length; i++){
14             char c = ch[i];
15             if(c >= '0' && c <= '9'){
16                 num.push(Double.parseDouble(Character.toString(c)));
17             }
18             else{
19                 switch(c){
20                 case '+':num.push(num.pop()+num.pop());break;
21                 case '-':num.push(num.pop()-num.pop());break;
22                 case '*':num.push(num.pop()*num.pop());break;
23                 case '/':num.push(num.pop()/num.pop());break;
24                 default:break;
25                 }
26             }
27         }
28         return num.pop();
29     }
30     
31     public static void main(String [] args){
32         System.out.println(evaluate("((1+2)*3)*5+4+(2+3)*3"));
33     }
34 }

猜你喜欢

转载自www.cnblogs.com/libin-blogs/p/9445716.html