Starting Out with C++ from Control Structures through Objects Brief, Student Value Edition (8th Edition)
bartleby

Concept explainers

Question
Book Icon
Chapter 18, Problem 8PC
Program Plan Intro

Dynamic MathStack Template

Program Plan:

MathStack.h:

  • Include required header files
  • Declare a class named “MathStack” which inherits “DynStack” Inside the class,
    • Inside “public” access specifier,
      • Declare functions “add ()”, “sub ()”, “mult ()”, “div ()”, “addAll ()”, and “multAll ()”.
  • Declare class template.
  • Give function definition to add elements “add ()”.
    • Declare required template variables “num_Value”, and “sum_Value”.
    • Call the function “pop ()”
    • Add the elements.
    • Push the value into the stack using the function “push ()”.
  • Declare class template.
  • Give function definition to subtract elements “sub ()”.
    • Declare required template variables “num_Value”, and “diff_Value”.
    • Call the function “pop ()”
    • Subtract the elements.
    • Push the value into the stack using the function “push ()”.
  • Declare class template.
  • Give function definition to multiply elements “mult ()”.
    • Declare required template variables “num_Value”, and “prod_Value”.
    • Call the function “pop ()”
    • Multiply the elements.
    • Push the value into the stack using the function “push ()”.
  • Declare class template.
  • Give function definition to divide elements “div ()”.
    • Declare required template variables “num_Value”, and “quo_Value”.
    • Call the function “pop ()”
    • Divide the elements.
    • Push the value into the stack using the function “push ()”.
  • Declare class template.
  • Give function definition to add all the elements “addAll ()”.
    • Declare required template variables “num_Value”, and “sum_Value”.
    • Call the function “pop ()”
    • Add all the elements.
    • Push the value into the stack using the function “push ()”.
  • Declare class template.
  • Give function definition to multiply all the elements “multAll ()”.
    • Declare required template variables “num_Value”, and “prod_Value”.
    • Call the function “pop ()”
    • Multiply all the elements.
    • Push the value into the stack using the function “push ()”.

DynStack.h:

  • Include required header files.
  • Create a template.
  • Declare a class named “DynStack”. Inside the class
    • Inside the “protected” access specifier,
      • Give structure declaration for the stack
        • Create an object for the template
        • Create a stack pointer name “next”.
      • Create a stack pointer name “top”
      • Declare a variable named “stackSize”.
    • Inside the “public” access specifier,
      • Give a declaration for a constructor.
        • Assign null to the top node.
      • Give function declaration for “push ()”, “pop ()”,and “isEmpty ()”.
  • Give the class template.
  • Give function definition for “push ()”.
    • Assign null to the new node.
    • Dynamically allocate memory for new node
    • Assign “num” to the value of new node.
    • Check if the stack is empty using the function “isEmpty ()”
      • If the condition is true then assign new node as the top and make the next node as null.
      • If the condition is not true then, assign top node to the next of new node and assign new node as the top.
  • Give the class template.
  • Give function definition for “pop ()”.
    • Assign null to the temp node.
    • Check if the stack is empty using the function “is_Empty ()”
      • If the condition is true then print “The stack is empty”.
      • If the condition is not true then,
        • Assign top value to the variable “num”.
        • Link top of next node to temp node.
        • Delete the top node and make temp as the top node.
  • Give the class template.
  • Give function definition for “isEmpty ()”.
    • Assign false to a Boolean variable
    • Check if the top node is null
      • Assign true to “status”.
    • Return the status

Main.cpp:

  • Include required header files.
  • Inside “main ()” function,
    • Declare constant variables “STACKSIZE”, “ADDSIZE”, and “MULTSIZE”.
    • Create three stacks “stack”, “addAllStack”, and “multAllStack”.
    • Declare two variables “popVar” and “ipopVar”.
    • 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
C++ ProgrammingActivity: Linked List Stack and BracketsExplain the flow of the code not necessarily every line, as long as you explain what the important parts of the code do. The code is already correct, just explain the flow #include "stack.h" #include "linkedlist.h" // SLLStack means Singly Linked List (SLL) Stack class SLLStack : public Stack {     LinkedList* list;      public:         SLLStack() {             list = new LinkedList();         }         void push(char e) {             list->add(e);             return;         }         char pop() {             char elem;             elem = list->removeTail();             return elem;         }         char top() {             char elem;             elem = list->get(size());             return elem;         }         int size() {             return list->size();         }         bool isEmpty() {             return list->isEmpty();         } };
class Node:  def __init__(self, e, n):    self.element = e    self.next = n class LinkedList:    def __init__(self, a):  #  Design the constructor based on data type of a. If 'a' is built in python list then  #  Creates a linked list using the values from the given array. head will refer  #  to the Node that contains the element from a[0]  #  Else Sets the value of head. head will refer  # to the given LinkedList   # Hint: Use the type() function to determine the data type of a    self.head = None    # To Do        # Count the number of nodes in the list  def countNode(self):    # To Do        # Print elements in the list  def printList(self):    # To Do       # returns the reference of the Node at the given index. For invalid index return None.  def nodeAt(self, idx):    # To Do
C++ ProgrammingActivity: Deque Linked List Explain the flow of the code not necessarily every line, as long as you explain what the important parts of the code do. The code is already correct, just explain the flow. #include "deque.h" #include "linkedlist.h" #include <iostream> using namespace std; class DLLDeque : public Deque {    DoublyLinkedList* list;       public:     DLLDeque() {         list = new DoublyLinkedList();      }         void addFirst(int e) {             list->addAt(e,1);          }              void addLast(int e) {             list->addAt(e,size()+1);          }         int removeFirst() {             return list->removeAt(1);          }         int removeLast() {             return list->removeAt(size());          }         int size(){             return list->size();         }              bool isEmpty() {             return list->isEmpty();         }             // OPTIONAL: a helper method to help you debug         void print() {…

Chapter 18 Solutions

Starting Out with C++ from Control Structures through Objects Brief, Student Value Edition (8th Edition)

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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education