算法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 ...
随机推荐
- 19 Python 正则模块和正则表达式
什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(.p ...
- 2018.7.24 Error Code
来不及解释了,写下再说 -------------------------------------------- SUCCESS = 0, RTC_SELFTEST_FAILED = 1, ...
- pdp 的连接方法
<?php $dsn = "mysql:host=127.0.0.1;dbname=blog_edu"; try{ $pdo = new PDO($dsn,'root','' ...
- LeetCode Maximum Distance in Arrays
原题链接在这里:https://leetcode.com/problems/maximum-distance-in-arrays/description/ 题目: Given m arrays, an ...
- python之 前端HTML/CSS基础知识学习笔记
1. 文件结构: HTML文件的固定结构: <html> <head>...</head> <body>...</body> </ht ...
- emacs复制粘贴和查找撤销
在emacs下复制粘贴是这样的.1.在任一行中按下ctrl+space键,最底行会显示 Mark set 表示已经开始标记了.2.移动鼠标或者选中文字然后,可以有两种方式:Alt+w (复制) 或者 ...
- Windows命令行操作MySQL
使用命令行操作mysql的一些简单步骤: //进入MySQL数据库 > mysql -hlocalhost -uroot -p//显示 所有数据库 > show databa ...
- 机器学习:PCA(人脸识别中的应用——特征脸)
一.思维理解 X:原始数据集: Wk:原始数据集 X 的前 K 个主成分: Xk:n 维的原始数据降维到 k 维后的数据集: 将原始数据集降维,就是将数据集中的每一个样本降维:X(i) . WkT = ...
- Swing编程中为什么对Panel设置了位置后,它的lable位置也跟这变了呢?
总结: package com.da; //这里我不明白,我对每一个Panle里的按钮设置了位置BorderLayout.SOUTH.可是为什么Lable的位置和它一样同步设置了呢? //做一个界面 ...
- canvas绘制简单图形
canvas绘图篇: canvas绘制矩形: <!DOCTYPE html> <html> <head lang="en"> <meta ...