算法Sedgewick第四版-第1章基础-014一用stack把前置表达式转为后置表达式并计算值

1.

 1 /*************************************************************************
 2  *  Exercise 1.3.10
 3  *  
 4  *  % java InfixToPostfix
 5  *  ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
 6  *  1 2 3 + 4 5 * * +
 7  *  
 8  *  % java InfixToPostfix
 9  *  ( sqrt ( 1 + 2 ) )
10  *  1 2 + sqrt
11  *  
12  *************************************************************************/
13 
14 public class InfixToPostfix
15 {
16     public static void main(String[] args)
17     {
18         Stack<String> ops  = new Stack<String>();
19         Stack<String> vals = new Stack<String>();
20 
21         while (!StdIn.isEmpty())
22         {
23             String s = StdIn.readString();
24             
25             if      (s.equals("("))               ;
26             else if (s.equals("+") ||
27                      s.equals("-") ||
28                      s.equals("*") ||
29                      s.equals("/") ||
30                      s.equals("sqrt")) ops.push(s);
31             else if (s.equals(")"))
32             {
33                 String op = ops.pop();
34                 String v = vals.pop();
35                 
36                 if (op.equals("+") ||
37                     op.equals("-") ||
38                     op.equals("*") ||
39                     op.equals("/"))
40                     v = String.format("%s %s %s", vals.pop(), v, op);
41                 else if (op.equals("sqrt"))
42                     v = String.format("%s %s", v, op);
43                 
44                 vals.push(v);
45             }
46             else vals.push(s);
47         }
48         
49         StdOut.println(vals.pop());
50     }
51 }

 

2.

 1 /*************************************************************************
 2  *  Exercise 1.3.11
 3  *  
 4  *  % java EvaluatePostfix
 5  *  1 2 3 + 4 5 * * +
 6  *  101.0
 7  *  
 8  *  % java EvaluatePostfix
 9  *  1 5 sqrt + 2.0 /
10  *  1.618033988749895
11  *  
12  *  % java EvaluatePostfix
13  *  12 9 - 105 7 / *
14  *  45.0
15  *  
16  *  % java InfixToPostfix | java EvaluatePostfix
17  *  ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
18  *  101.0
19  *  
20  *  % java InfixToPostfix | java EvaluatePostfix
21  *  ( ( 1 + sqrt ( 5 ) ) / 2.0 )
22  *  1.618033988749895
23  *  
24  *************************************************************************/
25 
26 public class EvaluatePostfix
27 {
28     public static void main(String[] args)
29     {
30         Stack<Double> vals = new Stack<Double>();
31 
32         while (!StdIn.isEmpty())
33         {
34             String s = StdIn.readString();
35             
36             if      (s.equals("(") ||
37                      s.equals(")")) ;
38             else if (s.equals("+") ||
39                      s.equals("-") ||
40                      s.equals("*") ||
41                      s.equals("/") ||
42                      s.equals("sqrt"))
43             {
44                 double v = vals.pop();
45                 
46                 if      (s.equals("+"))    v = vals.pop() + v;
47                 else if (s.equals("-"))    v = vals.pop() - v;
48                 else if (s.equals("*"))    v = vals.pop() * v;
49                 else if (s.equals("/"))    v = vals.pop() / v;
50                 else if (s.equals("sqrt")) v = Math.sqrt(v);
51                 
52                 vals.push(v);                
53             }
54             else
55                 vals.push(Double.parseDouble(s));
56         }
57         
58         StdOut.println(vals.pop());
59     }
60 }

 

posted @ 2016-04-19 17:29  shamgod  阅读(406)  评论(0编辑  收藏  举报
haha