Department of Electrical and Computer Engineering
University of Texas at Austin
EE 379K, Fall 2000
Y. N. Patt, Instructor
TAs: Kathy Buchheit, Laura Funderburg, Chandresh Jain, Onur Mutlu, Danny Nold, Kameswar Subramanian, Francis Tseng, Brian Ward

Programming Assignment 5
Due: December 3, 2000 11:59 PM

Infix Calculator

Problem Statement

In this program, we ask you to design an infix calculator. The program should start by displaying a prompt for the user Input>. The user will then enter an arbitrary arithmetic expression, using only the operator + (denoting the binary operation of addition), the operator - (denoting the binary operation of subtraction), the left parenthesis, the right parenthesis, together with decimal valued integer operands. This expression should be entered using an infix notation (explained below). As this expression is entered, the program can begin evaluating the expression. Once entry of the expression is complete (i.e., after the user hits the "ENTER" key), the program should display the results of calculating the arithmetic expression entered, followed by a new prompt for further user input. When the user types "x" in response to the prompt for further input, the program should display "Have a nice day!" and terminate.

Infix Notation

Most people are accustomed to writing arithmetic expressions in INFIX notation, in which case an operator that denotes a binary operation appears BETWEEN the two operands, as in

( 2 + 3 )
Using nested parentheses, we can represent more complex expressions such as
( 11 + ( ( 19 + 23 ) - ( 4 + 10 ) ) )

Program Details

• You may assume that only valid input expressions are entered.
• The numbers and symbols must be separated by exactly one space.
• The numbers in the input expression are positive integers up to three decimal digits.
• The input expression is fully parenthesized. Examples of expressions that are NOT fully parenthesized are
1 + 2
and
( 2 + ( 15 - 12 ) + 5 )
• There must be an operator for every pair of parentheses. For example, the following will not be considered as valid input expressions.

• ( 5 )
and
( ( 5 ) - ( 4 + 3 ) )
• Your result should be output to the screen using a plus or minus sign followed by three deciaml digits.
• When the result of an expression is zero, you should output +000.
• You are strongly encouraged to use a stack in the implementation of your design. You should allocate a block of 50 entries for the stack in your program.
• You are also encouraged to use subroutines to make your program easier to work with.
• You are free to use any example program in the textbook.

Sample Input/Output

Input> ( 101 - ( ( 23 + 31 ) + 74 ) )
-027

```Input> ( 150 - ( 1 + 2 ) )
+147```
```Input> ( 1 - 1 )
+000```
Input> ( ( 200 - ( ( ( ( ( 5 - 3 ) - 75 ) - 18 ) + 5 ) - 7 ) ) + 2 )
+295

Input> x
Have a nice day!