User: hellasenpai

  • Public profile

User profile: hellasenpai

User info
User name:hellasenpai
Bio:#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

class Stack {
public:
Stack(); //Constructor
Stack(const Stack& source); //Copy constructor
~Stack(); //Destructor

void push(int number);
int pop();

int peek() const;
bool isEmpty() const;
void showFullStack() const;
void resetStack();

Stack& operator=(const Stack& source); //Overload Assignament Operator

void memory(int currentCapacity); //Allocates memory
private:
int usedCapacity;
int capacity;
int top;
int * arr;
};

void menu();
void menuOperators();

int main() {
Stack stack;
Stack* pStack = &stack;
char oper;
int num, lhs, rhs, result;
bool trigger = true;
while (trigger == true)
{
menu();
cin >> oper;
if (oper == '#')
{
cout << "#: ";
cin >> num;
pStack->push(num);
}
else if (oper == '!')
{
menuOperators();
cin >> oper;
if (oper == '*')
{
lhs = pStack->pop();
rhs = pStack->pop();
result = lhs * rhs;
pStack->push(result);
}
else if (oper == '-')
{
lhs = pStack->pop();
rhs = pStack->pop();
result = lhs - rhs;
pStack->push(result);
}
else if (oper == '+')
{
lhs = pStack->pop();
rhs = pStack->pop();
result = lhs + rhs;
pStack->push(result);
}
else if (oper == '/')
{
lhs = pStack->pop();
rhs = pStack->pop();
result = lhs / rhs;
pStack->push(result);
}
}
else if (oper == '=')
{
cout << pStack->peek() << endl;
}
else if (oper == '.')
{
pStack->showFullStack();
}
else
trigger = false;
}

Stack stack2 = stack;
stack2.showFullStack();

system("pause");
return 0;
}

Stack::Stack()
{
usedCapacity = 0;
capacity = 1;
top = -1;
arr = new int[capacity];
}

Stack::Stack(const Stack & source)
{
cout << "Invoking Copy Constructor. " << endl;
usedCapacity = source.usedCapacity;
this->capacity = source.capacity;
this->top = source.top;
arr = new int[source.capacity];
copy(source.arr, source.arr + usedCapacity, this->arr);
}

Stack::~Stack()
{
delete[] arr;
}

void Stack::push(int number)
{
top++;
usedCapacity++;
arr[top] = number;
memory(usedCapacity);
}

int Stack::pop()
{
usedCapacity--;
return arr[top--];
memory(usedCapacity);
}

int Stack::peek() const
{
return arr[top];
}

bool Stack::isEmpty() const
{
if (top == -1)
return true;
else
return false;
}

void Stack::showFullStack() const
{
cout << "Peek: " << peek() << endl;
for (int i = 0; i < usedCapacity; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

void Stack::resetStack()
{
for (int i = 0; i < capacity; i++)
{
arr[i] = 0;
}
top = -1;
usedCapacity = 0;
}

Stack & Stack::operator=(const Stack & source)
{
cout << "overloaded Assignament Called. " << endl;

if (this == &source)
return *this;
this->capacity = source.capacity;
this->usedCapacity = source.usedCapacity;
this->top = source.top;
copy(source.arr, source.arr + usedCapacity, this->arr);
return *this;
}

void Stack::memory(int currentCapacity)
{
if (currentCapacity >= capacity)
{
int newCapacity = (currentCapacity * 3) / 2 + 1;
int * arr2 = new int[newCapacity];
memcpy(arr2, arr, sizeof(int)* usedCapacity);
delete[] arr;
arr = arr2;
capacity = newCapacity;
}
}
History
Joined:
Number of posts:1
Latest posts:

Why am I getting huge integers as the output for my code?
So I'm supposed to do the following: bigger, a, and b are parallel arrays. function's job is to set...

This user does not accept Private Messages