Labels

Wednesday 15 January 2014

Basic Reverse Polish Notation (RPN) Calculator


The RPN notation is also known as postfix expression. The capabilities of the Calculator are as follows:
1. Supports operations for sum, difference, division, multiplication.
2. Supports floating point operations.
3. Use '^' for exponent operator. values entered as "X Y ^" are calculated as X to the power of Y.
4. Use '%' for the percent operation. values entered as "X %" are calculated as X/100.
5. Use ‘!’ for Factorial (unary operation).
Example:
 Please Enter the Input: 5 1 2 + 4 * + 3 -
 Output:14
Rpn.Java

package rpn;


import java.util.LinkedList;
import java.util.Scanner;

public class Rpn {

    public static void evaluate(String expr){

        System.out.println("Your Input Is: " + expr);
        String symbols = symbols(expr);

        LinkedList<Double> stack = new LinkedList<Double>();

        for(String digit:symbols.split("\\s")){

            Double digitValue = null;
            try{
                digitValue = Double.parseDouble(digit);
            }catch(NumberFormatException e){}
            if(digitValue != null){

                stack.push(Double.parseDouble(digit+""));
            }else if(digit.equals("-")){
                double secondSymbol = stack.pop();
                double firstSymbol = stack.pop();
                stack.push(firstSymbol - secondSymbol);
            }else if(digit.equals("+")){

                double secondSymbol = stack.pop();
                double firstSymbol = stack.pop();
                stack.push(firstSymbol + secondSymbol);
            }else if(digit.equals("*")){

                double secondSymbol = stack.pop();
                double firstSymbol = stack.pop();
                stack.push(firstSymbol * secondSymbol);
            }else if(digit.equals("/")){
                double secondSymbol = stack.pop();
                double firstSymbol = stack.pop();
                stack.push(firstSymbol / secondSymbol);
            }else if(digit.equals("^")){
                double secondSymbol = stack.pop();
                double firstSymbol = stack.pop();
                stack.push(Math.pow(firstSymbol, secondSymbol));
            }else if(digit.equals("%")){
                double firstSymbol = stack.pop();
                stack.push(firstSymbol/100);
            }else if(digit.equals("!")){
                double firstSymbol = stack.pop();

                int c, fact = 1;

                if ( firstSymbol < 0 )
                    System.out.println("Number should be non-negative.");
                else
                {
                    for ( c = 1 ; c <= firstSymbol ; c++ )
                        fact = fact*c;
                }
                stack.push(Double.valueOf(fact));
            }else {
                System.out.print("error");
            }
        }
        System.out.println("Result: " + stack.pop());
    }

    private static String symbols(String expr){
        //remove all non-operators, non-whitespace, and non digit chars
        return expr.replaceAll("[^\\^\\*\\+\\-\\d/\\s\\!\\%]", "");
    }

    public static void main(String[] args){
        String search;
        Scanner in = new Scanner(System.in);
        System.out.print("Please Enter the Input ");
        search=in.nextLine();
        evaluate(search);
    }
}

Keywords:java,RPN, Basic Reverse Polish Notation,Calculator in Java

No comments:

Post a Comment