CS2 Summer 2006 Assignment 35

Informal Title: Postfix Evaluation Application

Date of Issue:


Tasks

Establish the PostfixEvalApp program (such as it is) within the programs package of the DataTypes project.

  1. Study the PostfixEvalApp code.
  2. Fill in the missing code fragment!
  3. Compile the program and run it!

Demo

init:
deps-jar:
compile-single:
run-single:
Please enter a postfix arithmetic expression:  
6 4 /
value = 1.5
BUILD SUCCESSFUL (total time: 20 seconds)


init:
deps-jar:
Compiling 1 source file to /home/blue/nb/datatypes/DataTypes/build/classes
compile-single:
run-single:
Please enter a postfix arithmetic expression:  
40.0 5.0 6.0 * - 3.0 2.0 / +
value = 11.5
BUILD SUCCESSFUL (total time: 13 seconds)


init:
deps-jar:
Compiling 1 source file to /home/blue/nb/datatypes/DataTypes/build/classes
compile-single:
run-single:
Please enter a postfix arithmetic expression:  
1 + 1
Stack underflow occurred
Evaluation Exception
Expression could not be evaluated.
BUILD SUCCESSFUL (total time: 4 seconds)

Code

/*
 * PostfixEvalApp.java
 */

package programs;

import datatypes.stack.*;
import datatypes.stack.sequential.*;
import java.util.*;
import java.io.*;

class PostfixEvalApp
{
    static public void main(String[] args)
    {
        String source = getSource();
        Double value = postfixEval(source);
        if ( value == null ) {
            System.out.println("Expression could not be evaluated.");
        } else {
            System.out.println("value = " + value.doubleValue());
        }
    }

    static private String getSource()
    {
        System.out.println("Please enter a postfix arithmetic expression:  ");
        return readLine();
    }

    static private boolean operatorp(String s)
    {
        if ( s.equals("+") ) {
            return true;
        } else if ( s.equals("-") ) {
            return true;
        } else if ( s.equals("*") ) {
            return true;
        } else if ( s.equals("/") ) {
            return true;
        } else {
            return false;
        }
    }

    static private Double postfixEval(String source)
    {
        Double result = null;
        ObjectStack stack = new ObjectStack();
        try {
            StringTokenizer t = new StringTokenizer(source);
            while ( t.countTokens() > 0 ) {
                String s = t.nextToken();
                if ( operatorp(s) ) {
                    performEval(stack,s);
                } else {
                    stack.push(s);
                }
            }
            result = Double.valueOf(stack.top().toString());
        } catch ( Exception e ) {
            System.out.println("Evaluation Exception");
            result = null;
        }
        return result;
    }

    static private void performEval(ObjectStack stack, String operator) throws Exception
    {
        try {
            Object o2 = stack.pop();
            Object o1 = stack.pop();
            String s2 = (String)o2;
            String s1 = (String)o1;
            Double bd2 = Double.valueOf(s2);
            Double bd1 = Double.valueOf(s1);
            double d2 = bd2.doubleValue();
            double d1 = bd1.doubleValue();
            double result;
            if ( operator.equals("+") ) {
                result = d1 + d2;
            } else if ( operator.equals("-") ) {
                result = d1 - d2;
            } else if ( operator.equals("*") ) {
                result = d1 * d2;
            } else { // it must be divides!
                result = d1 / d2;
            }
            stack.push(new Double(result).toString());
        } catch ( ObjectStackOverflowException e ) {
            System.out.println("Stack overflow occurred");
            throw new Exception();
        } catch ( ObjectStackUnderflowException e ) {
            System.out.println("Stack underflow occurred");
            throw new Exception();
        }
    }
        
    static private String readLine() 
    {
        String result = "";
        char ch = ' ';
        try {
            while ( ch != '\n' ) {
                result = result + ch;
                ch = (char)System.in.read();
            };
        } catch (IOException e) {
            result = "";
        };
        return result;
    }

    static private void display(ObjectStack s)
    {
        System.out.println(s.toString());
    }
    
}