CS 1723 -- Evaluate RPN


Here is an RPN evaluater that uses the Java Stack class.

// Eval: evaluate RPN string
//  single digits, treated as doubles, and operators
import java.util.*;
public class Eval { 

   Stack stack; // java library stack of objects
   public Eval() {
      stack = new Stack();
   }

   // evaluateRPN: evaluate the RPN in input string as doubles
   public double evaluateRPN(String s) {
      double op1 = 0.0, op2 = 0.0, res = 0.0; // = 0.0, for compiler
      for (int i = 0; i < s.length(); i++) {
         char ch = s.charAt(i);
         if (Character.isDigit(ch) ) { // push digit as Double
            stack.push(new Double( (double)(ch - '0')));
         }
         else { // char is an operator
            if (stack.empty())
               error(1);
            else
               op2 = ((Double)(stack.pop())).doubleValue();
            if (stack.empty())
               error(2);
            else
               op1 = ((Double)(stack.pop())).doubleValue();
            res = evaluate(ch, op1, op2);
            stack.push(new Double(res));
         }
      }
      if (stack.empty())
         error(4);
      else
         res = ((Double)(stack.pop())).doubleValue();
      if (!stack.empty())
         error(5);
      return res;
   }

   // evaluate: actually perform operator
   private double evaluate(char ch, double op1, double op2) {
      switch(ch) {
         case '+': return op1 + op2;
         case '-': return op1 - op2;
         case '*': return op1 * op2;
         case '/': return op1 / op2;
         case '^': return Math.pow(op1, op2);
         default: error(3); return 0;
      }
   }

   // error: in case of an error
   private void error(int num) {
      System.out.println("Error number: " + num);
      System.exit(0);
   }

   public static void main(String[] args) {
      GetNext getNext = new GetNext(); // input class
      String s = getNext.getNextString(); // fetch string
      Eval eval = new Eval(); // new evaluator
      double res = eval.evaluateRPN(s); // produce result
      System.out.println("Result: " + res); // print result
   }
}

// GetNext: fetch next char or unsigned integer import java.io.*; public class GetNext { private Reader in; // internal file name for input stream // GetNext: constructor public GetNext () { in = new InputStreamReader(System.in); } // getNextChar: fetches next char private char getNextChar() { char ch = ' '; // = ' ' to keep compiler happy try { ch = (char)in.read(); } catch (IOException e) { System.out.println("Exception reading character"); } return ch; } // getNextString: fetch String public String getNextString() { String s ; char ch = getNextChar(); while (ch == ' ' || ch == '\n') ch = getNextChar(); s = "" + ch; while ((ch = getNextChar()) != ' ' && ch != '\n') s += ch; return s; } }
Sample run.
% java Eval
234*+
Result: 14.0
% java Eval
23+4*
Result: 20.0
% java Eval
+34
Error number: 1
% java Eval
2+34
Error number: 2
% java Eval
23#
Error number: 3
% java Eval
234*
Error number: 5
% java Eval
11212121212121212121212121212121212121212/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+
Result:         1.414213562373095
(Note:sqrt(2) = 1.414213562373095048801688724209...)
% java Eval
211121111141111161111181111125*1111126*1111127*1+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+
Result:    2.7182818284590455
(Note:e =  2.718281828459045235260287471352...)
% java Eval
111111111111111111111111111111111111111111111111111111111111111111111111+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+/+
Result:    1.618033988749894
(Note: f = 1.618033988749894848204586834365 ...
        = 	(0.5)*(1 + sqrt(5)) )
% java Eval
317153*111498*1+*11111112111311127*121+/+/+/+/+/+/+/+/+/+/+/+/+/+
Result:    3.141592653589793
(Note:pi = 3.141592653589793238462643383279...)

Revision date: 2001-09-06. (Please use ISO 8601, the International Standard.)