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
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.
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
The card that wins the trick def winning_card(cards, trump=None): Playing cards are again represented as tuples of (rank,suit) as in the cardproblems.py lecture example program. In trick taking games such as whist or bridge, four players each play one card from their hand to the trick, committing to their play in clockwise order starting from the player who plays 0irst into the trick. The winner of the trick is determined by the following rules: If one or more cards of the trump suit have been played to the trick, the trick is won by the highest ranking trump card, regardless of the other cards played. If no trump cards have been played to the trick, the trick is won by the highest card of the suit of the 0irst card played to the trick. Cards of any other suits, regardless of their rank, are powerless to win that trick. Ace is the highest card in each suit. Note that the order in which the cards are played to the trick greatly affects the outcome of that trick, since the 0irst…

Chapter 19 Solutions

Starting Out with C++ from Control Structures to Objects Plus MyLab Programming with Pearson eText -- Access Card Package (9th 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