Download | Plain Text | No Line Numbers


  1. /*
  2.  * Copyright (c) 2008, Manuel Mausz <manuel at mausz.at>
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  * * Redistributions of source code must retain the above copyright
  8.  * notice, this list of conditions and the following disclaimer.
  9.  * * Redistributions in binary form must reproduce the above copyright
  10.  * notice, this list of conditions and the following disclaimer in the
  11.  * documentation and/or other materials provided with the distribution.
  12.  * * Neither the name of the copyright holders nor the
  13.  * names of its contributors may be used to endorse or promote products
  14.  * derived from this software without specific prior written permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20.  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  23.  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  25.  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  27.  * DAMAGE.
  28.  */
  29.  
  30. import java.util.Stack;
  31. import java.util.HashMap;
  32. import java.util.Iterator;
  33.  
  34. /**
  35.  * Implements a RPN Calculator (stack based)
  36.  * See http://en.wikipedia.org/wiki/Reverse_Polish_notation for more info
  37.  *
  38.  * @version 1.0
  39.  * @author Manuel Mausz (manuel at mausz.at)
  40.  * @author http://manuel.mausz.at/
  41.  */
  42. class Rechner implements UPNRechner
  43. {
  44. private Stack<Double> stack = new Stack<Double>();
  45. private HashMap<String, UPNOperation> operations = new HashMap<String, UPNOperation>();
  46.  
  47. /**
  48.   * add operation to "supported operations"-list
  49.   *
  50.   * @param op operation object
  51.   */
  52. public void addOperation(UPNOperation op)
  53. {
  54. operations.put(op.getOperationSymbol(), op);
  55. }
  56.  
  57. /**
  58.   * get operation mapped to the specified operation-symbol
  59.   *
  60.   * @param op operation symbol
  61.   * @return operation object or null
  62.   */
  63. public UPNOperation getOperation(String op)
  64. {
  65. return (operations.containsKey(op)) ? operations.get(op) : null;
  66. }
  67.  
  68. /**
  69.   * push operand onto internal stack
  70.   *
  71.   * @param val value
  72.   */
  73. public void enterNumber(Double val)
  74. {
  75. stack.push(val);
  76. }
  77.  
  78. /**
  79.   * executes operation assigned to operation symbol
  80.   *
  81.   * @param opstr operation symbol
  82.   * @throws InvalidOperationException if operation isn't supported
  83.   * @throws InvalidParameterException
  84.   */
  85. public void enterOperation(String opstr)
  86. throws InvalidOperationException, InvalidParameterException
  87. {
  88. UPNOperation op = getOperation(opstr);
  89. if (op == null)
  90. throw new InvalidOperationException("Operator doesn't exist");
  91. op.execute(stack);
  92. }
  93.  
  94. /**
  95.   * returns internal stack converted to string
  96.   *
  97.   * @return string
  98.   */
  99. public String toString()
  100. {
  101. String str = "";
  102. Iterator it = stack.iterator();
  103. while(it.hasNext())
  104. str += it.next() + " ";
  105. return str;
  106. }
  107. }
  108.