Question: In C++, Dynamic MathStack The MathStack class shown in this chapter only has two member functions: add and sub . Write the following additional member

In C++,

Dynamic MathStack

The MathStack class shown in this chapter only has two member functions: add and

sub . Write the following additional member functions:

Function Description

mult - Pops the top two values off the stack, multiplies them, and pushes

their product onto the stack.

div - Pops the top two values off the stack, divides the second value by

the first, and pushes the quotient onto the stack.

addAll - Pops all values off the stack, adds them, and pushes their sum

onto the stack.

multAll -Pops all values off the stack, multiplies them, and pushes their

product onto the stack.

Demonstrate the class with a driver program.

I keep getting errors! Below are 5 attached cpp files that my teacher provided for me.

1. // Implementation file for the IntStack class #include #include "IntStack.h" using namespace std; //******************* // Constructor * //******************* IntStack::IntStack(int size) { stackArray = new int[size]; stackSize = size; top = -1; } //******************* // Destructor * //******************* IntStack::~IntStack() { delete [] stackArray; stackArray = nullptr; } //************************************************* // Member function push pushes the argument onto * // the stack. * //************************************************* void IntStack::push(int num) { if (isFull()) { 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 IntStack::pop(int &num) { if (isEmpty()) { cout << "The stack is empty. "; } else { num = stackArray[top]; top--; } } //*************************************************** // Member function isFull returns true if the stack * // is full, or false otherwise. * //*************************************************** bool IntStack::isFull() { bool status = false; if (top == stackSize - 1) { status = true; } return status; } //**************************************************** // Member funciton isEmpty returns true if the stack * // is empty, or false otherwise. * //**************************************************** bool IntStack::isEmpty() { bool status = false; if (top == -1) { status = true; } return status; }
2. // Specification file for the IntStack class #ifndef INTSTACK_H #define INTSTACK_H class IntStack { protected: int *stackArray; int stackSize; int top; public: IntStack(int); ~IntStack(); void push(int); void pop(int &); bool isFull(); bool isEmpty(); }; #endif
3. // Implementation file for the MathStack class #include "MathStack.h" //*********************************************** // Member function add. add pops * // the first two values off the stack and * // adds them. The sum is pushed onto the stack. * //*********************************************** void MathStack::add() { int num, sum; pop(sum); pop(num); sum += num; push(sum); } //*********************************************** // Member functon 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. * //*********************************************** void MathStack::sub() { int num, diff; pop(diff); pop(num); diff -= num; push(diff); } //*********************************************** // Member function mult. mult pops the first * // two values off the stack and multiplies them.* // The product is pushed onto the stack. * //*********************************************** //*********************************************** // Member function div. div pops * // the first two values off the stack and * // divides the second value by the first. * // The quotient is pushed onto the stack. * //*********************************************** //************************************************ // Member function addAll. addAll pops * // the first two values off the stack and * // adds them. The sum is pushed onto the stack. * // addAll repeats this process through the stack.* //************************************************ //*********************************************** // Member function multAll. multAll pops * // the first two values off the stack and * // multiplies them. The product is pushed onto * // the stack. multAll repeats this process * // through the stack. * //*********************************************** 
 4. // Specification file for the MathStack class #ifndef MATHSTACK_H #define MATHSTACK_H #include "IntStack.h" class MathStack : public IntStack { public: MathStack(int s) : IntStack(s) {} void add(); void sub(); }; #endif
 5. // This program demonstrates the MathStack class. #include  #include "MathStack.h" using namespace std; int main() { int catchVar; // To hold values popped off the stack // Create a MathStack object. MathStack stack(5); // Push 3 and 6 onto the stack. cout << "Pushing 3 "; stack.push(3); cout << "Pushing 6 "; stack.push(6); // Add the two values. stack.add(); // Pop the sum off the stack and display it. cout << "The sum is "; stack.pop(catchVar); cout << catchVar << endl << endl; // Push 7 and 10 onto the stack cout << "Pushing 7 "; stack.push(7); cout << "Pushing 10 "; stack.push(10); // Subtract 7 from 10. stack.sub(); // Pop the difference off the stack and display it. cout << "The difference is "; stack.pop(catchVar); cout << catchVar << endl; return 0; }

Expert Answer:

// File Name: IntStack.h // Specification file for the IntStack class #ifndef INTSTACK_H #define INTSTACK_H

class IntStack { protected: // Data member to store data int *stackArray; int stackSize; int top;

public: // Prototype of member function IntStack(int); ~IntStack(); void push(int); void pop(int &); bool isFull(); bool isEmpty(); };

#endif

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

// File Name: IntStack.cpp

// Implementation file for the IntStack class #include #include "IntStack.h" using namespace std;

//******************* // Constructor * //******************* IntStack::IntStack(int size) { stackArray = new int[size]; stackSize = size; top = -1; }

//******************* // Destructor * //******************* IntStack::~IntStack() { delete [] stackArray; stackArray = NULL; }

//************************************************* // Member function push pushes the argument onto * // the stack. * //************************************************* void IntStack::push(int num) { if (isFull()) { 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 IntStack::pop(int &num) { if (isEmpty()) { cout << "The stack is empty. "; } else { num = stackArray[top]; top--; } }

//*************************************************** // Member function isFull returns true if the stack * // is full, or false otherwise. * //*************************************************** bool IntStack::isFull() { bool status = false;

if (top == stackSize - 1) { status = true; } return status; }

//**************************************************** // Member funciton isEmpty returns true if the stack * // is empty, or false otherwise. * //**************************************************** bool IntStack::isEmpty() { bool status = false;

if (top == -1) { status = true; } return status; }

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

// File Name: MathStack.h // Specification file for the MathStack class #ifndef MATHSTACK_H #define MATHSTACK_H

#include "IntStack.h"

// Class MathStack derived from IntStack class MathStack : public IntStack { public: // Prototype of member function MathStack(int s) : IntStack(s) {} void add(); void sub(); void mult(); void div(); void addAll(); void multAll(); };

#endif

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

// File Name: MathStack.cpp // Implementation file for the MathStack class #include "MathStack.h" #include "IntStack.cpp" //*********************************************** // Member function add. add pops * // the first two values off the stack and * // adds them. The sum is pushed onto the stack. * //***********************************************

void MathStack::add() { int num, sum; pop(sum); pop(num); sum += num; push(sum); }

//*********************************************** // Member functon 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. * //***********************************************

void MathStack::sub() { int num, diff; pop(diff); pop(num); diff -= num; push(diff); }

//*********************************************** // Member function mult. mult pops the first * // two values off the stack and multiplies them.* // The product is pushed onto the stack. * //*********************************************** void MathStack::mult() { int num, mul; pop(mul); pop(num); mul *= num; push(mul); }

//*********************************************** // Member function div. div pops * // the first two values off the stack and * // divides the second value by the first. * // The quotient is pushed onto the stack. * //*********************************************** void MathStack::div() { int num, div; pop(div); pop(num); div /= num; push(div); }

//************************************************ // Member function addAll. addAll pops * // the first two values off the stack and * // adds them. The sum is pushed onto the stack. * // addAll repeats this process through the stack.* //************************************************ void MathStack::addAll() { int num, sum; while(!isEmpty()) { if(top == 0) break; add(); } }

//*********************************************** // Member function multAll. multAll pops * // the first two values off the stack and * // multiplies them. The product is pushed onto * // the stack. multAll repeats this process * // through the stack. * //*********************************************** void MathStack::multAll() { int num, mul; while(!isEmpty()) { if(top == 0) break; mult(); } }

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

// File Name: MathStackMain.cpp // This program demonstrates the MathStack class. #include #include "MathStack.cpp" using namespace std;

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

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

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

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

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

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

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

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

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

// Multiply 2 with 3. stack.mult();

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

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

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

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

// Push 2, 5, 7, 3, 1 onto the stack cout << "Pushing 2 "; stack.push(2); cout << "Pushing 5 "; stack.push(5); cout << "Pushing 7 "; stack.push(7); cout << "Pushing 3 "; stack.push(3); cout << "Pushing 1 "; stack.push(1);

// Add all the values in the stack stack.addAll();

// Pop the sum off the stack and display it. cout << " Sum of all the elements in the stack "; stack.pop(catchVar); cout << catchVar << endl<

// Push 2, 5, 7, 3, 1 onto the stack cout << "Pushing 2 "; stack.push(2); cout << "Pushing 5 "; stack.push(5); cout << "Pushing 7 "; stack.push(7); cout << "Pushing 3 "; stack.push(3); cout << "Pushing 1 "; stack.push(1);

// Multiply all the values in the stack stack.multAll();

// Pop the product off the stack and display it. cout << " Product of all the elements in the stack "; stack.pop(catchVar); cout << catchVar << endl; return 0; }

Sample Output:

Pushing 3 Pushing 6 The sum is 9

Pushing 7 Pushing 10 The difference is 3

Pushing 2 Pushing 3

The Product is 6

Pushing 2 Pushing 10

The Quotient is 5

Pushing 2 Pushing 5 Pushing 7 Pushing 3 Pushing 1

Sum of all the elements in the stack 18

Pushing 2 Pushing 5 Pushing 7 Pushing 3 Pushing 1

Product of all the elements in the stack 210

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!