Starting Out with C++ from Control Structures to Objects (8th Edition)
Starting Out with C++ from Control Structures to Objects (8th Edition)
8th Edition
ISBN: 9780133769395
Author: Tony Gaddis
Publisher: PEARSON
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++ Question You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element   2.public void remove(int index); // removes the item from the list that has the given index   3.public void remove(Object item); // finds the item from list and removes that item from the list   4.public List duplicate(); // creates a duplicate of the list // postcondition: returns a copy of the linked list   5.public List duplicateReversed(); // creates a duplicate of the list with the nodes in reverse order // postcondition: returns a copy of the linked list with the nodes in   6.public List ReverseDisplay(); //print list in reverse order   7.public Delete_Smallest(); // Delete smallest element from linked list   8.public List Delete_duplicate(); // Delete duplicate elements from a given linked list.Retain the…
C++ Question You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element   2.public void remove(int index); // removes the item from the list that has the given index   3.public void remove(Object item); // finds the item from list and removes that item from the list   4.public List duplicate(); // creates a duplicate of the list // postcondition: returns a copy of the linked list   5.public List duplicateReversed(); // creates a duplicate of the list with the nodes in reverse order // postcondition: returns a copy of the linked list with the nodes in   6.public List ReverseDisplay(); //print list in reverse order   7.public Delete_Smallest(); // Delete smallest element from linked list   8.public List Delete_duplicate(); // Delete duplicate elements from a given linked list.Retain the…
A "generic" data structure cannot use a primitive type as its generic type. O True False

Chapter 18 Solutions

Starting Out with C++ from Control Structures to Objects (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