## 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).

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.:
• 1 evaluates to 1
• 1 + 2 evaluates to 3
• 1 + 2 /3 evaluates to 1
• (1 + (2-3)/4)*5 evaluates to 5
• 1 + 1 + 4 / 2 evaluates to 4
• ( 1 + ( 1 + 4 ) / 2 ) * ( 5 + 2 ) evaluates to 21
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)

#### Details:

• Data here!
• No partners for this one! Everyone write their own code! Okay to collaborate on design.
• Input and display each line from file of postfix expressions; pass the path to the MyReader constructor so you don't have to select the file each time you run.
• Follow the algorithm presented in class (and reproduced below) to implement a postfix expression evaluator. To become familiar with the Singleton pattern, write a PostFixEvaluator class that has a `static int evaluate(String inputLine)` method. Thus, the code in your Frame might simply be:
```while (mr.hasMoreData()) {
String nextLine = mr.giveMeTheNextLine();
emit(nextLine + "=" + PostFixEvaluator.evaluate(nextLine) + "\n");
}
```
• Test before you leap! Use a StringTokenizer to separate the input lines into tokens (`StringTokenizer st = new StringTokenizer(s, "+-*/ ", true);` will work well) -- or, if you prefer, learn to use a Scanner to do it. Test your code to be sure it is generating the correct stream of tokens.
• What will you store in your Stack? You might choose to store Strings, or Integers, or Operands (you would have to write the Operand class), or something else; think through the design, and make a decision on that basis (what will be on the Stack in Phase 2? Or will you have two different kinds of Stack?).

Here is the algorithm presented in class:

```  while (more input) {
nextToken = get next token
if (nextToken is an operand)
push(nextToken)
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

#### Details:

• Infix data here!
• Convert each infix expression to postfix with a `String InfixToPostFix.convert(String)` method following the algorithm presented in lecture, then...
• Let the Phase 1 code do the rest!