CS 241 Data Structures- Lab 3, Due 2/13/15 and 2/16/15 (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 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/13)


Here is the algorithm presented in class:

  while (more input) {
     nextToken = get next token
     if (nextToken is an operand)
     else {  // an operator
         x = pop();
         y = pop();
         push(y nextToken x);   // need to implement the operation as a method
     } // else
  emit(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/16


Getting credit

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