Assignment 7: Infix and Postfix Expressions (20 points)

In this assignment, you will write one class and a function that will convert an infix expression to an equivalent postfix expression. You will not write a main() routine. One will be supplied for you and it will call your conversion function.

Files We Give You

A suitable Makefile, inpost_main.cpp, a sample input file called infix.in, and the correct output generated by that input file postfix.key is available on hopper in this directory:

/home/hopper/winans/501/a7/

Use diff to verify that your output matches that found in postfix.key.

Files You Must Write

You will write the following files:

Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in the same source file.

The mystack class

The mystack class represents a stack of characters implemented as an array. This stack will be used by the algorithm that converts an infix string to a postfix string.

Like the other classes we've written this semester, this class should be implemented as two separate files. The class definition should be placed in a header file called mystack.h.

Data Members

The mystack class should contain the following private data members:

The data type size_t is defined in several header files, including <cstddef> and <cstdlib>.

In addition to the data members described above, your class definition will need prototypes for the public member functions described below.

Member Functions

The definitions for the member functions of the class should be placed in a separate source code file called mystack.cpp. Make sure to #include "mystack.h" at the top of this file.

The mystack class should have the following member functionss (most of which are quite small):

Important Point

Some of the member functions of the mystack class will not be used in this assignment. However, you are still required to write them, and you should expect to lose points if they do not work correctly. Thoroughly testing the member functions of this class to make sure that they work is your responsibility.

inpost.cpp

This file should contain a definition for the following function:

    string convert(const string& infix)

This function converts the infix expression passed to it as a C++ string into an equivalent postfix expression stored in a string object. You may assume that infix is a valid expression, that only '(' and ')' will be used (i.e., no '{}' or '[]' will appear in the expression), that all variables will be single characters always in lower case, and that all constants will be integers.

The operators used, in order of precedence from highest to lowest, are

  1. ~ (unary negation) and ^ (exponentiation)
  2. * (multiplication) and / (division)
  3. + (addition) and - (subtraction)

Your function must take the expression in infix (which is a C++ string that may contain unnecessary whitespace and/or parentheses) and convert it to a postfix string stored in a mystring object. It is very important that the postfix expression does not contain any leading or trailing whitespace and that the operators/operands are separated by exactly one space.

Infix to postfix conversion algorithm

Here is a description of the logic for the infix to postfix conversion using a stack.

To convert the infix expression to a postfix expression:

Scan the infix string from left to right, extracting and processing one token at a time, skipping over whitespace:

When the end of the infix string is encountered, pop and append all remaining operators from opstack and append them to the postfix string.

Output

The only output from this program is generated by the main routine supplied for you in inpost_main.cpp.

The following is a sample of what the given main() function will output when you run your finished program.

  infix: ( d +1) *2
postfix: d 1 + 2 *

  infix: a-e-a
postfix: a e - a -

  infix: (a-e-a)/(  ~d + 1)
postfix: a e - a - d ~ 1 + /

  infix: (a^2 + ~b ^ 2) * (5 - c)
postfix: a 2 ^ b ~ 2 ^ + 5 c - *

  infix: ~ 3*~(a+1)- b/c^2
postfix: 3 ~ a 1 + ~ * b c 2 ^ / -

  infix: 246 + b /123
postfix: 246 b 123 / +

  infix: ( 246+(( b /123) ) )
postfix: 246 b 123 / +

Hints