#include <iostream>
#include "NumStack.hpp"
#include "stack.hpp"
int main()
{
int catchVar; // To hold values popped off the stack
// Create a MathStack object.
Stack <int> stack(5);
// Push 3 and 6 onto the stack.
std::cout << "Pushing 3\n";
stack.push(3);
std::cout << "Pushing 6\n";
stack.push(6);
//Add the two values.
stack.add();
// Pop the sum off the stack and display it.
std::cout << "The sum is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl << std::endl;
// Push 7 and 10 onto the stack
std::cout << "Pushing 7\n";
stack.push(7);
std::cout << "Pushing 10\n";
stack.push(10);
// Subtract 7 from 10.
stack.sub();
// Pop the difference off the stack and display it.
std::cout << "The difference is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl << std::endl;;
//Push 2 and 4 onto the stack.
std::cout << "Pushing 2\n";
stack.push(2);
std::cout << "Pushing 4\n";
stack.push(4);
// Multiply the two numbers
//stack.mult();
//Pop the product off the stack and display it.
std::cout << "The product is ";
stack.pop(catchVar);
std::cout << catchVar << std::endl << std::endl;
//Push 10 and 5 onto the stack.
std::cout << "Pushing 10\n";
stack.push(10);
std::cout << "Pushing 5\n";
stack.push(5);
// Divide the two numbers
//stack.div();
//Pop the product off the stack and display it.
std::cout << "The quotient is ";
stack.pop(catchVar);
std::cout << catchVar << "\n\n";
//Pushing 5 numbers: 1, 2, 3, 4 & 5 to the stack
std::cout << "Pushing 1\n";
stack.push(1);
std::cout << "Pushing 2\n";
stack.push(2);
std::cout << "Pushing 3\n";
stack.push(3);
std::cout << "Pushing 4\n";
stack.push(4);
std::cout << "Pushing 5\n";
stack.push(5);
// Add all the numbers
//stack.addAll();
//Pop the sum off the stack and display it.
std::cout << "The sum is ";
stack.pop(catchVar);
std::cout << catchVar << "\n\n";
//Pushing 5 numbers: 2, 4, 3, 8 & 10 to the stack
std::cout << "Pushing 2\n";
stack.push(2);
std::cout << "Pushing 4\n";
stack.push(4);
std::cout << "Pushing 3\n";
stack.push(3);
std::cout << "Pushing 8\n";
stack.push(8);
std::cout << "Pushing 10\n";
stack.push(10);
// Multiply all the numbers
//stack.multAll();
//Pop the product off the stack and display it.
std::cout << "The product is ";
stack.pop(catchVar);
std::cout << catchVar << "\n\n";
return 0;
}
#include "NumStack.hpp"
#include "Stack.hpp"
template <class T>
void Stack<T>::add()
{
T 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);
}
template <class T>
void Stack<T>::sub()
{
T 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);
}
template <class T>
void Stack<T>::mult()
{
T num, product;
//Pop the first two values off the stack
pop(product);
pop(num);
//Multiply num and product
product *= num;
//Push product back onto the stack
push(product);
}
template <class T>
void Stack<T>::div()
{
T num, quotient;
//Pop the first two values off the stack
pop(num);
pop(quotient);
//Divide num from quotient
quotient /= num;
//Push product back onto the stack
push(quotient);
}
template <class T>
void Stack<T>::addAll()
{
T num(0), sum(0);
//While loop to pop all members off the stack
while (!this.isEmpty())
{
pop(num);
sum += num;
}
push(sum);
}
template <class T>
void Stack<T>::multAll()
{
T num(0), product(1);
//While loop to pop all members off the stack
while(!this.isEmpty())
{
pop(num);
product *= num;
}
push(product);
}