Please read the remarks about programming problems for this course.
In this assignment you will use a stack to assist in two activities:
Your program will accept an arithmetic expression typed in by the user; for example, the user might enter
3*(22 + 15) + 4*(3 + 10)
to which the program would respond with 163. The program will be a simple interpreter. An interpreter translates source code to executable code like a compiler, but an interpreter executes the translated code immediately rather than storing it in an executable file for later execution. The translation is a multipart process:
The file scanner.h (see below) defines the Token type. Our token object stores a character (symbol field) and an integer (value field). A token's symbol may be one of $, +, *, (, or ). If the token's symbol is $, the token represents an integer, and so its value field stores a valid integer value. If the token's symbol is any other character, the token's meaning is derived from its symbol; that is, + represents addition, * represents multiplication, ( represents a left parenthesis, and ) represents a right parenthesis. If the token's symbol is not $, the token's value field is meaningless.
The function scan (provided for you) converts a line of user input into a token sequence, if possible. This function is the scanner. You should use this function as is. The function expects a line that contains just non-negative integers, plus signs, asterisks, and parentheses. An acceptable input string will be an arithmetic expression in infix notation (see http://en.wikipedia.org/wiki/Infix_notation). The function's return value is a token sequence in infix notation. Any whitespace (spaces and tabs) in the original input string does not appear in the token sequence.
The code you write will deal with this infix token sequence. You must translate this infix token sequence into an equivalent postfix token sequence. The postfix sequence is devoid of parentheses, because parentheses are not necessary in a postfix arithmetic expression. Fortunately, the algorithm to perform the translation is well known and easy to implement (see below).
Once you have the postfix token sequence, you must write a function that processes the token sequence to produce an integer result. Again, the algorithm is relatively easy to implement.
Both the infix-to-postfix translation and postfix evaluation rely on one very useful linear data structure—the stack.
The complete project consists of six C++ source files:
Part of your task is to complete the code for a generic stack abstract data type. The incomplete stack code is found in stack.h. It defines a template class that clients can instantiate to the specific type of elements they need to store. The non-type template parameter (N) specifies the stack's maximum capacity. Since the Stack class is a template, there is no advantage to define its methods outside of the class itself.
Your stack should use a statically allocated C++ array for its storage. The non-type template parameter, N, determines the array's size. Your stack type must support at least the following operations:
You have full control over the exact parameters and return types of these methods. You also are responsible for determining the error-checking capabilities the methods provide, if any (for example, what if the user attempts to pop an element off an empty stack or push an item onto a full stack?). Your stack implementation should be general enough so that your stack objects could be used in many different contexts.
After you have tested your stack in isolation to ensure that it behaves properly, you will use it in two different ways:
Implement the infix_to_postfix function which performs the translation. Your code in this function must use a stack of tokens.
Implement the evaluate function which performs the translation. Your code in this function must use a stack of integers.
The requirement of using both a stack of tokens and a stack of integers demonstrates that your Stack class truly is generic.
For this assignment you do not need to worry about handling illegal arithmetic expressions the user might enter; however, you must be able to process all legal arithmetic expressions the user submits.