算法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 ...
随机推荐
- 炫酷自定义翻转View--第三方开源--TagCloudView
下载地址:https://github.com/ChinaZeng/3dTagCloudAndroid 贴上Demo代码: <com.moxun.tagcloudlib.view.TagClou ...
- Spring_总结_04_高级配置(六)_Bean的初始化和销毁
一.前言 本文承接上一节:Spring_总结_04_高级配置(五)_运行时注入值
- LeetCode OJ:Minimum Size Subarray Sum(最小子数组的和)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 《Effective C++》——条款04:确定对象使用前已先被初始化
读取未初始化的值会导致不明确的行为.在某些平台上,仅仅只是读取未初始化的值,就可能让你的程序终止运行.更可能的情况是读入一些“半随机”bits,污染了正在进行读取动作的那个对象,最终导致不可预知的程序 ...
- SQL使用指南(1)—— 数据定义语言(DDL)
1.使用create 语句创建表 CREATE TABLE table_name (column_name datatype[null|not null], column_name datatype[ ...
- group by 和 聚合函数
1.在oracle中 select * from Table group by id 会报错. 会报不是group by 表达式.为什么一定不能是 * ,而必须是分组的列或者某个列的聚合函数. 在my ...
- LeetCode 362. Design Hit Counter
原题链接在这里:https://leetcode.com/problems/design-hit-counter/description/ 题目: Design a hit counter which ...
- 微信小程序 报错Setting data field "variableName" to undefined is invalid.
Setting data field "variableName" to undefined is invalid. 将数据字段“variableName”设置为未定义是无效的. ...
- php写入数据到mysql数据库中出现乱码解决方法
乱码情况: 在选择数据库前加入一句代码即可 mysql_query("set names utf8"); 最后效果
- 使用cef
win10的同学注意了按右键以管理员模式启动cmake-gui.exe在Where is the source code:里填上你解压的CEF3路径,如:F:\cef3\cef_binary_3.23 ...