您的位置:首页 > 其它

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

2016-04-19 17:14 309 查看
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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: