Dynamic MathStack
The MathStack class shown in this chapter only has 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.
multAll -Pops all values off the stack, multiplies them, and pushes their
product onto the stack.
Demonstrate the class with a driver program.
Using these header files
IntStack.h
// Specification file for the IntStack class
#ifndef INTSTACK_H
#define INTSTACK_H
class IntStack
{
protected:
int *stackArray;
int stackSize;
int top;
public:
IntStack(int);
~IntStack();
void push(int);
void pop(int &);
bool isFull();
bool isEmpty();
};
#endif
and
MathStack.h
// Specification file for the MathStack class
#ifndef MATHSTACK_H
#define MATHSTACK_H
#include "IntStack.h"
class MathStack : public IntStack
{
public:
MathStack(int s) : IntStack(s) {}
void add();
void sub();
void mult();
void div();
void addAll();
void multAll();
};
#endif
In C++
PLEASE KEEP IN MIND that I use visual studio and cannot use #insert <bits/stdc.h>. I would also like some directions on where to put the code you are gonna make (For example if it's a .h file please instruct me to put it in the header files or source files if necessary.) Thank you !
Code
I am also using visual studio so i give you the instructions
First make a IntStack.h in Header file and copy paste below code in it
#ifndef INTSTACK_H
#define INTSTACK_H
class IntStack
{
protected:
int *stackArray;
int stackSize;
int top;
public:
IntStack(int);
~IntStack();
void push(int);
void pop(int &);
bool isFull();
bool isEmpty();
};
#endif
Snap
Now make one more file in Header file named MathStack.h and copy and paste below code
#ifndef MATHSTACK_H
#define MATHSTACK_H
#include "IntStack.h"
class MathStack : public IntStack
{
public:
MathStack(int s) : IntStack(s) {}
void add();
void sub();
void mult();
void div();
void addAll();
void multAll();
};
#endif
Snap

Now create A IntStack.cpp file and put in the Source Files than copy and paste below code in it
#include"IntStack.h"
#include<iostream>
using namespace std;
IntStack::IntStack(int size)
{
stackArray = new int[size];
stackSize = size;
top=-1;
}
void IntStack::push(int num)
{
if (isFull())
{
cout << "The stack is
full.\n";
}
else
{
top++;
stackArray[top] = num;
}
}
void IntStack::pop(int &num)
{
if(isEmpty())
cout << "The stack is
empty.\n";
else
{
num = stackArray[top];
top--;
}
}
bool IntStack::isFull()
{
if (top == stackSize - 1)
return true;
else
return false;
}
bool IntStack::isEmpty()
{
if (top == -1)
return true;
else
return false;
}
IntStack::~IntStack()
{
delete stackArray;
}

Now create another MathStack.cpp file in Source Files and copy and paste below code in it
#include "MathStack.h"
void MathStack::add()
{
int num, sum;
// Pop the first two values off the stack.
pop(sum);
pop(num);
// Add the two values, store in sum.
sum += num;
// Push sum back onto the stack.
push(sum);
}
void MathStack::sub()
{
int num, diff;
// Pop the first two values off the stack.
pop(diff);
pop(num);
// Subtract num from diff.
diff-=num;
// Push diff back onto the stack.
push(diff);
}
void MathStack::mult()
{
int num, num2;
pop(num);
pop(num2);
num *= num2;
push(num);
}
void MathStack::multAll()
{
int total = 1;
int num;
while (!isEmpty())
{
pop(num);
total *= num;
}
push(total);
}
void MathStack::addAll()
{
int total = 0;
int num;
while (!isEmpty())
{
pop(num);
total += num;
}
push(total);
}
void MathStack::div()
{
int q;
int first, second;
pop(first);
pop(second);
q = second / first;
push(q);
}
Snap

Make Aother Main.cpp file that is with a driver program. and copy paste below code in it
#include <iostream>
#include "MathStack.h"
using namespace std;
int main()
{
int catchVar; // To hold values popped off the
stack
// Create a MathStack object.
MathStack stack(5);
// Push 3 and 6 onto the stack.
cout << "Pushing 3\n";
stack.push(3);
cout << "Pushing 6\n";
stack.push(6);
// Add the two values.
stack.add();
// Pop the sum off the stack and display it.
cout << "The sum is ";
stack.pop(catchVar);
cout << catchVar << endl <<
endl;
// Push 7 and 10 onto the stack
cout << "Pushing 7\n";
stack.push(7);
cout << "Pushing 10\n";
stack.push(10);
// Subtract 7 from 10.
stack.sub();
// Pop the difference off the stack and display
it.
cout << "The difference is ";
stack.pop(catchVar);
cout << catchVar << endl <<
endl;
// Push 10 and 20 onto the stack
cout << "Pushing 10\n";
stack.push(10);
cout << "Pushing 20\n";
stack.push(20);
//Multiply 10 with 20.
stack.mult();
// Pop the multiplication off the stack and display
it.
cout << "The multiplication is ";
stack.pop(catchVar);
cout << catchVar << endl <<
endl;
// Push 30 and 15 onto the stack
cout << "Pushing 30\n";
stack.push(30);
cout << "Pushing 15\n";
stack.push(15);
//Devide 30 by 15.
stack.div();
// Pop the multiplication off the stack and display
it.
cout << "The division is ";
stack.pop(catchVar);
cout << catchVar << endl <<
endl;
// Push 10, 20, 40 and 40 onto the stack
cout << "Pushing 10\n";
stack.push(10);
cout << "Pushing 20\n";
stack.push(20);
cout << "Pushing 30\n";
stack.push(30);
cout << "Pushing 40\n";
stack.push(40);
//adding all the vlaue
stack.addAll();
cout << "The Addition of all values: ";
stack.pop(catchVar);
cout << catchVar << endl <<
endl;
// Push 1, 2, 5, 7, and 10 onto the stack
cout << "Pushing 1\n";
stack.push(1);
cout << "Pushing 2\n";
stack.push(2);
cout << "Pushing 5\n";
stack.push(5);
cout << "Pushing 7\n";
stack.push(7);
cout << "Pushing 10\n";
stack.push(10);
//mlutliplying all the vlaue
stack.multAll();
cout << "The Multiplication of all values:
";
stack.pop(catchVar);
cout << catchVar << endl <<
endl;
system("pause");
return 0;
}
snap

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.
Dynamic MathStack The MathStack class shown in this chapter only has two member functions: add and...
1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...
Reverse Polish (HP) Style Calculator - Part 3 The purpose of this assignment is to build the business calculator using supporting files built in Topics 4 and 5. Create a Java application file named RPN.java containing a main method by using the ForthStack.java and associated files from Topic 5. The application should have one text box for numeric data entry, one text box for numeric display, one text box for error display, and buttons labeled "+", "-", "*", "/", "dup",...
// Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...
1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available // copy original's array member into this new array { // Please complete the function here } else { cerr << "*Inadequate memory to allocate...
Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...
(C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...
C++ programming help.
The only change you have to make is in current program,
where it says "your code goes here", do not make any changes to any
other files, they are just there to show you. This
code should be written for arbitrary data, not specifically for
this sequence of data. The only place you need to
write a code is marked YOUR CODE GOES HERE.
You have been supplied a file with data in it (data2.txt). The
line...
Please Answer this question using the language of C++.
I provide you with the picture of figure 18_02. Thank you.
I 7/ Fig. 18.2: Stack.h 2 // Stack class template. #ifndef #de fine 3 STACK-H STACK-H 5 #include 7 template 8 class Stack ( 9 public: 10 I const T& top) 12 13 l/ return the top element of the Stack return stack.frontO; // push an element onto the Stack void push(const T& pushValue) 15 stack push front (pushValue); 17...
I need help fixing my code. My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...