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.
Below are 5 attached cpp files that my teacher provided for me. Show all work and show the screenshots!
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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started