Stackprogramdesign

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

In2Post.

cpp
CS 121 Bolden --------- g++ (gcc) 11.2.0 --------- Luke Maupin
3/3/2023 --------- Windows 10, Ryzen 5 3600 --------- maup8702@vandals.uidaho.edu

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

4:00 Finished the set up and I am ready to start the assignment

3/2/2023

5:00 Started working on the In2Post.cpp files

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.

6:47 I have coded up to step 15 in the algorithm.

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:26 Added further detail to my comments

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"

using namespace std;

// 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;

cout << "Type \'y\' to do another or \'n\' to quit: ";


cin >> restart;
cin.ignore(); // fixes an issue I had with getline() upon looping through the program more than once
}while(restart != "n"); // loops again if the user doesn't input 'n'

system("Pause");
}

string conversion(string str)


{
int i = 0; // keeps track of what character we are on in the infix string
char c; // variable that stores the character that gets popped off the stack
string postfix; // string to store postfix
Stack stack; // creates our stack

// Infix to Postfix Algorithm


stack.Push('('); // Pushs '(' onto the stack

str += ')'; // adds ')' to the end of the infix

while(!stack.IsEmpty()) // loops while the stack isn't empty


{
if(str[i] == '(')
{
stack.Push(str[i]); // pushes the character from the string onto the stack if the character is '('
}else if(str[i] >= '0' && str[i] <= '9'){
postfix += str[i]; // adds the character to the postfix if it is a number 0 - 9
}else if(str[i] == ')'){
char c = stack.Pop(); // stores the variable that pops off the stack when the character is ')'
while(c != '(')
{
postfix += c;
c = stack.Pop(); // when c isn't '(', adds c to the postfix then stores the variable that pops off the stack
}
// basically does nothing to account for if the user adds spaces in between different parts of the infix
}else if(str[i] == ' '){
postfix = postfix;
}else{
while(opPrecedence(stack.Peek()) >= opPrecedence(str[i]))
{
c = stack.Pop(); // stores the variable that pops off the stack
postfix += c; // adds that variable to postfix
}
stack.Push(str[i]); // pushes the character of the string onto the stack
}

i++; // increments to the next character of the infix string


}

return postfix;
}

int opPrecedence(char x) // applies P.E.M.D.A.S. to the operators


{
if(x == '*' || x == '/')
{
return 2;
}else if(x == '+' || x == '-')
{
return 1;
}else{
return 0;
}
}

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();

void Push(char n); // push item onto stack


char Pop(); // remove item from stack
char IsEmpty(); // is the stack empty?
void Print(); // print the stack
char Peek(); // look at the top of the 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>

using namespace std;

class LinkedList
{
private:

struct node
{
char info;
node * next;
};

typedef node * nodeptr;


nodeptr start;
int count;

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.

void AddNode(char x);

// Delete the first node found with the value x, if one exists.

void DeleteNode(char x);

// Return the first node found in the list

int FirstNode();

// Output the values in the nodes, one integer per line.

void PrintNodes();

// Return true if there in a node in the list with the value x.

//bool IsInList(int x);


int IsInList(char x);

// Return a count of the number of nodes in the list.

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"

// Add an item to the FRONT of the list


void LinkedList::AddNode( char x )
{
nodeptr n;

// allocate new node


n = new node;
n->info = x;
count++;

if( start == NULL )


{
start = n;
n->next = NULL;
}
else
{
nodeptr tmp = start;
n->next = tmp;
start = n;
}
}

void LinkedList::DeleteNode( char x )


{
nodeptr prev, curr;

curr = start;

while( curr != NULL && x > curr->info )


{
prev = curr;
curr = curr->next;
}

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;
}

//\end{verbatim} \lecpb \begin{verbatim}


#ifdef NOT_USING_CC_WHATEVER_ITS_PREDEF_IS
bool LinkedList::IsInList(int x)
#else
int LinkedList::IsInList(char x)
#endif
{
nodeptr p = start;

while( p != NULL && x > p->info )


p = p->next;

return (x == p->info);
}

int LinkedList::Size()
{
return count;
}

Output

You might also like