Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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

NOTES:

I followed the notes that one of the experts provided for me but each file still has errors.

1.ALL FILES HAS TO BE IN SAME FOLDER(DIRECTORY). 2.SAVE THE FILES AS PER THE NAME MENTIONED(WITH CORRECT EXTENSION) ON THE FIRST LINE OF EACH PROGRAM 3.RUN/EXECUTE THE MathStackMain.cpp FILE ONLY(VERY IMPORTANT). ***************************************************************************** // save this file as 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 ******************************************************************************** // save this file as 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; } ******************************************************************************************* // save this file as 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 ******************************************************************************************* // Save this file as 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(); } } ************************************************************************************************* //This is the driver class program, save as MathStackMain.cpp and RUN THIS(MathStackMain.cpp) CLASS ONLY(IMPORTANT). // 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 3 and 6 onto the stack cout << "Pushing 2 "; stack.push(3); cout << "Pushing 3 "; stack.push(6); // 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 5 and 10 onto the stack cout << "Pushing 2 "; stack.push(5); cout << "Pushing 10 "; stack.push(10); // Divide 10 by 5. stack.div(); // Pop the quotient off the stack and display it. cout << " The Quotient is "; stack.pop(catchVar); cout << catchVar << endl< // Push 2, 5, 10, 7, 3 onto the stack cout << "Pushing 2 "; stack.push(2); cout << "Pushing 5 "; stack.push(5); cout << "Pushing 10 "; stack.push(10); cout << "Pushing 7 "; stack.push(7); cout << "Pushing 3 "; stack.push(3); // 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, 5, 4, 1 onto the stack cout << "Pushing 2 "; stack.push(2); cout << "Pushing 5 "; stack.push(5); cout << "Pushing 5 "; stack.push(5); cout << "Pushing 4 "; stack.push(4); 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; } ****************************************************************************************

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions