Assignment 2
Assignment 2
Goals:
1. Implement a generic Stack
2. Write a program that parses Infix artithmetic expressions to Postfix arithmetic
expressions using a Stack
3. Write a program that evaluates Postfix arithmetic expressions using a Stack
More detailed descriptions for each of the above tasks are now provided.
Task1: Implement a GenericStack:
Interface:
The interface of GenericStack is specified below.
It provides for the following public functionality, all of which can be efficiently achieved
using an internal array representation for the data.
GenericStack(): no-argument constructor. Initializes the Stack to some pre-specified
capacity.
~GenericStack (): destructor.
GenericStack (const GenericStack <T>&): copy constructor.
GenericStack<T>& operator= (const GenericStack <T>&): similar to the
copy constructor.
(alphanumeric)
After converting a given expression in Infix notation to Postfix notation, you will evaluate
the resulting arithmetic expression IF all the operands are numeric (int, float, etc.)
values. Otherwise, if the resulting Infix expression contains characters, your output
should be equal to the input.
Example inputs:
5 3 + 12 * 7
5 3 12 * + 7
3 5 * c 10 /
Example outputs:
89
34
3 5 * c 10 /
To achieve this, you will have an operand_stack, initially empty. Assume that the
expression contains only numeric operands (no variable names). Operands are pushed
into the stack as they are ready from the input. When an operator is read from the input,
remove the two values on the top of the stack, apply the operator to them, and push the
result onto the stack. If an operator is read and the stack has fewer than two elements in
it, report an error. If end of input is reached and the stack has more than one operand
left in it, report an error. If end of input is reached and the stack has exactly one operand
in it, print that as the final result, or 0 if the stack is empty.
For more information on the evaluation of Infix notation arithmetic expressions, look up
section 3.6 of the textbook.
We provide scripts with example input and output values.
Summarizing task 2.
Your program should expect as input from (possibly re-directed) stdin a series of
space-separated strings. If you read a1 (no space) this is the name of the variable a1
and not a followed by 1. Similarly, if you read bb 12, this is a variable bb followed by
the number 12 and not b ,b, 12 or bb, 1 ,2.
Your program should convert all Infix expressions to Postfix expressions, including
expressions that contain variable names. The resulting Postfix expression should be
printed to stdout.
Your program should evaluate the computed Postfix expressions that contain only
numeric operands, using the above algorithm, and print the results to stdout.
Restrictions
1. You MAY NOT use any C++ STL classes in addition to the GenericStack,
BasicContainer, and ANSI string class for all your data structures.
2. The Infix Postfix conversion MUST be able to convert expressions containing both
numbers and variable names (alphanumeric strings). Variable names may be arbitrary
strings, such as sci_fi_freak88
3. You MAY use the "ctype.h" (alpha/digit check) and "stdlib.h" (alpha to digit/float
conversion) library.
4. Their program MUST be able to produce floating number evaluation (i.e., deal with
floats correctly).
5. Your program MUST NOT attempt to evaluate postfix expressions containing variable
names. It should print the Postfix-converted result to stdout and MAY NOT throw an
exception nor reach a runtime error in that case.
6. Your MUST check for mismatched parentheses.
7. Their program MUST re-prompt the user for the next infix expression. Your program
must be able to process several inputs at a time.
Deliverables
Your implementation should be contained in three files, which MUST be named Generic
Stack.h, GenericStack.cpp and in2post.cpp. Note that we will use our own
implementation of BasicContainer.cpp to test your code. So make sure that all calls
made by your GenericStack class to your BasicContainer class use the public interface
of BasicContainer. Please read the following instructions for submitting your assignment.
For instructions on how to submit your project, consult the first assignment,
which is posted online. Remember to substitute 2 for 1 in all the command and
path names which have the form projectX or projX