1.

  1. /*************************************************************************
  2. * Exercise 1.3.10
  3. *
  4. * % java InfixToPostfix
  5. * ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
  6. * 1 2 3 + 4 5 * * +
  7. *
  8. * % java InfixToPostfix
  9. * ( sqrt ( 1 + 2 ) )
  10. * 1 2 + sqrt
  11. *
  12. *************************************************************************/
  13.  
  14. public class InfixToPostfix
  15. {
  16. public static void main(String[] args)
  17. {
  18. Stack<String> ops = new Stack<String>();
  19. Stack<String> vals = new Stack<String>();
  20.  
  21. while (!StdIn.isEmpty())
  22. {
  23. String s = StdIn.readString();
  24.  
  25. if (s.equals("(")) ;
  26. else if (s.equals("+") ||
  27. s.equals("-") ||
  28. s.equals("*") ||
  29. s.equals("/") ||
  30. s.equals("sqrt")) ops.push(s);
  31. else if (s.equals(")"))
  32. {
  33. String op = ops.pop();
  34. String v = vals.pop();
  35.  
  36. if (op.equals("+") ||
  37. op.equals("-") ||
  38. op.equals("*") ||
  39. op.equals("/"))
  40. v = String.format("%s %s %s", vals.pop(), v, op);
  41. else if (op.equals("sqrt"))
  42. v = String.format("%s %s", v, op);
  43.  
  44. vals.push(v);
  45. }
  46. else vals.push(s);
  47. }
  48.  
  49. StdOut.println(vals.pop());
  50. }
  51. }

2.

  1. /*************************************************************************
  2. * Exercise 1.3.11
  3. *
  4. * % java EvaluatePostfix
  5. * 1 2 3 + 4 5 * * +
  6. * 101.0
  7. *
  8. * % java EvaluatePostfix
  9. * 1 5 sqrt + 2.0 /
  10. * 1.618033988749895
  11. *
  12. * % java EvaluatePostfix
  13. * 12 9 - 105 7 / *
  14. * 45.0
  15. *
  16. * % java InfixToPostfix | java EvaluatePostfix
  17. * ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
  18. * 101.0
  19. *
  20. * % java InfixToPostfix | java EvaluatePostfix
  21. * ( ( 1 + sqrt ( 5 ) ) / 2.0 )
  22. * 1.618033988749895
  23. *
  24. *************************************************************************/
  25.  
  26. public class EvaluatePostfix
  27. {
  28. public static void main(String[] args)
  29. {
  30. Stack<Double> vals = new Stack<Double>();
  31.  
  32. while (!StdIn.isEmpty())
  33. {
  34. String s = StdIn.readString();
  35.  
  36. if (s.equals("(") ||
  37. s.equals(")")) ;
  38. else if (s.equals("+") ||
  39. s.equals("-") ||
  40. s.equals("*") ||
  41. s.equals("/") ||
  42. s.equals("sqrt"))
  43. {
  44. double v = vals.pop();
  45.  
  46. if (s.equals("+")) v = vals.pop() + v;
  47. else if (s.equals("-")) v = vals.pop() - v;
  48. else if (s.equals("*")) v = vals.pop() * v;
  49. else if (s.equals("/")) v = vals.pop() / v;
  50. else if (s.equals("sqrt")) v = Math.sqrt(v);
  51.  
  52. vals.push(v);
  53. }
  54. else
  55. vals.push(Double.parseDouble(s));
  56. }
  57.  
  58. StdOut.println(vals.pop());
  59. }
  60. }

算法Sedgewick第四版-第1章基础-014一用stack把前置表达式转为后置表达式并计算值的更多相关文章

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

    package algorithms.exercise; import algorithms.ADT.Stack; import algorithms.util.StdIn; import algor ...

  2. 算法Sedgewick第四版-第1章基础-012一用stack实现输出一个数的二进制形式

    @Test public void e1_3_5() { Stack<Integer> stack = new Stack<Integer>(); int N = 7; whi ...

  3. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  4. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  6. 算法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 ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  8. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

随机推荐

  1. LeetCode OJ:Flatten Binary Tree to Linked List(捋平二叉树)

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  2. 11462 Age Sort(计数排序)

    内存不够用,用计数排序可以解决问题. #include<iostream> #include<cstdio> #include<cstdlib> #include& ...

  3. wordpress 自定义删除后台管理菜单

    <?php /* //wordpress共有5种角色:administrator(管理员) editor(编辑) author(作者) contributor(投稿者) subscriber(订 ...

  4. Magic Index 寻找数组中A[i]=i的位置(原题转自微信号待字闺中)

    有一个有意思的题目叫做Magic Index:给定一个数组A,其中有一个位置被称为Magic Index,含义是:如果i是Magic Index,则A[i] = i.假设A中的元素递增有序.且不重复, ...

  5. 转载:关于消息队列的使用----ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ

    转载: http://blog.csdn.net/konglongaa/article/details/52208273

  6. 每天一个linux命令(16):tail命令

    版权声明更新:2017-05-20博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...

  7. 《Javascript高级程序设计》阅读记录(三):第五章 上

    这个系列以往文字地址: <Javascript高级程序设计>阅读记录(一):第二.三章 <Javascript高级程序设计>阅读记录(二):第四章 这个系列,我会把阅读< ...

  8. C# 代码注释规范文档

    C# 提供一种机制,使程序员可以使用含有 XML 文本的特殊注释语法为他们的代码编写文档.在源代码文件中,具有某种格式的注释可用于指导某个工具根据这些注释和它们后面的源代码元素生成 XML.使用这类语 ...

  9. 手机访问PC网站自动跳转到手机网站代码(转)

    4G时代,手机网站已经非常普遍了,一般手机网站都有一个二级域名来访问,比如 m.16css.com 如果手机直接访问www.16css.com 就是PC网站,在手机上浏览电脑版网站体验非常不好. 如果 ...

  10. bzoj 3160 万径人踪灭——FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3160 似乎理解加深了. 用卷积算相同的位置:先把 a 赋成1. b 赋成0,卷积一遍:再把 ...