Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task: Modify the provided Stack to store integers instead of characters. Use the

ID: 3629993 • Letter: T

Question

Task: Modify the provided Stack to store integers instead of characters. Use the modified stack to implement a RPN calculator. Output an appropriate error message if there are not two operands on the stack when given an operator. Use a loop to process an RPN expression, one operator/operand at a time, until a ‘q’ is entered. When the ‘q’ is entered, print the top of the stack (it contains the solution) and either exit the program or ask the user if he/she wants to continue with a new expression. Please mark the code made to the stack.h and stack.cpp in red. The only changes should be made to stack.h and stack.cpp are changes to make the stack store integers instead of characters. Please help also with the RPNcalculator.cpp. This is how far i got. At the end of the page are helpful hints and rules to help with the implementation RPNcalculator.cpp...

Sample Exuction: Here is a sample input and output based on the expresstion given.Please enter an expression in reverse polish notation. " Please enter an expression in reversee polish notation. All operands must be integers. allowed operators are + - * / %. Enter 'q' to stop entering operators/operands and to evaluate the expression.

10

2

3

+

-

2

*

5

/

q

the top of the stack is 2 : do you wish to enter and evaluate expression y/n?

n

Goodbye

File: Stack.cpp
*/

#include
#include
#include "Stack.h"

using namespace std;

namespace Stack
{

Stack::Stack() : top(NULL)
{
// body intentionally left empty
}

// copy constructor: kind of tricky.
Stack::Stack(const Stack& a_stack)
{
// simple case - the other stack is empty
if(a_stack.top == NULL)
{
top = NULL;
return;
}

StackNodePtr temp = a_stack.top;
StackNodePtr end;

end = new StackNode;
end->data = temp->data;
top = end;

// advance the temp pointer
temp = temp->link;
// as long as there are node left, copy them
while(temp != NULL)
{
end->link = new StackNode;
end = end->link;
end->data = temp->data;
temp = temp->link;
}

end->link = NULL;
}

Stack::~Stack()
{
// create a character to hold popped data
char next;
while(! empty())
{
next = pop(); // pop calls delete
}
}
bool Stack::empty()
{
return(top == NULL);
}

// add a new character to the top of the stack
void Stack::push(char the_symbol)
{
// use a temporary pointer and a new node
StackNodePtr temp = new StackNode;
temp->data = the_symbol;
temp->link = top;

// reset top to point to the new node
top = temp;
}

char Stack::pop()
{
if(empty())
{
cout << "Error: pop() called on an empty stack. ";
exit(1);
}

char ch = top->data;

StackNodePtr temp = top;
top = temp->link;
delete temp;
return ch;
}
char Stack::getTop()
{
if(empty())
{
cout << "Error: getTop() called on an empty stack. ";
exit(1);
}
return top->data;
}

void Stack::operator =(const Stack& a_stack)
{

if(top == a_stack.top)
{
return;
}
// create a character to hold popped data
char next;
while(! empty())
{
next = pop(); // pop calls delete
}
if(a_stack.top == NULL)
{
top = NULL;
return;
}
StackNodePtr temp = a_stack.top;
StackNodePtr end;

end = new StackNode;
end->data = temp->data;
top = end;

// advance the temp pointer
temp = temp->link;
// as long as there are node left, copy them
while(temp != NULL)
{
end->link = new StackNode;
end = end->link;
end->data = temp->data;
temp = temp->link;
}

end->link = NULL;
}
bool operator ==(const Stack& s1, const Stack& s2)
{
// quick check: are they either both NULL or both the same stack?
if(s1.top == s2.top)
{
return 1;
}
StackNodePtr temp1, temp2;
temp1 = s1.top;
temp2 = s2.top;

if(temp1 == NULL || temp2 == NULL) return 0;
// is the data different in the top node of either stack?
if(temp1->data != temp2->data) return 0;

while((temp1 != NULL && temp2 != NULL))
{
// advance pointers
temp1 = temp1->link;
temp2 = temp2->link;
// if neither pointer is null, compare their node's data
if(temp1 != NULL && temp2 != NULL)
{
if(temp1->data != temp2->data)
{
/
return 0;
}
}
}

return (temp1 == NULL && temp2 == NULL);
}

} // end namespace

____________________________________________________________________________________

/*
File: Stack.h
*/

#ifndef STACK_H
#define STACK_H

namespace Stack
{

// node definition
struct StackNode
{
char data;
StackNode *link;
};

// define a type for pointers to nodes
typedef StackNode* StackNodePtr;

// define the Stack Class
class Stack
{
public:
// default constructor initializes an empty stack
Stack();

Stack(const Stack& a_stack); //copy constructor

~Stack(); //destructor

void push(char the_symbol);
char pop();
bool empty();
char getTop();
void operator=(const Stack& a_stack);
friend bool operator ==(const Stack& s1, const Stack& s2);

private:
StackNodePtr top;
};

}

#endif // STACK_H

________________________________________________________________________________________

Rules for implementing an RPN calculator

The task is to implement a calculator capable of reading an expression in RPN and calculating the value of that expression. A RPN calculator can be implemented with a stack using the following rules:
-if a number is input, push it onto the stack.

-if "+" is input, then pop the last two operands off the stack, add them, and push the result onto the stack.

-If "_" is input, then pop value1, pop value2, and push value2-value1 on the stack.

-If "*" is input, then pop the last two operands off of the stack, multiply them, and pus the result onto the stack.

-if "/" is input, then pop value1, pop value2, and push value2/value1 on the stack.

-if "%" is input, then pop value1, pop value2, and push value2%value1 on the stack.

-If "q" is input, then stop inputting values, print out the top of the stack, and either exit the program or ask the user if he/she wishes to enter a new expression.

Hints

The string class's at method will tell you what character is at a given position. If your string variable is called input, then input.at(0) is the first character.

To convert a string representation of an integer to an int, use the atoi function in the cstdlib library. You must convert your string to a c_string to to this.
Example: int result = atoi(input.c_str());

Remember, you should always check the stack to make sure it is not empty before you call pop(). If the stack is empty when it shouldn't be, print an error message as appropriate. Your program should gracefully handle this case.

Explanation / Answer

// stack.h

#ifndef STACK_H

#define STACK_H

namespace stack //changes s to small letter in stack

{

// node definition

struct StackNode

{

int data; //changed datatype char to int

StackNode *link;

};

// define a type for pointers to nodes

typedef StackNode* StackNodePtr;

// define the Stack Class

class Stack

{

public:

// default constructor initializes an empty stack

Stack();

Stack(const Stack& a_stack); //copy constructor

~Stack(); //destructor

void push(int the_symbol); //changed argument type char to int

int pop(); //changed return type char to int

bool empty();

int getTop(); //changed return type char to int

void operator=(const Stack& a_stack);

friend bool operator ==(const Stack& s1, const Stack& s2);

private:

StackNodePtr top;

}; }

#endif // STACK_H

// stack.cpp

#include "stdafx.h"

#include <iostream>

#include "stack.h"

using namespace std;

namespace stack //changed s to small

{

Stack::Stack() : top(NULL)

{

// body intentionally left empty

}

// copy constructor: kind of tricky.

Stack::Stack(const Stack& a_stack)

{

// simple case - the other stack is empty

if(a_stack.top == NULL)

{

top = NULL;

return;

}

StackNodePtr temp = a_stack.top;

StackNodePtr end;

end = new StackNode;

end->data = temp->data;

top = end;

// advance the temp pointer

temp = temp->link;

// as long as there are node left, copy them

while(temp != NULL)

{

end->link = new StackNode;

end = end->link;

end->data = temp->data;

temp = temp->link;

}

end->link = NULL;

}

Stack::~Stack()

{

// create a character to hold popped data

int next; //changed data type char to int

while(! empty())

{

next = pop(); // pop calls delete

}

}

bool Stack::empty()

{

return(top == NULL);

}

// add a new character to the top of the stack

void Stack::push(int the_symbol) //changed argument the_symbol type char to int

{

// use a temporary pointer and a new node

StackNodePtr temp = new StackNode;

temp->data = the_symbol;

temp->link = top;

// reset top to point to the new node

top = temp;

}

int Stack::pop() //changed return type char to int

{

if(empty())

{

cout << "Error: pop() called on an empty stack. ";

exit(1);

}

int ch = top->data; //changed data type char to int

StackNodePtr temp = top;

top = temp->link;

delete temp;

return ch;

}

int Stack::getTop() //changed return type char to int

{

if(empty())

{

cout << "Error: getTop() called on an empty stack. ";

exit(1);

}

return top->data;

}

void Stack::operator =(const Stack& a_stack)

{

if(top == a_stack.top)

{

return;

}

// create a character to hold popped data

int next;

while(! empty())

{

next = pop(); // pop calls delete

}

if(a_stack.top == NULL)

{

top = NULL;

return;

}

StackNodePtr temp = a_stack.top;

StackNodePtr end;

end = new StackNode;

end->data = temp->data;

top = end;

// advance the temp pointer

temp = temp->link;

// as long as there are node left, copy them

while(temp != NULL)

{

end->link = new StackNode;

end = end->link;

end->data = temp->data;

temp = temp->link;

}

end->link = NULL;

}

bool operator ==(const Stack& s1, const Stack& s2)

{

// quick check: are they either both NULL or both the same stack?

if(s1.top == s2.top)

{

return 1;

}

StackNodePtr temp1, temp2;

temp1 = s1.top;

temp2 = s2.top;

if(temp1 == NULL || temp2 == NULL) return 0;

// is the data different in the top node of either stack?

if(temp1->data != temp2->data) return 0;

while((temp1 != NULL && temp2 != NULL))

{

// advance pointers

temp1 = temp1->link;

temp2 = temp2->link;

// if neither pointer is null, compare their node's data

if(temp1 != NULL && temp2 != NULL)

{

if(temp1->data != temp2->data)

{

return 0;

} } }

return (temp1 == NULL && temp2 == NULL);

} } // end namespace

// main.cpp

#include "stack.h"

#include <iostream>

#include <string>

#include <cstdlib>

using namespace std;

using namespace stack;

int main()

{

char choice = 'y';

do{

Stack st;

string input;

bool error =false;

cout<<"Please enter an expression in reversee polish notation"<<endl;

cout<<"All operands must be integers. allowed operators are + - * / %."<<endl;

cout<<"Enter 'q' to stop entering operators/operands and to evaluate the expression."<<endl;

while(1){

cin>>input;

char ch = input.at(0);

if(ch == 'q')

break;

else if(ch == '+' || ch== '-' || ch== '*' || ch == '/' || ch=='%')

{

int a,b;

if(st.empty())

{

error = true;

break;

}

b = st.pop();

if(st.empty())

{

error = true;

break;

}

a = st.pop();

int result = 0;

switch(ch)

{

case '+':result = a+b; break;

case '-':result = a-b; break;

case '*':result = a*b; break;

case '/':result = a/b; break;

case '%':result = a%b; break;

default: error = true;

}

if(!error)

st.push(result);

}

else

{

int operand = atoi(input.c_str());

st.push(operand);

}

}

if(!st.empty() && !error)

{

int top = st.pop();

if(st.empty())

cout<<" the top of the stack is: "<<top<<endl;

else

cout<<"error - in expression"<<endl;

}

else

cout<<"error - in expression"<<endl;

cout<<" do you wish to enter and evaluate expression y/n? "<<endl;

cin>>choice;

}while(choice != 'n' && choice != 'N');

cout<<"Goodbye"<<endl;

return 0;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote