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.
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
.
You will write the following files:
mystack.h
- contains the class definition for the mystack
class.mystack.cpp
- contains the definitions for member functions of the mystack
class.inpost.cpp
- contains your convert()
function.inpost.h
- contains the function prototype for convert()
so that the main()
can call it.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.
mystack
classThe 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:
char
. I'll refer to this data member as the stack array pointer. It will be used to dynamically allocate an array of char
(the stack array).size_t
variable used to keep track of the number of elements in the stack array. I'll refer to this data member as the stack capacity.size_t
variable used to track the number of values current stored in the stack array. I'll refer to this data member as the stack size. The stack size must always be less than or equal to the stack capacity. Unless the stack is empty, the top item in the stack is always located in the stack array at location (stack size - 1).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):
mystack::mystack()
This "default" constructor for the mystack
class should initialize a new mystack
object to an empty stack. When the function ends:
nullptr
.mystack::mystack(const mystack& x)
This "copy constructor" for the mystack
class should initialize a new mystack
object to the same values for all of its data members as the existing mystack
object x
. When the function ends:
x
.x
.nullptr
. Otherwise, the stack array pointer should point to an array of char
with a number of elements equal to the stack capacity. The array should contain the same values as the stack array of the object x
. The contents of any array elements that are not actually part of the stack are unimportant.mystack::~mystack()
The destructor should delete the stack array.
mystack& mystack::operator=(const mystack& x)
This overloaded copy assignment operator should assign one mystack
object (the object x
) to another (the object that called the member function, which is pointed to by this
). The state of the data members when the function ends should be same as described above for the copy constructor.
size_t mystack::capacity() const
This member function should return the stack capacity.
size_t mystack::size() const
This member function should return the stack size.
bool mystack::empty() const
This member function should return true
if the stack size is 0. Otherwise, it should return false
.
void mystack::clear()
This member function should set the stack size back to 0.
void mystack::reserve(size_t n)
This member function modifies an object's stack capacity without changing the stack size or the contents of the stack array. The required logic is:
n
is less than the stack size or n
is equal to the current stack capacity, simply return.n
.char
).char
. The number of elements in the new temporary array should be equal to the stack capacity.const char& mystack::top() const
This member function should return the top item in the stack. You may assume this function will not be called if the stack is empty.
void mystack::push(char value)
This member function should push the character value
onto the top of the stack. The required logic is:
reserve()
function will need to be called to increase the stack's capacity. If the current capacity is 0, the capacity should be increased to 1. Otherwise, the current capacity should be doubled.value
into the stack array as the new top item in the stack.void mystack::pop()
This member function should pop the top item off of the stack by decreasing the stack size by 1. You may assume this function will not be called if the stack is empty.
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
~
(unary negation) and ^
(exponentiation)*
(multiplication) and /
(division)+
(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.
opstack
be a stack used to hold operators during conversion.infix
be a string containing the infix (input) expression tokens.postfix
be a string where a postfix (output) expression will be
constructed.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:
'('
, push it on the opstack
.')'
, pop the opstack
until the corresponding left parenthesis is removed. Append each operator popped to the end of the postfix string and discard the '('
and ')'
parenthesis tokens.opstack
and append them to the postfix string until either a) opstack
is empty or b) the operator on opstack
has a lower precedence than the new operator token. Then push the new operator token into opstack
.When the end of the infix string is encountered, pop and append all remaining operators from opstack
and append them to the postfix string.
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 / +
Start by writing the mystack
class, making sure to test it thoroughly. As with the matrix
class from Assignment 6, you should write the member functions for this class incrementally, building and testing as you go.
The mystack
class is extremely similar in terms of its memory management to the Vector
class used as an example in the notes on dynamic storage allocation, so you can use that as your model when writing the destructor, copy constructor, and copy assignment operator. The logic for the various member functions of the class is also identical to what's outlined in the notes on array-based stacks.
Save the convert()
function for last. You may want to make a copy of infix.in
and modify it to only include a few simple infix expressions at first, adding more as you proceed.
You may want to write a function that takes a character operator and returns an integer precedence for that operator. This can be done using a simple switch
statement or something more sophisticated. The precedence of a left parenthesis character '('
should be less than that of any of the operators.
If you look at the algorithm to convert infix to postfix you will note that you never use the stack to store operands (variables or constants). You only use the stack to store operators or '('
, which are always single characters.
The way the mystack
class works, the operation "pop the stack and append what was popped to the postfix string" will actually require three steps: 1) call top()
to get the top character from the stack, 2) append that character to the postfix string (adding a space separator as needed), and 3) call pop()
to remove the top character from the stack.
In processing the infix string, you may find it useful to use the standard library functions isspace()
, isdigit()
, and islower()
, which are declared in the header file <cctype>
.
It is also worth familiarizing yourself with the member functions of the C++ string
class. For example, there are several member functions for appending to a string that might be very useful on this assignment.