-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
52 lines (43 loc) · 997 Bytes
/
Stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
** EPITECH PROJECT, 2024
** ppool13
** File description:
** Stack
*/
#include "Stack.hpp"
void Stack::push(double value)
{
_stack.push(value);
}
double Stack::pop() {
if (_stack.empty())
throw Error(Error::ErrorType::EMPTY_STACK);
double value = _stack.top();
_stack.pop();
return value;
}
double Stack::top() const {
if (_stack.empty())
throw Error(Error::ErrorType::EMPTY_STACK);
return _stack.top();
}
void Stack::add() {
if (_stack.size() < 2)
throw Error(Error::ErrorType::NOT_ENOUGH_OPERANDS);
push(pop() + pop());
}
void Stack::sub() {
if (_stack.size() < 2)
throw Error(Error::ErrorType::NOT_ENOUGH_OPERANDS);
push(pop() - pop());
}
void Stack::mul() {
if (_stack.size() < 2)
throw Error(Error::ErrorType::NOT_ENOUGH_OPERANDS);
push(pop() * pop());
}
void Stack::div() {
if (_stack.size() < 2)
throw Error(Error::ErrorType::NOT_ENOUGH_OPERANDS);
push(pop() / pop());
}