STARTING OUT WITH C++ REVEL >IA<
STARTING OUT WITH C++ REVEL >IA<
9th Edition
ISBN: 9780135853115
Author: GADDIS
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 19, Problem 7PC
Program Plan Intro

Dynamic MathStack

Program Plan:

MathStack.h:

  • Include required header files
  • Declare a class named “MathStack”. Inside the class,
    • Inside “public” access specifier,
      • Declare functions “add ()”, “sub ()”, “mult ()”, “div ()”, “addAll ()”, and “multAll ()”.

MathStack.cpp:

  • Include required header files.
  • Give function definition to add elements “add()”.
    • Declare required variables “number”, and “sum_Value”.
    • Call the function “pop()”.
    • Add the elements.
    • Push the value into the stack using the function “push()”.
  • Give function definition to subtract elements “sub()”.
    • Declare required variables “number”, and “diff_Value”.
    • Call the function “pop()”.
    • Subtract the elements.
    • Push the value into the stack using the function “push()”.
  • Give function definition to multiply elements “mult()”.
    • Declare required variables “number”, and “prod_Value”.
    • Call the function “pop()”.
    • Multiply the elements.
    • Push the value into the stack using the function “push ()”.
  • Give function definition to divide elements “div()”.
    • Declare required variables “number”, and “quo_Value”.
    • Call the function “pop()”.
    • Divide the elements.
    • Push the value into the stack using the function “push()”.
  • Give function definition to add all the elements “addAll()”.
    • Declare required variables “number”, and “sum_Value”.
    • Call the function “pop()”.
    • Add all the elements.
    • Push the value into the stack using the function “push()”.
  • Give function definition to multiply all the elements “multAll()”.
    • Declare required variables “number”, and “prod_Value”.
    • Call the function “pop()”.
    • Multiply all the elements.
    • Push the value into the stack using the function “push()”.

IntStack.h:

  • Include required files.
  • Declare a class named “IntStack”. Inside the class,
    • Inside “protected” access specifier,
      • Declare a pointer named “stackArray”.
      • Declare variables “stackSize” and “top”.
    • Inside “public” access specifier,
      • Declare constructor and destructor.
      • Give function declarations.

IntStack.cpp:

  • Declare required header files.
  • Give definition for constructor,
    • Create a stack array and assign the size
  • Give definition for destructor,
    • Delete the array and assign it to null
  • Give function definition to push elements “push()”
    •  Check if the stack is full using the function “isFull()”,
      • If the condition is true then, print “The stack is full”.
      • If the condition is not true then,
        • Increment the variable.
        • Assign “num” to the top position.
  • Give function definition to pop elements “pop ()”,
    • Check if the stack is empty using the function “isEmpty()”.
      • If the condition is true then, print “The stack is empty”.
        • Assign top element to the variable.
        • Decrement the variable.
  • Give function definition to check if the stack is full “isFull()”.
    • Assign “false” to the Boolean variable “status”.
    • Check if the stack size if full.
      • Assign true
    • Return the Boolean variable “status”.
  • Give function definition to check if the stack is empty “isEmpty()”.
    • Assign “false” to the Boolean variable
    • Check if top is empty
      • Assign true.
    • Return the variable

Main.cpp:

  • Include required header files.
  • Inside “main ()” function,
    • Declare constant variables “STACKSIZE”, “ADDSIZE”, and “MULTSIZE”.
    • Create three stacks “stack”, “addAllStack”, and “multAllStack”.
    • Declare a variable “popVar”.
    • Push two elements to perform add operation.
    • Call the function “add()”.
    • Display the element.
    • Push two elements to perform multiplication operation.
    • Call the function “mult()”.
    • Display the element.
    • Push two elements to perform division operation.
    • Call the function “div()”.
    • Display the element.
    • Push two elements to perform subtraction operation.
    • Call the function “sub()”.
    • Display the element.
    • Push four elements to perform addAll operation.
    • Call the function “addAll()”.
    • Display the element.
    • Push six elements to perform multAll operation.
    • Call the function “multAll()”.
    • Display the element.

Blurred answer
Students have asked these similar questions
7. Dynamic MathStack The MathStack class shown in this chapter has only 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. multA11 Pops all values off the stack, multiplies them, and pushes their prod- uct onto the stack. Demonstrate the class with a driver program. here is the extention file please use it. // Specification file for the IntStack class #ifndef INTSTACK_H #define INTSTACK_H   class IntStack { private:    int *stackArray;  // Pointer to the stack array    int stackSize;    // The stack size    int top;          // Indicates the top of the stack   public:    // Constructor    IntStack(int);      // Copy constructor…
C# Reverse the stack - This procedure will reverse the order of items in the stack.  This one may NOT break the rules of the stack. HINTS: Make use of more stacks.  Arrays passed as parameters are NOT copies.  Remember, this is a procedure, not a function.  This would occur in the NumberStack class, not the main class. These are the provided variables: private int [] stack;private int size; Create a method that will reverse the stack when put into the main class.
Use Python for this question. Also please comment what each line of code means for this question as well: Inheritance (based on 8.38) You MUST use inheritance for this problem. A stack is a sequence container type that, like a queue, supports very restrictive access methods: all insertions and removals are from one end of the stack, typically referred to as the top of the stack.  A stack is often referred to as a last-in first-out (LIFO) container because the last item inserted is the first removed.  Implement a Stack class using Note that this means you may be able to inherit some of the methods below.  Which ones?  (Try not writing those and see if it works!) Constructor/_init__ - Can construct either an empty stack, or initialized with a list of items, the first item is at the bottom, the last is at the top. push() – take an item as input and push it on the top of the stack pop() – remove and return the item at the top of the stack isEmpty() – returns True if the stack is empty,…
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage