Question

C++

Carefully review Program 19-2 in your textbook, MathStack. This is a static stack, meaning that the size of the stack is set at the beginning of the program (see line 11): MathStack stack(5).

I would like you to modify this program as follows:

1. Implement it as a dynamic stack (do not use a static stack as it is designed in the book). Use a linked list to implement this. There's code in the book.

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:
- a. Push (an integer onto the stack)
- b. Pop (an integer off the stack)
- c. 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 proper validation throughout your code. 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.

Please submit:
1. .cpp and .h files

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

Validation is a bit different in this program since, by default, if you are inputting an integer, then it will accept an integer. For example,

int var1;

cout<<"Please enter an integer: ";

cin>>var1;

if the user enters 5.6, your program will put 5 into var1.


However, there are still areas where you have to be careful:

1. Division by 0 is not allowed.

2. Not having enough integers on your stack to do the operation.

Make sure you have proper validation throughout your code to handle these situations. You can decide how to handle them but I don't want to see your program crash and burn because of either of these situations.

Finally, make sure main() is properly organized into functions and don't forget to document your code including your class.

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

Program 19-2 1 This program demonstrates the MathStack class. #1 nclude <iostream> 3 #include MathStack.h 4 using namespace

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code has been tested and submitted as below.

MathStack class and main functions are inside the same file hence no need of external .h

other used default headers are iostream, stdlib.h

********************************************************************************************

#include<stdlib.h>
#include<iostream>

#define INT_MIN 0

using namespace std;

class MathStack
{
public:
int data;
MathStack* next;
};

MathStack* newNode(int data)
{
   MathStack* mathStack = new MathStack();
   mathStack->data = data;
   mathStack->next = NULL;
   return mathStack;
}

int isEmpty(MathStack *root)
{
   return !root;
}

void push(MathStack** root, int data)
{
   MathStack* mathStack = newNode(data);
   mathStack->next = *root;
   *root = mathStack;
}

int pop(MathStack** root)
{
   if (isEmpty(*root))
   return INT_MIN;
   MathStack* temp = *root;
   *root = (*root)->next;
   int popped = temp->data;
   free(temp);

   return popped;
}

int display(MathStack* root)
{
   if (isEmpty(root))
   return INT_MIN;
   return root->data;
}



int main(){

MathStack* root = NULL;
char ch;
do{
int n;
int num,num1,num2;

cout<<"ENTER CHOICE\n"<<"1.Push\n"<<"2.Pop\n"<<"3.Add\n"<<"4.Subtract\n"<<"5.Mul\n"<<"6.Div\n"<<"7.Display\n"<<"8.End\n";
cout<<"Make a choice: ";
cin>>n;

switch(n){
case 1:
cout<<"Enter the element to be pushed : \n";
cin>>num;

//push data into the stack
push(&root,num);
break;

case 2 :
//pop data from stack
  
cout<"The popped number is \n";
cout<<pop(&root)<<endl;
break;

case 3 :
//Add data from stack
num1 = pop(&root);
num2 = pop(&root);
push(&root,(num1+num2));
  
cout<"The addition of two numbers is \n";
cout<<pop(&root)<<endl;
break;

case 4 :
//Subtract data from stack

num1 = pop(&root);
num2 = pop(&root);
if(num1>num2){
push(&root,(num1-num2));
}else{
push(&root,(num2-num1));
}
  
cout<"The difference in two numbers is \n";
cout<<pop(&root)<<endl;
break;

case 5 :
//Multiply data from stack
num1 = pop(&root);
num2 = pop(&root);
push(&root,(num1*num2));
  
cout<"The muliplication of two numbers is \n";
cout<<pop(&root)<<endl;
break;

case 6 :
//Divide data from stack
num1 = pop(&root);
num2 = pop(&root);
if(num2 == 0){
push(&root,0);
}else{
push(&root,(num1/num2));
}
cout<"The division of two numbers is \n";
cout<<pop(&root)<<endl;

break;

case 7 :
//display data
cout<<display(root)<<endl;
break;
  
case 8 :
   exit(0);
   break;

default :
cout<<"Invalid Choice\n";
}

cout<<"Do you want to continue ? : ";
cin>>ch;

}while(ch=='Y'||ch=='y');

return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Carefully review Program 19-2 in your textbook, MathStack. This is a static stack, meaning that the size of the stack is set at the beginning of the program (see line 11): MathStack stack(5). I wo...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a queue and stack class program that deals with a palindrome, I need someone...

    I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...

  • Must be written in C++ Please Lab 11 Due Date: April 25, 2019 Total Points: 15...

    Must be written in C++ Please Lab 11 Due Date: April 25, 2019 Total Points: 15 points The purpose of this lab is to implement and test a static and dy namic stack Part 1: Static Stack In this part, you are going to design a stack of characters. Assume a simple static array implementation. Complete the code below as specified by the comments below const int MAX- 5 // define an alias for the element type class Stack private:...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • Please answer in C++. Derive a class called Stack from the linked list described in Assignment...

    Please answer in C++. Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions...

  • C++ programming help. The only change you have to make is in current program, where it...

    C++ programming help. The only change you have to make is in current program, where it says "your code goes here", do not make any changes to any other files, they are just there to show you. This code should be written for arbitrary data, not specifically for this sequence of data. The only place you need to write a code is marked YOUR CODE GOES HERE. You have been supplied a file with data in it (data2.txt). The line...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • Note: The question needs to be answered in "C Programming Languange ". And after the question fin...

    Note: The question needs to be answered in "C Programming Languange ". And after the question find 3 pages for needed informations. Spring CE4717 Language Processors Q1. Consider the following LEx program. return R1 return R2 return R3 return R4 return R5; return R6; IA-2a-z)[A-Za-z0-9]- -2 10-91+ 10-9a-EA-FI Ihi] [01] [01] 이삐 t Vtin) int main (void) int tcode; do f tcode -yylex()i printf ("token type td \"%s\"\n", tcode, yytext); ) while (tcode)i return 0; i. Explain the steps needed...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT