Objectives

Understand the stack ADT and its applications. Understand infix to postfix conversion and postfix expression evaluation.

Statement of Work

Implement a generic stack container as an adaptor class template. Implement a program that converts infix expression to postfix expression and implement a program that evaluates postfix expression using the stack container you develop.

Project Description

A Stack is a type of data container/ data structure that implements the LAST-IN-FIRST-OUT (LIFO) strategy for inserting and recovering data. This is a very useful strategy, related to many types of natural programming tasks as we have discussed.

Remember that in the generic programming paradigm, every data structure is supposed to provide encapsulation of the data collection, enabling the programmer to interact with the entire data structure in a meaningful way as a container of data. By freeing the programmer from having to know its implementation details and only exporting the interface of its efficient operations, a generic Stack provides separation of data access/manipulation from internal data representation. Programs that access the generic Stack only through the interface can be re- used with any other Stack implementation. This results in modular programs with clear functionality and that are more manageable.

Goals:

  • Implement a generic Stack as an adaptor class template
  • Write a program that parses infix arithmetic expressions to postfix arithmetic expressions using a Stack
  • 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 Stack adaptor class template

  • Stack MUST store elements internally using a proper C++ STL container (of course, you cannot use the C++ STL stack container).
  • Your Stack implementation MUST:
    • be able to store elements of an arbitrary type.
    • Every Stack instance MUST accept insertions as long as the system has enough free memory.
  • Stack MUST implement the full interface specified below
  • You MUST provide the template and the implementation in two different files stack.h and stack.hpp, respectively. You must include stack.hpp into stack.h
  • Your stack implementation MUST be in the namespace prog.

Interface:

The interface of the Stack class template is specified below.

  • Stack(): zero-argument constructor.
  • ~Stack (): destructor.
  • Stack (const Stack< T>&): copy constructor.
  • Stack(Stack< T> &&): move constructor.
  • Stack< T>& operator= (const Stack < T>&): copy assignment operator=.
  • Stack< T> & operator=(Stack< T> &&): move assignment operator=
  • bool empty() const: returns true if the Stack contains no elements, and false otherwise.
  • void clear(): delete all elements from the stack.
  • void push(const T& x): adds x to the Stack. copy version.
  • void push(T && x): adds x to the Stack. move version.
  • void pop(): removes and discards the most recently added element of the Stack.
  • T& top(): returns a reference to the most recently added element of the Stack (as a modifiable L-value).
  • const T& top() const: accessor that returns the most recently added element of the Stack (as a const reference)
  • int size() const: returns the number of elements stored in the Stack.
  • void print(std::ostream& os, char ofc = ' ') const: print elements of Stack to ostream os. ofc is the separator between elements in the stack when they are printed out. Note that print() prints elements in the opposite order of the Stack (that is, the oldest element should be printed first).

The following non-member functions should also be supported.

  • std::ostream& operator<< (std::ostream& os, const Stack< T>& a): invokes the print() method to print the Stack< T> a in the specified ostream
  • bool operator== (const Stack< T>&, const Stack < T>&): returns true if the two compared Stacks have the same elements, in the same order, and false otherwise
  • bool operator!= (const Stack< T>&, const Stack < T>&): opposite of operator==().
  • bool operator<= (const Stack< T>& a, const Stack < T>& b): returns true if every element in Stack a is smaller than or equal to the corresponding element of Stack b, i.e., if repeatedly invoking top() and pop() on both a and b, we will generate a sequence of elements ai from a and bi from b, and for every i, ai <= bi, until a is empty.

Analyze the worst-case run-time complexity of the member function clear() of Stack. Give the complexity in the form of Big-O. Your analysis can be informal; however, it must be clearly understandable by others. Note that the time complexity of the function clear() depends on the underlying adaptee class you use for the implementation of Stack. Name the file containing the complexity analysis as "analysis.txt", in which, you should state the time complexity of clear() in the Big-O notation, your discussions on why it is so, in particular, you need to include the information on the underlying adaptee class you used in the implementation of the Stack.

Task 2:

Convert infix arithmetic expressions into postfix arithmetic expressions and evaluate them (whenever possible)

For the sake of this, an arithmetic expression is a sequence of space-separated strings. Each string can represent an operand, an operator, or parentheses.

Operands: can be either a numerical value or a variable. A variable name only consists of alphanumerical letters and the underscore letter "_". A variable name starts with a English letter. Numerical operands can be either integer or floating point values.

Examples of operands: "34", "5", "5.3", "a", "ab", "b1", and "a_1"

Operators: one of the characters '+', '-', '*', or '/'. As usual, '*' and '/' are regarded as having higher precedence than '+' or '-'. Note that all supported operators are binary, that is, they require two operands.

Parentheses: '(' or ')'

An Infix arithmetic expression is the most common form of arithmetic expression used.

Examples:

( 5 + 3 ) * 12 - 7 is an infix arithmetic expression that evaluates to 89
5 + 3 * 12 - 7 is an infix arithmetic expression that evaluates to 34

For the sake of comparison, postfix arithmetic expressions (also known as reverse Polish notation) equivalent to the above examples are:

5 3 + 12 * 7 -
5 3 12 * + 7 -

Two characteristics of the Postfix notation are (1) any operator, such as '+' or '/' is applied to the two prior operand values, and (2) it does not require the use of parenthesis.

More examples:

a + b1 * c + ( dd * e + f ) * G in Infix notation becomes
a b1 c * + dd e * f + G * + in Postfix notation

To implement infix to postfix conversion with a stack, one parses the expression as sequence of space-separated strings. When an operand is read in the input, it is immediately output. Operators (e.g., '-', '*') may have to be saved by placement in an operator stack. We also stack left parentheses. Start with an initially empty operator stack.

Follow these 4 rules for processing operators/parentheses:

  • If input symbol is '(', push it into stack.
  • If input operator is '+', '-', '*', or '/', repeatedly print the top of the stack to the output and pop the stack until the stack is either (i) empty ; (ii) a '(' is at the top of the stack; or (iii) a lower-precedence operator is at the top of the stack. Then push the input operator into the stack.
  • If input operator is ')' and the last input processed was an operator, report an error. Otherwise, repeatedly print the top of the stack to the output and pop the stack until a '(' is at the top of the stack. Then pop the stack discarding the parenthesis. If the stack is emptied without a '(' being found, report error.
  • If end of input is reached and the last input processed was an operator or '(', report an error. Otherwise print the top of the stack to the output and pop the stack until the stack is empty. If an '(' is found in the stack during this process, report error.

Evaluating postfix arithmetic expressions

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 postfix expression contains characters, your output should be the same as the input (the postfix expression).

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.

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". 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

The infix to postfix conversion MUST be able to convert expressions containing both numbers and variable names.

Your program MUST be able to produce floating number evaluation (i.e., deal with floats correctly).

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.

Your program MUST check for mismatched parentheses (this should be taken care of if you infix to postifx expression conversion is performed properly.

Your program MUST check invalid infix expressions and report errors. We consider the following types of infix expressions as invalid expressions: 1) an operator does not have the corresponding operands, 2) an operand does not have the corresponding operator; or ) mismatched parentheses. Note that some checks can be performed in the expression conversion or postfix evaluation.

Your program MUST re-prompt the user for the next infix expression. Your program must be able to process several inputs before terminating.

Deliverable Requirements

Your implementation should be contained in three files, which MUST be named makefile, stack.h, stack.hpp and in2post.cpp. Including the three files (stack.h, stack.hpp, and in2post.cpp), the makefile you use, and the analysis.txt file for time complexity of the clear() function of Stack.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.