C++ programing Stacks and Queues (.cpp .h) modify this program as follows: 1. Im
ID: 3918620 • Letter: C
Question
C++ programing Stacks and Queues (.cpp .h)
modify this program as follows:
1. Implement it as a dynamic stack (do not use a static stack ). Use a linked list to implement this.
2. Add functionality for Mult, Div, Pop, and Display.
3. Create a looping menu-driven program to demonstrate your code so that the user has the choice of:
1. Push (an integer onto the stack)
2. Pop (an integer off the stack)
3. Add (the top two values in the stack and replace those two values with the sum)
4. Subtract (the top two values on the stack and replace those two values with the difference)
5. Mult (same as above)
6. Div (same as above but check for div by 0)
7. Display Stack
8. End
Make sure you have validation in the mutators. Watch out for division by 0 and not having enough integers on your stack to do the operation. Make sure to only allow integers on your stack. Finally, make sure main() is properly organized into functions and don't forget to document your code including your class.
MathStack.h
// Specification file for the MathStack class
#ifndef MATHSTACK_H
#define MATHSTACK_H
#include "IntStack.h"
class MathStack : public IntStack 7{
public:
MathStack(int s) : IntStack(s) {} // Constructor
void sub(); // MathStack operations void add();
void add();
}; #endif
MathStack.cpp
#include "MathStack.h"
void MathStack::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);
}
void MathStack::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);
}
Explanation / Answer
/******************************************************************************
.
*******************************************************************************/
#include <iostream>
using namespace std;
/****************************************************************************************/
struct node
{
int data;
node *next;
};
class stack
{
node *top;
public :
stack()
{ top=NULL;}
void push();
void pop();
void display();
void add();
void push1(int x);
void sub();
void mul();
void division();
~stack();
};
void stack::push()
{
node *temp;
temp=new node;
cout<<"Enter data :";
cin>>temp->data;
temp->next=top;
top=temp;
}
void stack::push1(int x)
{
node *temp;
temp=new node;
//cout<<"Enter data :";
//cin>>temp->data;
temp->data = x;
temp->next=top;
top=temp;
}
void stack::pop()
{
if(top!=NULL)
{
node *temp=top;
top=top->next;
cout<<endl<<temp->data<<"deleted"<<endl;
delete temp;
}
else
cout<<"Stack empty";
}
void stack::display()
{
node *temp=top;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
//*********************************************************************
void stack :: add()
{
node *temp=top;
if (top == NULL || temp->next == NULL) cout<< "There is no elements ";
else
{
int a,b,x;
a = temp->data;
//b = temp->data;
for (int i = 1; i < 2; i++)
{
temp = temp->next;
}
b = temp->data;
x = a + b ;
cout<<"x ="<<x;
cout<<" ";
pop();
pop();
cout<<endl;
push1(x);
push1(x);
}
}
void stack :: sub()
{
node *temp=top;
if (top == NULL || temp->next == NULL) cout<< "There is no elements ";
else
{
int a,b,x;
a = temp->data;
//b = temp->data;
for (int i = 1; i < 2; i++)
{
temp = temp->next;
}
b = temp->data;
x = a - b ;
cout<<"x ="<<x;
cout<<" ";
pop();
pop();
cout<<endl;
push1(x);
push1(x);
}
}
void stack :: mul()
{
node *temp=top;
if (top == NULL || temp->next == NULL) cout<< "There is no elements ";
else
{
int a,b,x;
a = temp->data;
//b = temp->data;
for (int i = 1; i < 2; i++)
{
temp = temp->next;
}
b = temp->data;
x = a * b ;
cout<<"x ="<<x;
cout<<" ";
pop();
pop();
cout<<endl;
push1(x);
push1(x);
}
}
void stack :: division()
{
node *temp=top;
if (top == NULL || temp->next == NULL) cout<< "There is no elements ";
else
{
int a,b,x;
a = temp->data;
//b = temp->data;
for (int i = 1; i < 2; i++)
{
temp = temp->next;
}
b = temp->data;
if(b == 0)
{
cout<<"Division by 0 is not possible..";
}
else
{
x = a / b ;
cout<<"x ="<<x;
cout<<" ";
pop();
pop();
cout<<endl;
push1(x);
push1(x);
}
}
}
//***********************************************************************
stack::~stack()
{
while(top!=NULL)
{
node *temp=top;
top=top->next;
delete temp;
}
}
/****************************************************************************************/
int main()
{
stack st;
while(1)
{
int selection;
cout<<" Menu";
cout<<" ========";
cout<<" 1 - Push";
cout<<" 2 - Pop";
cout<<" 3 - Add";
cout<<" 4 - Subtract";
cout<<" 5 - Multiply ";
cout<<" 6 - Division ";
cout<<" 7 - Display ";
cout<<" 8 - End ";
cout<<" Enter selection: ";
// read the input
cin>>selection;
switch(selection)
{
case 1 : st.push();break;
case 2 : st.pop();break;
case 3 : st.add();break;
case 4 : st.sub();break;
case 5 : st.mul();break;
case 6 : st.division();break;
case 7 : st.display();break;
case 8: exit(0); break;
}
cout<<" ";
} // end of while
return 0;
}//end of main
OUTPUT
Menu
========
1 - Push
2 - Pop
3 - Add
4 - Subtract
5 - Multiply
6 - Division
7 - Display
8 - End
Enter selection: 1
Enter data :10
Menu
========
1 - Push
2 - Pop
3 - Add
4 - Subtract
5 - Multiply
6 - Division
7 - Display
8 - End
Enter selection: 1
Enter data :20
Menu
========
1 - Push
2 - Pop
3 - Add
4 - Subtract
5 - Multiply
6 - Division
7 - Display
8 - End
Enter selection: 1
Enter data :30
Menu
========
1 - Push
2 - Pop
3 - Add
4 - Subtract
5 - Multiply
6 - Division
7 - Display
8 - End
Enter selection: 7
30 20 10
Menu
========
1 - Push
2 - Pop
3 - Add
4 - Subtract
5 - Multiply
6 - Division
7 - Display
8 - End
Enter selection: 5
x =600
30deleted
20deleted
Menu
========
1 - Push
2 - Pop
3 - Add
4 - Subtract
5 - Multiply
6 - Division
7 - Display
8 - End
Enter selection:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.