Download | Plain Text | No Line Numbers
- /*
- * Copyright (c) 2008, Manuel Mausz <manuel at mausz.at>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the copyright holders nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
- * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- */
-
- import java.util.Stack;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.lang.reflect.Method;
- import java.lang.reflect.InvocationTargetException;
-
- /**
- * Implements a RPN Calculator (stack based)
- * UPN is the german name for RPN
- * See http://en.wikipedia.org/wiki/Reverse_Polish_notation for more info
- *
- * @version 1.0
- * @author Manuel Mausz (manuel at mausz.at)
- * @author http://manuel.mausz.at/
- */
- class Upn
- {
- private Stack<Integer> stack = new Stack<Integer>();
-
- /**
- * constructor
- * adds the supported operators and their method name
- * to the internal hashmap
- */
- Upn()
- {
- operators.put("+", "opAdd");
- operators.put("-", "opSub");
- operators.put("*", "opMul");
- operators.put("/", "opDiv");
- operators.put("s", "opSwap");
- }
-
- /**
- * check wheter the passed operator is supported
- *
- * @param op operator
- * @return true if supported, false otherwise
- */
- {
- return operators.containsKey(op);
- }
-
- /**
- * push operand to the internal stack
- *
- * @param int operator
- */
- public void addOperand(int op)
- {
- stack.push(op);
- }
-
- /**
- * get the method assigned to the operator
- * pop the amount of operands required from the stack
- * and finally invoke the method
- *
- * @param op operator
- * @return true on success, false otherwise
- */
- {
- if (!supportsOperator(op))
- throw new UpnException("Unknown operator");
-
- Object ret;
- try
- {
- /* get the method */
- {
- if (m.getName() == methodname)
- {
- method = m;
- break;
- }
- }
- if (method == null)
- throw new UpnException("Unknown method assigned to operator");
-
- /* pop required operands from stack */
- for (int i = 0; i < method.getParameterTypes().length; i++)
- {
- if (stack.empty())
- {
- /* NOTE:
- * EPROG specification doesn't specify whether the unused operand should
- * be pushed back on stack. I think it should BUT eprog test cases don't!
- */
- /*for (Integer v : params)
- {
- if (v != null)
- stack.push(v);
- }*/
- throw new UpnException("Insuifficent operands on stack");
- }
- params[i] = stack.pop();
- }
-
- /* invoke the method */
- }
- /* reflection encapsulate exceptions thrown during invoke. unpack them */
- {
- throw new UpnException(e.getCause().getMessage());
- }
-
- throw new UpnException("Unknown datatype returned from operation");
-
- }
-
- /**
- * Addition
- *
- * @param op1
- * @param op2
- * @return true on success, false otherwise
- */
- public boolean opAdd(int op1, int op2)
- {
- stack.push(op2 + op1);
- return true;
- }
-
- /**
- * Subtraction
- *
- * @param op1
- * @param op2
- * @return true on success, false otherwise
- */
- public boolean opSub(int op1, int op2)
- {
- stack.push(op2 - op1);
- return true;
- }
-
- /**
- * Multiplication
- *
- * @param op1
- * @param op2
- * @return true on success, false otherwise
- */
- public boolean opMul(int op1, int op2)
- {
- stack.push(op2 * op1);
- return true;
- }
-
- /**
- * Divison
- *
- * @param op1
- * @param op2
- * @return true on success, false otherwise
- */
- public boolean opDiv(int op1, int op2)
- {
- if (op1 == 0)
- throw new UpnException("Division by zero");
- stack.push(op2 / op1);
- return true;
- }
-
- /**
- * Swap
- *
- * @param op1
- * @param op2
- * @return true on success, false otherwise
- */
- public boolean opSwap(int op1, int op2)
- {
- stack.push(op1);
- stack.push(op2);
- return true;
- }
-
- /**
- * returns internal stack converted to string
- *
- * @return string
- */
- {
- while(it.hasNext())
- str += it.next() + "\n";
- return str;
- }
- }
-