-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.cpp
42 lines (36 loc) · 903 Bytes
/
Calculator.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
#include "Calculator.h"
Calculator::Calculator()
: nodes_ (Stack<Expr_Command *>()),
ops_ (Stack<Binary_Expr_Command *>())
{ }
Calculator::~Calculator () {
try {
Expr_Command * n = get_expression();
delete n;
} catch (std::exception & e) {
}
}
void Calculator::addOp (Binary_Expr_Command * op) {
if (!ops_.is_empty() && ops_.top()->precedence() >= op->precedence()) {
get_expression();
}
ops_.push(op);
}
void Calculator::addCommand (Expr_Command * node) {
nodes_.push(node);
}
void Calculator::print() {
std::cout<<"Calculator: "<<std::endl;
nodes_.top()->printName();
}
Expr_Command * Calculator::get_expression () {
while (!ops_.is_empty()) {
ops_.top()->setRight(nodes_.top());
nodes_.pop();
ops_.top()->setLeft(nodes_.top());
nodes_.pop();
nodes_.push(ops_.top());
ops_.pop();
}
return nodes_.top();
}