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 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
For C++ please just need cpp file
Data structure & Algorithum java program 1) Add a constructor to the class "AList" that creates a list from a given array of objects.2) Add a method "getPosition" to the "AList" class that returns the position of a given object in the list. The header of the method is as follows : public int getPosition(T anObject)3) Write a program that thoroughly tests all the methods in the class "AList".4) Include comments to all source code.
C programmingTopic: Stacks

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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage