表达式求值

这段代码用到的是栈的方法,先将中缀表达式改为后缀表达式,再对后缀表达式求值。

不多说,看代码:

 1 import java.util.*;
 2 
 3 public class Main {
 4     static Scanner sc = new Scanner(System.in);
 5     
 6     public static int lastvalue(String str){    //后缀表达式求值
 7         Stack<Integer> stack = new Stack<Integer>();
 8         String[] data = str.split(" ");
 9          
10         for(int i = 0;i < data.length;i++){
11             if(data[i].charAt(0) >= '0' && data[i].charAt(0) <= '9'){
12                 //将data[i]转换为int数据
13                 int m =  Integer.parseInt(data[i]);
14                 stack.push(m);
15             }else{
16                 int a = stack.pop();
17                 int b = stack.pop();
18                 //构造b ? a计算结果,,将结果入栈
19                 switch(data[i].charAt(0)){
20                     case '+':stack.push(b+a);break;
21                     case '-':stack.push(b-a);break;
22                     case '*':stack.push(b*a);break;
23                     case '/':stack.push(b/a);break;
24                     case '%':stack.push(b%a);break;
25                 }
26             }
27 //          System.out.println("---list:"+stack);//测试输出栈内的情况
28         }
29         return stack.peek();    //程序运行结束后,栈内只剩一个数据,即答案
30     }
31     public static String turnvalue(String s){   //中缀变换后缀
32         Stack<String> stack = new Stack<String>();
33         StringBuffer sb = new StringBuffer();
34         int i = 0;
35         while(i < s.length()){
36             char c = s.charAt(i);
37             switch(c){
38                 case '+':case '-':    //存加减号
39                     while(!stack.isEmpty() && !stack.peek().equals("("))
40                         sb.append(stack.pop()+" ");
41                     stack.push(c + "");i++;
42                     break;
43                 case '*':case '/':    //存乘除号
44                     while(!stack.isEmpty() && (stack.peek().equals("*") || stack.peek().equals("/")))
45                         sb.append(stack.pop()+" ");
46                     stack.push(c + "");i++;
47                     break;
48                 case '(':
49                     stack.push(c + "");i++;
50                     break;
51                 case ')':
52                     while(stack.peek() != null && !stack.peek().equals("("))
53                         sb.append(stack.pop() + " ");
54                     stack.pop();
55                     i++;break;
56                 default:
57                     if(c == ';'){    //可以存放一些结束符号
58                         i++;
59                         break;
60                     }
61                     while(i < s.length() && c >='0' && c <= '9'){    //保存数字
62                         sb.append(c);i++;
63                         if(i < s.length())
64                             c = s.charAt(i);
65                     }
66                     sb.append(" ");
67             }
68         }
69         while(!stack.isEmpty())
70             sb.append(stack.pop()+" ");
71 //      System.out.println(sb);        //检查结果
72          
73         return sb.toString();
74     }
75     
76     public static void main(String[] args) {
77         while(sc.hasNext()){
78             String s = sc.next();
79             System.out.println(lastvalue(turnvalue(s)));
80         }
81         System.gc();sc.close();
82     }
83 }

 

posted @ 2018-11-26 09:52  MoonTwilight  阅读(182)  评论(0编辑  收藏  举报