# Infix calculator

## Infix calculator

In this assignment, you will implement an infix calculator.

This infix calculator accepts input from a file specified as a command-line parameter. It reads each line of the file. For each line, it considers each character of the line, one at a time, taking the appropriate action for each character. A character is one of the following:

• a digit, 0..9, which should be converted to an integer and pushed onto the operand stack, or
• a space character, which should be ignored, or
• an operator, +, -, *, /, or %, which should be compared to the operator at the top of the stack (if any):
• If the operator precedence of the new operator is higher than the precedence of the operator on top of the stack, the new operator should simply be pushed onto the stack.
• If the operator precedence of the new operator is less than or equal to the precedence of the operator on top of the stack, then:
1. the operator on top of the stack should be popped and applied to the two operands on top of the operand stack, then
2. the result of that operation should be pushed back onto the operand stack, and
3. if the operator precedence of the new operator is less than or equal to the precedence of the operator on top of the stack, then repeat, otherwise the new operator should be pushed onto the operator stack.
• once the end of the input is reached, the following should be repeated until the operator stack is empty and the operand stack has a single operand:
1. the operator on top of the stack should be popped and applied to the two operands on top of the operand stack, then
2. the result of that operation should be pushed back onto the operand stack

Any character other than space, a digit, or +-*/%, is illegal for this calculator, and should lead to your program reporting the problem and exiting.

There are only two operator precedence levels: + and - have low operator precendence, and *, / , and % have high operator precedence.

Your program must exit once the end of the file is reached.

At the end of each line, there should be exactly one number left on the stack, and nothing left in the operator stack. If that is the case, the number should be printed as the result of the operation. Otherwise, an error should be reported and the program should exit.

You should use this interface for your operand stack, as well as this implementation. Include these files when submitting the assignment. If you want to, you may add new methods to the the interface and the implementation, for example some people may want to add a peek() method.

You must also convert the integer stack code to store char instead of int, for the operator stack. Call your code CharStackInterface.java and CharArrayStack.java.html. Again you may add any methods you need (but do not remove any methods).

For some ideas on how to read lines in a file, see ReadFile. You may copy and adapt any code you wish from this file.

You may also test your program on this file. The expected result of the computations are 1, 2, 3, ... 10. You should also test your program on other files that you create.

You are welcome to consult the book (e.g. Section 5.4) in doing this project. However, the book does things in a different way than is required for this project, and you may not (and cannot and are not allowed to) directly use the code in the book. You MAY copy and use any bits and pieces (up to 4 lines of code per snippet) that are useful for your project. You MUST appropriately label (with a comment) any code that you copy or adapt from the book.

The TA is very familiar with the code in the book, and any wholesale copying of code from the book will get no credit for the assignment.

## Hints

You can use Character.isSpaceChar to determine if a character is a space.

Likewise, you can use Character.isDigit to determine if a character is a digit, and Character.digit to convert a digit to an int (radix should simply be 10 in your code).

## Turning in the Assignment

Email your assignment to the TA following the instructions here.