Stackprogramdesign
Stackprogramdesign
cpp
CS 121 Bolden --------- g++ (gcc) 11.2.0 --------- Luke Maupin
3/3/2023 --------- Windows 10, Ryzen 5 3600 --------- [email protected]
Overview:
In this program, the user will enter an equation in infix notation, which will then be saved
as string. This string will be sent through an algorithm that converts it into the postfix notation
through the use of another string and stacks.
1. Stack.h, stack.cpp, list.h, and list.cpp are included into the In2Post.cpp file which allows
the use of stacks.
● The stack class provided by these files will be especially important. It will give
me the ability to use the Push (adds item onto the stack), Pop (takes an item off
the stack), IsEmpty (checks to see if the stack is empty or not), Print (prints out
the stack), and Peek (checks what the item on the top of the stack is) methods.
2. The user will enter the infix equation and it will be stored into a string. There will be
some conditions to prevent the user from doing invalid operations like dividing by 0.
3. The string will then be sent into the conversion function that switches it from infix
notation to postfix notation.
4. Conversion function:
● This is where a string for the postfix notation will be, along with where the stack
will be created.
● The infix string will then be walked through character by character to convert it
into postfix notation through the use of the provided infix to postfix algorithm.
● Once the algorithm is completed, the function will return the postfix string.
5. The postfix string will then be printed.
6. This process will loop until the user inputs the keyword that exits the loop.
I estimate that it will take me around 5 hours to fully implement the program and fix any issues
that come up during the process.
Programming Log:
3/1/2023
3:30 Starting by getting the stack and list files from provide class notes and modifying them to fit
the assignment
3/2/2023
5:35 I found out more about strings and the syntax required to use them. So far I have the user
input the infix, assign that to a string, then send that string into the conversion function.
5:50 So far I have the first two steps of the postfix algorithm. I learned that you could just use +=
to add a character to a string. I thought it would be much more complicated.
5:55 I am going to begin working on the while loop the algorithm goes into.
7:00 I figured out step 15 by making a separate function to determine the operator precedence
7:10 I finished the algorithm but it only worked properly if the infix had no spaces in it. So, I
added another else if to account for the spaces.
8:30 Started work on polishing the start of the program where it asks for input, and added the
loop that will keep the program going until the user types 'n' to quit.
8:57 I added a loop that will continuously keep asking the user for input until they don't divide 0.
9:45 Did some final testing and I am calling the program complete
Source code:
In2Post.cpp
#include <iostream>
#include "list.cpp"
#include "stack.cpp"
// Prototypes
string conversion(string);
int opPrecedence(char);
int main()
{
string restart; // variable that keeps track of if the user wants to run the program again or not
string infix; // variable that keeps track of the user inputted equation
int isValid; // integer that keeps track of if the user has a division by zero in their equation
do
{
cout << "Infix: ";
getline(cin, infix); // takes user's input and puts it into the infix string
// goes through user's infix and if there is a divison by 0 it will set isValid to something other than -1
isValid = infix.find("/ 0");
while(isValid != -1) // loops until user enters an equation that doesn't divide by zero
{
cout << "Don't divide by zero: ";
getline(cin, infix); // retakes user's input
isValid = infix.find("/ 0"); // checks to see if there is division by zero
}
// sends the infix through the conversion function then prints the results
cout << "Postfix: " << conversion(infix) << endl;
system("Pause");
}
return postfix;
}
Stack.h
// Code from course website expanded upon
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include <assert.h>
#include "list.h"
class Stack {
public:
Stack();
~Stack();
private:
LinkedList topPtr; // pointer to list
};
#endif
Stack.cpp
// Code from course website expanded upon
#include <iostream>
#include <assert.h>
#include "stack.h"
Stack::Stack()
{
}
Stack::~Stack()
{
//delete topPtr;
while( !IsEmpty() ) {
char n = topPtr.FirstNode();
topPtr.DeleteNode( n );
}
}
void Stack::Push(char n)
{
topPtr.AddNode( n );
}
char Stack::Pop()
{
assert(!IsEmpty());
char n = topPtr.FirstNode();
topPtr.DeleteNode( n );
return n;
}
char Stack::IsEmpty()
{
int n = topPtr.Size();
return (n == 0);
}
void Stack::Print()
{
topPtr.PrintNodes();
}
char Stack::Peek()
{
char n = topPtr.FirstNode();
return n;
}
List.h
// Code from course website expanded upon
#ifndef LINK_H
#define LINK_H
#ifdef NOT_USING_CC_WHATEVER_ITS_PREDEF_IS
#include <bool.h>
#endif
#include <iostream>
class LinkedList
{
private:
struct node
{
char info;
node * next;
};
public:
// Constructor
LinkedList()
{
start = NULL;
count = 0;
}
// Destructor
~LinkedList()
{
nodeptr p = start, n;
while (p != NULL)
{
n = p;
p = p->next;
delete n;
}
}
// Add a node onto the front of the linked list.
// Delete the first node found with the value x, if one exists.
int FirstNode();
void PrintNodes();
int Size();
};
#endif
List.cpp
// Code from course website expanded upon
#ifdef NOT_USING_CC_WHATEVER_ITS_PREDEF_IS
#include <bool.h>
#endif
#include <iostream>
#include "list.h"
curr = start;
if( x == curr->info )
{
if( curr == start )
start = start->next;
else
prev->next = curr->next;
delete curr;
count--;
}
}
int LinkedList::FirstNode()
{
return start->info;
}
void LinkedList::PrintNodes()
{
nodeptr p = start;
while( p != NULL )
{
cout << p->info << endl;
p = p->next;
}
return (x == p->info);
}
int LinkedList::Size()
{
return count;
}
Output