算法Sedgewick第四版-第1章基础-013一用stack实现自动补全表达式括号
package algorithms.exercise; import algorithms.ADT.Stack;
import algorithms.util.StdIn;
import algorithms.util.StdOut; /*************************************************************************
*
* % java Ex_1_3_09
* 1 + 2 ) * 3 - 4 ) * 5 - 6 ) ) )
* ( ( 1 + 2 ) * ( ( 3 - 4 ) * ( 5 - 6 ) ) )
*
* % java Ex_1_3_09
* sqrt 1 + 2 ) )
* ( sqrt ( 1 + 2 ) )
*
*************************************************************************/ public class Ex_1_3_09
{
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(), op, v);
else if (op.equals("sqrt"))
v = String.format("( %s %s )", op, v); vals.push(v);
}
else vals.push(s);
//else vals.push(((Double)Double.parseDouble(s)).toString());
} StdOut.println(vals.pop());
}
}
算法Sedgewick第四版-第1章基础-013一用stack实现自动补全表达式括号的更多相关文章
- 算法Sedgewick第四版-第1章基础-014一用stack把前置表达式转为后置表达式并计算值
1. /************************************************************************* * Exercise 1.3.10 * * ...
- 算法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 ...
随机推荐
- ACM提交,C++,G++,C,GCC的区别
今天做了一道水题,POJ-1004,水题一个,12个double类型的数求平均数 但是, #include <iostream> #include <cstdio> using ...
- python_判断字符串编码的方法
1. 安装chardet 在命令行中,进入Python27\Scripts目录,输入以下的命令:easy_install chardet 2. 操作 import chardet f = open(' ...
- Linux 下如何调试 Python?
一般开发者都是在 IDE 中进行程序的调试,当然,有 IDE 的话,当然首选 IDE 进行调试. 但是,有时我们的业务场景,限制只能在 Linux 命令行模式进行调试. 这时该怎么办呢? 今天,就给大 ...
- EF各版本增删查改及执行Sql语句
自从我开始使用Visual Studio 也已经经历了好几个版本了,而且这中间EF等框架的改变也算是比较多的.本篇文章记录下各个版本EF执行Sql语句和直接进行增删查改操作的区别,方便自己随时切换版本 ...
- 洛谷 P3048 [USACO12FEB]牛的IDCow IDs
题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...
- 尚硅谷Java视频教程导航(学习路线图)
最近很火,上去看了看,对于入门的人还是有点作用的,做个记号,留着以后学习. Java视频教程下载导航(学习路线图) 网站地址:http://www.atguigu.com/download.shtml
- Httpclient远程调用WebService示例
我们将Web Service发布在Tomcat或者其他应用服务器上后,有很多方法可以调用该Web Service,常用的有两种: 1.通过浏览器HTTP调用,返回规范的XML文件内容 2.通 ...
- CF884D:Boxes And Balls
浅谈\(Huffman\)树:https://www.cnblogs.com/AKMer/p/10300870.html 题目传送门:https://codeforces.com/problemset ...
- Laravel 传递数据到视图
// 使用传统的方法 $view = view('greeting')->with('name', 'Victoria'); // 使用魔术方法 $view = view('greeting') ...
- Doubango开源项目
compv Insanely fast computer vision library for ARM and x86 devices (Up to #50 times faster than Ope ...