算法Sedgewick第四版-第1章基础-007一用两个栈实现简单的编译器

1.

 1 package algorithms.util;
 2
 3 import algorithms.ADT.Stack;
 4
 5 /******************************************************************************
 6  *  Compilation:  javac Evaluate.java
 7  *  Execution:    java Evaluate
 8  *  Dependencies: Stack.java
 9  *
10  *  Evaluates (fully parenthesized) arithmetic expressions using
11  *  Dijkstra‘s two-stack algorithm.
12  *
13  *  % java Evaluate
14  *  ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
15  *  101.0
16  *
17  *  % java Evaulate
18  *  ( ( 1 + sqrt ( 5 ) ) / 2.0 )
19  *  1.618033988749895
20  *
21  *
22  *  Note: the operators, operands, and parentheses must be
23  *  separated by whitespace. Also, each operation must
24  *  be enclosed in parentheses. For example, you must write
25  *  ( 1 + ( 2 + 3 ) ) instead of ( 1 + 2 + 3 ).
26  *  See EvaluateDeluxe.java for a fancier version.
27  *
28  *
29  *  Remarkably, Dijkstra‘s algorithm computes the same
30  *  answer if we put each operator *after* its two operands
31  *  instead of *between* them.
32  *
33  *  % java Evaluate
34  *  ( 1 ( ( 2 3 + ) ( 4 5 * ) * ) + )
35  *  101.0
36  *
37  *  Moreover, in such expressions, all parentheses are redundant!
38  *  Removing them yields an expression known as a postfix expression.
39  *  1 2 3 + 4 5 * * +
40  *
41  *
42  ******************************************************************************/
43
44 public class Evaluate {
45     public static void main(String[] args) {
46         Stack<String> ops  = new Stack<String>();
47         Stack<Double> vals = new Stack<Double>();
48
49         while (!StdIn.isEmpty()) {
50             String s = StdIn.readString();
51             if      (s.equals("("))               ;
52             else if (s.equals("+"))    ops.push(s);
53             else if (s.equals("-"))    ops.push(s);
54             else if (s.equals("*"))    ops.push(s);
55             else if (s.equals("/"))    ops.push(s);
56             else if (s.equals("sqrt")) ops.push(s);
57             else if (s.equals(")")) {
58                 String op = ops.pop();
59                 double v = vals.pop();
60                 if      (op.equals("+"))    v = vals.pop() + v;
61                 else if (op.equals("-"))    v = vals.pop() - v;
62                 else if (op.equals("*"))    v = vals.pop() * v;
63                 else if (op.equals("/"))    v = vals.pop() / v;
64                 else if (op.equals("sqrt")) v = Math.sqrt(v);
65                 vals.push(v);
66             }
67             else vals.push(Double.parseDouble(s));
68         }
69         StdOut.println(vals.pop());
70     }
71 }
时间: 2024-11-18 10:39:28

算法Sedgewick第四版-第1章基础-007一用两个栈实现简单的编译器的相关文章

算法Sedgewick第四版-第1章基础-001递归

一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归,因为递归代码比相应的非递归代码更加简洁优雅.易懂.下面这种实现中的注释就言简意赅地说明了代码的作用.我们可以用数学归纳法证明这段注释所解释的算法的正确性.我们会在 3.1 节中展开这个话题并为二分查找提供一个这样的证明.编写递归代码时最重要的有以下三点.? 递归总有一个最简单的情况——方法的第一条语

算法Sedgewick第四版-第1章基础-008一用数组实现固定长度的栈

1 package algorithms.ADT; 2 3 /****************************************************************************** 4 * Compilation: javac FixedCapacityStackOfStrings.java 5 * Execution: java FixedCapacityStackOfStrings 6 * Dependencies: StdIn.java StdOut.

算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

1. 1 package algorithms.analysis14; 2 3 import algorithms.util.StdOut; 4 import algorithms.util.StdRandom; 5 6 /****************************************************************************** 7 * Compilation: javac DoublingTest.java 8 * Execution: jav

算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 1 package algorithms.elementary21; 2 3 /****************************************************************************** 4 * Compilation: javac Insertion.java 5 * Execution: java I

算法Sedgewick第四版-第1章基础-018一解决不能声明泛型数组的两咱方法(强转或反射)

1. 1 /****************************************************************************** 2 * Compilation: javac ResizingArrayStackWithReflection.java 3 * Execution: java ResizingArrayStackWithReflection < input.txt 4 * Dependencies: StdIn.java StdOut.jav

算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

一. 1. 2. 3. 二.代码 1 package algorithms.mergesort22; 2 3 import algorithms.util.StdIn; 4 import algorithms.util.StdOut; 5 6 /****************************************************************************** 7 * Compilation: javac MergeBU.java 8 * Executio

算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的

1. package algorithms.stacks13; /****************************************************************************** * Compilation: javac ResizingArrayBag.java * Execution: java ResizingArrayBag * Dependencies: StdIn.java StdOut.java * * Bag implementatio

算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)

一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursively), and then merge the results. As you will see, one of mergesort’s most attractive properties is that it guarantees to sort any array of N items in t

算法Sedgewick第四版-第1章基础-003一封装交易对象

1. 1 package ADT; 2 3 /****************************************************************************** 4 * Compilation: javac Transaction.java 5 * Execution: java Transaction 6 * Dependencies: StdOut.java 7 * 8 * Data type for commercial transactions.