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

Please Code in C++, PLEASE code stack.cpp, Stack.hpp, NumStack.cpp, and NumStack

ID: 3727530 • Letter: P

Question

Please Code in C++, PLEASE code stack.cpp, Stack.hpp, NumStack.cpp, and NumStack.hpp using the main.cpp as the TEST CASE

-----------------------------------------

For the assignment, we are given a Stack class that has only has two member functions: add and sub. You will see also the specification file and implementation file for the NumStack class. The stack is implemented as an array of the type integer.

For this assignment, you will need to provide these public member functions that perform the following tasks:

a) mult Pops the top two values off the stack, multiplies them, and pushes their product onto the stack.

b) div Pops the top two values off the stack, divides the second value by the first, and pushes the quotient onto the stack.

c) addAll Pops all values off the stack, adds them, and pushes their sum onto the stack.

d) multAll Pops all values off the stack, multiplies them, and pushes their product onto the stack.

Next, you will need modify the NumStack class to that of a template that will create this stack for any data type.

There are plenty of specification and implementation files in this assignment’s folder. There is also the original driver program as well, called main. Modify the driver to include 2 cases of each of the 4 new member functions, and then make sure your program can execute the test cases successfully.

(TEST CASE) main:

#include <iostream>
#include "Stack.hpp"


int main()
{
int catchVar; // To hold values popped off the stack

// Create a MathStack object.
Stack stack(5);

// Push 3 and 6 onto the stack.
std::cout << "Pushing 3 ";
stack.push(3);
std::cout << "Pushing 6 ";
stack.push(6);

// Add the two values.
stack.add();

// Pop the sum off the stack and display it.
std::cout << "The sum is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl << std::endl;

// Push 7 and 10 onto the stack
std::cout << "Pushing 7 ";
stack.push(7);
std::cout << "Pushing 10 ";
stack.push(10);

// Subtract 7 from 10.
stack.sub();

// Pop the difference off the stack and display it.
std::cout << "The difference is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl;
return 0;
}

-------------------------------------------------------

Stack.hpp:

#ifndef STACK_HPP
#define STACK_HPP
#include "NumStack.hpp"

class Stack : public NumStack
{
public:
// Constructor
Stack(int s) : NumStack(s) {}

// MathStack operations
void add();
void sub();
};

#endif // STACK_HPP_INCLUDED

--------------------------------------------------

NumStack.hpp:

#ifndef NUMSTACK_HPP_INCLUDED
#define NUMSTACK_HPP_INCLUDED

class NumStack
{
private:
int *stackArray; // Pointer to the stack array
int stackSize; // The stack size
int top; // Indicates the top of the stack

public:
// Constructor
NumStack(int);

// Copy constructor
NumStack(const NumStack &);

// Destructor
~NumStack();

// Stack operations
void push(int);
void pop(int &);
bool isFull() const;
bool isEmpty() const;
};

#endif // NUMSTACK_HPP_INCLUDED

------------------------------------------------------------------

stack.cpp:

#include "stack.hpp"

//***********************************************
// Member function add. add pops *
// the first two values off the stack and *
// adds them. The sum is pushed onto the stack. *
// pre: no parameters *
// post: return the new stack entry of sum *
//***********************************************

void Stack::add()
{
int num, sum;

// Pop the first two values off the stack.
pop(sum);
pop(num);

// Add the two values, store in sum.
sum += num;

// Push sum back onto the stack.
push(sum);
}

//***********************************************
// Member function sub. sub pops the *
// first two values off the stack. The *
// second value is subtracted from the *
// first value. The difference is pushed *
// onto the stack. *
// pre: no parameters *
// post: return the new stack entry of diff *
//***********************************************

void Stack::sub()
{
int num, diff;

// Pop the first two values off the stack.
pop(diff);
pop(num);

// Subtract num from diff.
diff -= num;

// Push diff back onto the stack.
push(diff);
}

---------------------------------------------------------

NumStack.cpp:

#include
#include "NumStack.hpp"


//***********************************************
// Constructor *
// This constructor creates an empty stack. The *
// size parameter is the size of the stack. *
//***********************************************

NumStack::NumStack(int size)
{
stackArray = new int[size];
stackSize = size;
top = -1;
}

//***********************************************
// Copy constructor *
//***********************************************

NumStack::NumStack(const NumStack &obj)
{
// Create the stack array.
if (obj.stackSize > 0)
stackArray = new int[obj.stackSize];
else
stackArray = nullptr;

// Copy the stackSize attribute.
stackSize = obj.stackSize;

// Copy the stack contents.
for (int count = 0; count < stackSize; count++)
stackArray[count] = obj.stackArray[count];

// Set the top of the stack.
top = obj.top;
}

//***********************************************
// Destructor *
//***********************************************

NumStack::~NumStack()
{
delete [] stackArray;
}

//*************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************

void NumStack::push(int num)
{
if (isFull())
{
std::cout << "The stack is full. ";
}
else
{
top++;
stackArray[top] = num;
}
}

//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************

void NumStack::pop(int &num)
{
if (isEmpty())
{
std::cout << "The stack is empty. ";
}
else
{
num = stackArray[top];
top--;
}
}

//***************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//***************************************************

bool NumStack::isFull() const
{
bool status;

if (top == stackSize - 1)
status = true;
else
status = false;

return status;
}

//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************

bool NumStack::isEmpty() const
{
bool status;

if (top == -1)
status = true;
else
status = false;

return status;
}

Explanation / Answer

Please find the code below with detailed inline comments.

CODE

///////////////////////////////////////////////

NumStack.hpp

*************************

#ifndef NUMSTACK_HPP_INCLUDED
#define NUMSTACK_HPP_INCLUDED

template <class T>
class NumStack
{
private:
T *stackArray; // Pointer to the stack array
int stackSize; // The stack size
int top; // Indicates the top of the stack

public:
// Constructor
NumStack(T);

// Copy constructor
NumStack(const NumStack &);

// Destructor
~NumStack();

// Stack operations
void push(T);
void pop(T &);
bool isFull() const;
bool isEmpty() const;
};

#endif // NUMSTACK_HPP_INCLUDED

NumStack.cpp

*************************

#include <iostream>
#include "NumStack.hpp"


//***********************************************
// Constructor *
// This constructor creates an empty stack. The *
// size parameter is the size of the stack. *
//***********************************************

template <class T>
NumStack<T>::NumStack(int size)
{
stackArray = new T[size];
stackSize = size;
top = -1;
}

//***********************************************
// Copy constructor *
//***********************************************

template <class T>
NumStack<T>::NumStack(const NumStack &obj)
{
// Create the stack array.
if (obj.stackSize > 0)
stackArray = new T[obj.stackSize];
else
stackArray = nullptr;

// Copy the stackSize attribute.
stackSize = obj.stackSize;

// Copy the stack contents.
for (int count = 0; count < stackSize; count++)
stackArray[count] = obj.stackArray[count];

// Set the top of the stack.
top = obj.top;
}

//***********************************************
// Destructor *
//***********************************************

template <class T>
NumStack<T>::~NumStack()
{
delete [] stackArray;
}

//*************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************

template <class T>
void NumStack<T>::push(T num)
{
if (isFull())
{
std::cout << "The stack is full. ";
}
else
{
top++;
stackArray[top] = num;
}
}

//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************

template <class T>
void NumStack<T>::pop(T &num)
{
if (isEmpty())
{
std::cout << "The stack is empty. ";
}
else
{
num = stackArray[top];
top--;
}
}

//***************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//***************************************************

template <class T>
bool NumStack<T>::isFull() const
{
bool status;

if (top == stackSize - 1)
status = true;
else
status = false;

return status;
}

//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************

template <class T>
bool NumStack<T>::isEmpty() const
{
bool status;

if (top == -1)
status = true;
else
status = false;

return status;
}

Stack.hpp

*************************

#ifndef STACK_HPP
#define STACK_HPP
#include "NumStack.hpp"

class Stack : public NumStack<int>
{
public:
// Constructor
Stack(int s) : NumStack(s) {}

// MathStack operations
void add();
void sub();
void mult();
void div();
void addAll();
void multAll();
};

#endif // STACK_HPP_INCLUDED

Stack.cpp

*************************

#include "Stack.hpp"

//***********************************************
// Member function add. add pops *
// the first two values off the stack and *
// adds them. The sum is pushed onto the stack. *
// pre: no parameters *
// post: return the new stack entry of sum *
//***********************************************

void Stack::add()
{
int num, sum;

// Pop the first two values off the stack.
pop(sum);
pop(num);

// Add the two values, store in sum.
sum += num;

// Push sum back onto the stack.
push(sum);
}

//***********************************************
// Member function sub. sub pops the *
// first two values off the stack. The *
// second value is subtracted from the *
// first value. The difference is pushed *
// onto the stack. *
// pre: no parameters *
// post: return the new stack entry of diff *
//***********************************************

void Stack::sub()
{
int num, diff;

// Pop the first two values off the stack.
pop(diff);
pop(num);

// Subtract num from diff.
diff -= num;

// Push diff back onto the stack.
push(diff);
}

//***********************************************
// Member function mult. mult pops the *
// first two values off the stack. The *
// second value is multipled with the *
// first value. The product is pushed *
// onto the stack. *
// pre: no parameters *
// post: return the new stack entry of mult *
//***********************************************

void Stack::mult()
{
int num, mul;

// Pop the first two values off the stack.
pop(mul);
pop(num);

// multiply num by mul.
mul *= num;

// Push product back onto the stack.
push(mul);
}

//***********************************************
// Member function div. div pops the *
// first two values off the stack. The *
// second value is divided by the *
// first value. The quotient is pushed *
// onto the stack. *
// pre: no parameters *
// post: return the new stack entry of div *
//***********************************************

void Stack::div()
{
int num, quot;

// Pop the first two values off the stack.
pop(num);
pop(quot);

// divide d by num.
quot /= num;

// Push quotient back onto the stack.
push(quot);
}

//***********************************************
// Member function addAll. addAll pops the *
// all the values off the stack and adds all of them.
// The sum is pushed *
// onto the stack. *
// pre: no parameters *
// post: return the new stack entry of addAll *
//***********************************************

void Stack::addAll()
{
int sum = 0;
while(isEmpty() != true) {
int num;
pop(num);
sum += num;
}
push(sum);
}

//***********************************************
// Member function multAll. multAll pops the *
// all the values off the stack and multiples all of them.
// The product is pushed *
// onto the stack. *
// pre: no parameters *
// post: return the new stack entry of multAll *
//***********************************************

void Stack::multAll()
{
int product = 1;
while(isEmpty() != true) {
int num;
pop(num);
product *= num;
}
push(product);
}

main.cpp

*************************

#include <iostream>
#include "Stack.cpp"

int main()
{
int catchVar; // To hold values popped off the stack

// Create a MathStack object.
Stack stack(5);

// Push 3 and 6 onto the stack.
std::cout << "Pushing 3 ";
stack.push(3);
std::cout << "Pushing 6 ";
stack.push(6);

// Add the two values.
stack.add();

// Pop the sum off the stack and display it.
std::cout << "The sum is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl << std::endl;

// Push 7 and 10 onto the stack
std::cout << "Pushing 7 ";
stack.push(7);
std::cout << "Pushing 10 ";
stack.push(10);

// Subtract 7 from 10.
stack.sub();

// Pop the difference off the stack and display it.
std::cout << "The difference is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl;

// Push 12 and 8 onto the stack
std::cout << "Pushing 12 ";
stack.push(12);
std::cout << "Pushing 8 ";
stack.push(8);

// Multiply 12 by 8.
stack.mult();

// Pop the product off the stack and display it.
std::cout << "The product is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl;

// Push 20 and 10 onto the stack
std::cout << "Pushing 20 ";
stack.push(20);
std::cout << "Pushing 10 ";
stack.push(10);

// Divide 20 by 10.
stack.div();

// Pop the quotient off the stack and display it.
std::cout << "The quotient is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl;

// Add all the elements of the stack
stack.addAll();
// Pop the sum off the stack and display it.
std::cout << "The sum is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl;


// Push 10, 20 and 30 onto the stack
std::cout << "Pushing 10 ";
stack.push(10);
std::cout << "Pushing 20 ";
stack.push(20);
std::cout << "Pushing 30 ";
stack.push(30);

// multiply all the elements of the stack
stack.multAll();

// Pop the quotient off the stack and display it.
std::cout << "The product of all the elements is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl;

return 0;
}