CS 241 Data Structures- Lab 3, Due 2/5/13 and 2/7/13 (no credit for late programs)

Expression Evaluation

This lab will give you some practice with expressions, Stacks, and StringTokenizer (or Scanner, if you prefer).

Your task is to write a program that will: 1) read in a series of infix expressions from a file (one per line), 2) evaluate each expression and display it and its value in a TextArea. For this lab infix expressions contain only ints, the 4 binary arithmetic operators (/*-+), spaces, and parentheses. E.g.:

Infix expressions can be arbitrarily complex, so, this is not a completely trivial task. Fortunately, as we have seen in class, evaluating postfix expressions is easy, and converting infix expressions to postfix expressions is not too difficult. For this reason, this lab is divided into two phases.

Phase 1: Implement a postfix expression evaluator (Demo 2/5/13)

Details:

Here is the algorithm presented in class:

  while (more input) {
     nextToken = get next token
     if (nextToken is an operand)
         push
     else {  // must be an operator
         x = pop();
         y = pop();
         push(y nextToken x);   // need to implement the operation as a method
     } // else
  }
  output(pop());   // the answer will be on top of the stack
    

Phase 2: Implement an infix expression evaluator without parentheses and then, with. Demo on or before 2/7/13

Details:

Getting credit

Demo and email just your InfixToPostFix class before the deadline!.