算法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 ...
随机推荐
- js修改隔行tr的颜色。
<!DOCTYPE html><html lang="zh-Hans"><head> <meta charset="UTF-8& ...
- 使文字出现波纹效果--第三方开源--Titanic
下载地址:https://github.com/RomainPiel/Titanic 使用的时候直接将代码复制过来即可(注意res文件下有张波浪图也要一起复制) xml代码: <com.roma ...
- Spring_总结_03_装配Bean(一)_自动装配
一.前言 本文承接上一节:Spring_总结_02_依赖注入 在上一节我们了解到依赖注入的实质就是装配. 这一节我们来学习下装配Bean的相关知识. 二.Bean的装配机制 1.三种装配机制 Spri ...
- UVALive - 4126 Password Suspects (AC自动机+状压dp)
给你m个字符串,让你构造一个字符串,包含所有的m个子串,问有多少种构造方法.如果答案不超过42,则按字典序输出所有可行解. 由于m很小,所以可以考虑状压. 首先对全部m个子串构造出AC自动机,每个节点 ...
- 使用kaptcha验证码组件操作演示
1.创建一个Maven项目 2.在pom.xml中引入相关依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- leetcode_sql_1,176,177
1.176题目,Second Highest Salary,https://leetcode.com/problems/second-highest-salary/#/description Writ ...
- [独孤九剑]Oracle知识点梳理(四)SQL语句之DML和DDL
本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...
- 生成Texture2D纹理图片
using UnityEngine;using System.Collections; public class ProceduralTexture : MonoBehaviour{ public i ...
- Sentry深入
Sentry的架构 内部架构 核心就是规则引擎以及Metadata Store:记录格式有两种,一种policy file记录授权内容,另外一种是通过命令方式进行授权:前者记录在策略文件中,保存形式是 ...
- WPF基本概念入门
关于数据类型,有原子类型,列表类型,字典类型等等,而wpf对应控件有contentControl,itemsControl,headerItemsControl等. 控件和类型一一对应,控件和类型之间 ...