Homework 3 - Stacks

Due: Wednesday April 20, 2011 at 5:00pm

The purpose of this assignment is to implement a stack data structure which uses a statically sized array to store the data. The second purpose of this assignment is to implement a postfix expression evaluator with the stack.

For this assignment, you will create a structure called arrayStack which will have a statically sized array of 100 integers to store the data and the current count of elements in the array (which will allow you to calculate the top of the stack). You may also use doubles for your data array if you wish, which will support a wider variety of postfix expressions.

You will need the following functions to operate on an instance of the arrayStack structure:

Your main function will implement a postfix expression evaluator. A postfix expression is of the following form:
expression expression operator
where expression can be either a numerical literal or another postfix expression. For example, the postfix expression 3 4 + is equal to 3 + 4 and the postfix expression 3 5 + 6 * is equal to (3 + 5) * 6.

Postfix expressions are evaluated by starting at the leftmost variable and working your way to the right. The stack stores the numbers in the expression. You can choose either integers or doubles for your numbers (this will be ELEMTYPE in the above function definitions).

The easiest way to implement a postfix expression evaluator is to code a separate function for the evaluation. This allows you to exit the function when the user types an invalid expression and return to main to give the user an opportunity to type a new expression in. The logic for postfix expression evaluation function is as follows:

Function: evaluatePostfix
  Given - A postfix expression string (or retrieve a line from stdin)
  Returns - Nothing (prints result to screen)

  declare a stack to store the numbers
  initialize the stack

  while there are still variables in the expression to parse (or data on stdin)
    retrieve the next variable in the expression
    if the variable is a number
      push the number onto the stack
    else if the variable is an operator
      use top to retrieve the top value off the stack (operand2)
      use pop to remove operand2 from the stack
      if pop returns false
        print "invalid number of operands" error
        return out of function
      end-if
      use top to retrieve the top value off the stack (operand1)
      use pop to remove operand1 from the stack
      if pop returns false
        print "invalid number of operands" error
        return out of function
      end-if
      perform the given operation with the two values (operand1 op operand2)
      push the result of the operation on the stack
    else
      print "invalid character in expression" error
      return out of function
    end-if
  end-while

  if the stack not empty
    retrieve the top value off the stack (result)
    pop the value off the stack
    if the stack is now empty
      print the value as the result of the expression
    else
      print "invalid expression" error
    end-if
  else
    print "invalid expression" error
  end-if
For example, the expression 3 5 + 6 * would put 3 on the stack, then 5 on the stack and then see the + operator. It would then pop 3 and 5 off the stack, add them together and put 8 on the stack. Then it would put the 6 on the stack and then see the * operator. It would then pop 8 and 6 off the stack, multiply them together and put 48 on the stack. It would then exit the while loop and pop the result 48 off the stack.

Your main program will have a do-while loop which asks the user for a postfix expression, performs the above evalutation and then prompts the user if they wish to enter another expression. This input loop will continue while the user indicates they wish to enter another expression. Use a prompt similar to what you did in Homework 2 that works regardless of if the user types 'y', 'Y', "yes", "YES", or any other combination of upper and lower case letters.

Email your completed source code to me.