算法Sedgewick第四版-第1章基础-014一用stack把前置表达式转为后置表达式并计算值
1.
/*************************************************************************
* Exercise 1.3.10
*
* % java InfixToPostfix
* ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
* 1 2 3 + 4 5 * * +
*
* % java InfixToPostfix
* ( sqrt ( 1 + 2 ) )
* 1 2 + sqrt
*
*************************************************************************/ public class InfixToPostfix
{
public static void main(String[] args)
{
Stack<String> ops = new Stack<String>();
Stack<String> vals = new Stack<String>(); while (!StdIn.isEmpty())
{
String s = StdIn.readString(); if (s.equals("(")) ;
else if (s.equals("+") ||
s.equals("-") ||
s.equals("*") ||
s.equals("/") ||
s.equals("sqrt")) ops.push(s);
else if (s.equals(")"))
{
String op = ops.pop();
String v = vals.pop(); if (op.equals("+") ||
op.equals("-") ||
op.equals("*") ||
op.equals("/"))
v = String.format("%s %s %s", vals.pop(), v, op);
else if (op.equals("sqrt"))
v = String.format("%s %s", v, op); vals.push(v);
}
else vals.push(s);
} StdOut.println(vals.pop());
}
}
2.
/*************************************************************************
* Exercise 1.3.11
*
* % java EvaluatePostfix
* 1 2 3 + 4 5 * * +
* 101.0
*
* % java EvaluatePostfix
* 1 5 sqrt + 2.0 /
* 1.618033988749895
*
* % java EvaluatePostfix
* 12 9 - 105 7 / *
* 45.0
*
* % java InfixToPostfix | java EvaluatePostfix
* ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
* 101.0
*
* % java InfixToPostfix | java EvaluatePostfix
* ( ( 1 + sqrt ( 5 ) ) / 2.0 )
* 1.618033988749895
*
*************************************************************************/ public class EvaluatePostfix
{
public static void main(String[] args)
{
Stack<Double> vals = new Stack<Double>(); while (!StdIn.isEmpty())
{
String s = StdIn.readString(); if (s.equals("(") ||
s.equals(")")) ;
else if (s.equals("+") ||
s.equals("-") ||
s.equals("*") ||
s.equals("/") ||
s.equals("sqrt"))
{
double v = vals.pop(); if (s.equals("+")) v = vals.pop() + v;
else if (s.equals("-")) v = vals.pop() - v;
else if (s.equals("*")) v = vals.pop() * v;
else if (s.equals("/")) v = vals.pop() / v;
else if (s.equals("sqrt")) v = Math.sqrt(v); vals.push(v);
}
else
vals.push(Double.parseDouble(s));
} StdOut.println(vals.pop());
}
}
算法Sedgewick第四版-第1章基础-014一用stack把前置表达式转为后置表达式并计算值的更多相关文章
- 算法Sedgewick第四版-第1章基础-013一用stack实现自动补全表达式括号
package algorithms.exercise; import algorithms.ADT.Stack; import algorithms.util.StdIn; import algor ...
- 算法Sedgewick第四版-第1章基础-012一用stack实现输出一个数的二进制形式
@Test public void e1_3_5() { Stack<Integer> stack = new Stack<Integer>(); int N = 7; whi ...
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)
一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
随机推荐
- 浪漫爱心--第三方开源--PeriscopeLayout
点此下载 使用很简单,首先在xml里面添加 <Button android:id="@+id/btn_start" android:layout_width="wr ...
- 常用集合ArrayList浅度解析
首先,先看一下java中对ArrayList的定义代码: public class ArrayList<E> extends AbstractList<E> implement ...
- hdu-2544-最短路(dijkstra算法模板)
题目链接 题意很清晰,入门级题目,适合各种模板,可用dijkstra, floyd, Bellman-ford, spfa Dijkstra链接 Floyd链接 Bellman-Ford链接 SPFA ...
- stencil in unity3d
Pass { Stencil { Ref Comp Always Pass REPLACE } AlphaTest Greater Blend SrcAlpha OneMinusSrcAlpha Co ...
- Codeforces Round #276 (Div. 2)D - Maximum Value(筛法)
就是一种筛法思想的应用. #include<iostream> #include<cstdio> #include<cstdlib> #include<cst ...
- 使用range()生成自然数序列
- LG2865 [USACO06NOV]路障Roadblocks
题意 Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. ...
- [转]200 OK (from cache) 与 304 Not Modified------没有这个规则(ETag是否移除)!!!from cache和304,请查看顶部的流程图!
//========没有这个规则(ETag是否移除) 20160422============// 200 OK (from cache) 与 304 Not Modified 为什么有的缓存是 20 ...
- 深入浅出 消息队列 ActiveMQ------增强版
本小节我们将讲解Apache开源下的ActiveMQ,而ActiveMQ是JMS的一个具体实现.JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于 ...
- Apache Geronimo Remote Code Execute Vulnerability
简介: Apache Geronimo 是 Apache 软件基金会的开放源码J2EE服务器,它集成了众多先进技术和设计理念. 这些技术和理念大多源自独立的项目,配置和部署模型也各不相同. Geron ...