EBK STARTING OUT WITH C++
EBK STARTING OUT WITH C++
9th Edition
ISBN: 9780134996066
Author: GADDIS
Publisher: PEARSON CUSTOM PUB.(CONSIGNMENT)
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…
Term by CodeChum Admin (JAVA CODE) Construct a class called Term. It is going to represent a term in polynomial expression. It has an integer coefficient and an exponent. In this case, there is only 1 independent variable that is 'x'.   There should be two operations for the Term: public Term times(Term t) - multiplies the term with another term and returns the result public String toString() - prints the coefficient followed by "x^" and appended by the exponent. But with the following additional rules: if the coefficient is 1, then it is not printed. if the exponent is 1, then it is not printed ( the caret is not printed as well) if the exponent is 0, then only the coefficient is printed.  Input The first line contains the coefficient and the exponent of the first term. The second line contains the coefficient and the exponent of the second term. 1·1 4·3 Output Display the resulting product for each of the test case. 4x^4
Computer Science C# Programming   Object Orientation   Please use Arrays or ArrayLists where appropriate   Implement a menu driven program that has some operations for a food truck. A food truck has multiple food items as well as a menu which contains a selection of the food items which will be sold for that day. A maximum of 10 food items can appear on the menu for any day. For each food item keep a code, description, category, price and quantity in stock (code, description and category are not usually changed).   Create a comma delimited text file called "items.txt" for 15 or more food items, indicating the item's description, category, price and quantity in stock.   Read all the food items from the text file “Items.txt”   Display all the food items available for this food truck.   Continuously prompt the user for a food item’s code to be placed on the menu until the menu is full or the user enters -1. Note that only food items which have a positive quantity in stock may be added to…
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