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

Dynamic MathStack The MathStack class shown in this chapter only has two member

ID: 3787981 • Letter: D

Question

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. (The folowing code must be done in C++)

Explanation / Answer

#include <iostream>

using namespace std;

//-- set maximum length of stack
int Maxlenght=10;

class Mathstack
{
    private:
        int *StackArray;
        int top;
    public:
        Mathstack();
        void Push(int);
        void Pop(int);
        void Multiply();
        void Divide();
        void addAll();
        void MultiplyAll();
        bool IsEmpty();
        bool IsFull();
};

/*
*   Initiate stack poniter
*/
Mathstack::Mathstack()
{
    StackArray= new int[Maxlenght];
    top=-1;
}

/*
*   To check whether the stack is empty or not with return type boolean
*/
bool Mathstack::IsEmpty()
{
    if (top==-1) return 1;
    else return 0;
}

/*
*   To check whether the stack is full or not with return type boolean
*/
bool Mathstack::IsFull()
{
    if (top==Maxlenght-1) return 1;
    else return 0;
}

/*
*   To push the value into stack
*/
void Mathstack::Push(int number)
{
    top++;
    StackArray[top]=number;
}

/*
*   To pop the value from stack
*/
void Mathstack::Pop (int number)
{
    number=StackArray[top];
    top--;
}

/*
*    Multiply last 2 values which is stack
*    Return type void
*/
void Mathstack::Multiply()
{
    int fnumber=StackArray[top],snumber=StackArray[--top];
    Pop(snumber);
    Pop(fnumber);
    snumber*=fnumber;
    Push(snumber);
    cout<< snumber;
}

/*
* Divide last 2 values which is stack
* Return type void
*/
void Mathstack::Divide()
{
    int fnumber=StackArray[top],snumber=StackArray[--top];
    Pop(snumber);
    Pop(fnumber);
    snumber/=fnumber;
    Push(snumber);
    cout<< snumber;
}

/*
* Add all values which is stack
* Return type void
*/
void Mathstack::addAll()
{
    int avalues;
    while (!IsEmpty())
    {
        avalues += StackArray[top];
        top--;
    }
    Push(avalues);
    cout << avalues;
}

/*
* Multiply all values which is stack
* Return type void
*/
void Mathstack::MultiplyAll()
{
    int mvalues = 1;
    while (!IsEmpty())
    {
        mvalues *= StackArray[top];
        top--;
    }
    Push(mvalues);
    cout << mvalues;
}


int main()
{
    Mathstack optStack;
    Mathstack();
      
    //-- Used sample values, you can use 'cin' option to get input and then push it to stack.
    //int number;
    //cout<<"Please fill the stack...";
    //cin >> number; optStack.Push(number);
  
    optStack.Push(3);
    optStack.Push(10);
    optStack.Push(5);
  
    //-- Output
    cout<<"The expected output is...";
    optStack.MultiplyAll();
}