Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

he MathStack Class (attached here) has only two member functions: add and sub. Write the following additional member functions: mult - Pops the two top

he MathStack Class (attached here) has only two member functions: add and sub. Write the following additional member functions:

  • mult - Pops the two top 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
  • multAll - Pops all values off the stack, multiples them and pushes their product onto the stack.

Here is the MathStack class

// Implementation file for the MathStack class #include "MathStack.h" //*********************************************** // Member function add. add pops * // the first two values off the stack and * // adds them. The sum is pushed onto the stack. * //*********************************************** void MathStack::add() { int num, sum; pop(sum); pop(num); sum += num; push(sum); } //*********************************************** // Member functon sub. sub pops the * // first two values off the stack. The * // second value is subtracted from the * // first value. The difference is pushed * // onto the stack. * //*********************************************** void MathStack::sub() { int num, diff; pop(diff); pop(num); diff -= num; push(diff); }

Here is the Driver Class:

#include #include "Mathstack.h" using namespace std; int main() { // Constants for stack sizes const int STACK_SIZE = 5; const int ADD_SIZE = 4; const int MULT_SIZE = 6; // Create three stacks MathStack stack(STACK_SIZE); MathStack addAllStack(ADD_SIZE); MathStack multAllStack(MULT_SIZE); // Variable to catch values as they are // popped from the stack int catchVar; // Test Add() cout << "Pushing 3 "; stack.push(3); cout << "Pushing 6 "; stack.push(6); stack.add(); cout << "The sum is "; stack.pop(catchVar); cout << catchVar << endl << endl; // Test Sub() cout << "Pushing 7 "; stack.push(7); cout << "Pushing 10 "; stack.push(10); stack.sub(); cout << "The difference is "; stack.pop(catchVar); cout << catchVar << endl << endl; return 0; }

DIRECTIONS:

1) Modify the MathStack class to add the additional functions (mult, div, addAll, multAll) and submit the file named MathStack.cpp

2) Modify the Driver class to add the additional functions (mult, div, addAll, multAll) and submit the file named Driver.cpp

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

Students also viewed these Databases questions