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把前置表达式转为后置表达式并计算值的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-013一用stack实现自动补全表达式括号

    package algorithms.exercise; import algorithms.ADT.Stack; import algorithms.util.StdIn; import algor ...

  2. 算法Sedgewick第四版-第1章基础-012一用stack实现输出一个数的二进制形式

    @Test public void e1_3_5() { Stack<Integer> stack = new Stack<Integer>(); int N = 7; whi ...

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

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  4. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

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

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  6. 算法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 ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  8. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

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

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

随机推荐

  1. js抛物线

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. hdoj-1715-大菲波数(大斐波那契数列)

    题目链接 import java.util.*; import java.math.*; public class Main{ public static void main(String[] arg ...

  3. How to install php 7.x on CentOS 7

    Step 1: Setup the Webtatic YUM repo Precompiled PHP 7.x binaries are available for CentOS 7 from the ...

  4. 基于RTP协议的H.264传输

    1.  引言        随 着信息产业的发展,人们对信息资源的要求已经逐渐由文字和图片过渡到音频和视频,并越来越强调获取资源的实时性和互动性.但人们又面临着另外一种不可避免 的尴尬,就是在网络上看 ...

  5. THUWC2017

    100+20+20=140 还是很菜... T1 在美妙的数学王国中畅游 一棵树每个点有一个函数(sin,exp,一次函数),支持加边,删边,单点修改,查询一条路径在 $x$ 处的点值和 sol: 题 ...

  6. controller返回js中文变成?解决方案

    在使用spring-mvc的mvc的时候既享受它带来的便捷,又头痛它的一些问题,比如经典的中文乱码问题.现在是用json作为客户端和服务端 的数据交换格式貌似很流行,但是在springmvc中有时候会 ...

  7. 使用 MLCC 替代电解电容需要注意几点 (2018-07-23)

    使用 MLCC 替代电解电容需要注意几点 容量,MLCC 在高压时容量会降到标称的 30~50% 以下 1. MLCC 的 ESR 很低,比较适合高频 DCDC 输出. MLCC 会有压电效应,可能会 ...

  8. linux内核图形配置疑难解决

    配置linux内核是遇到的问题:(1)问题一make gconfig * * Unable to find the GTK+ installation. Please make sure that * ...

  9. [转]实现微信浏览器内打开App Store链接

    微信浏览器是不支持打开App Store 页面的,不知道微信为什么这么做.比如你页面写 <a href=”http://itunes.apple.com/us/app/id399608199″& ...

  10. Dynamic Web Project vs Static Web Project 以及 Project facets

    Dynamic Web Project vs Static Web Project 需要用到JSP,servlet等技术的动态服务器技术,就需要DWP:对于全部都是html页面的可以使用static ...