-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.cpp
38 lines (33 loc) · 1013 Bytes
/
token.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
#include "token.h"
#include <cmath>
#include <stdexcept>
using namespace std;
double Operator::apply(double a, double b) {
if (signature == '+') {
return a + b;
} else if (signature == '-') {
return a - b;
} else if (signature == '*') {
return a * b;
} else if (signature == '/') {
return a / b;
} else if (signature == '^') {
return pow(a, b);
} else {
throw invalid_argument("Unknown operator: " + to_string(signature));
}
}
map<char, Operator> *Operator::operatorMap = new map<char, Operator> {
{'+', Operator(1, '+')},
{'-', Operator(1, '-')},
{'*', Operator(2, '*')},
{'/', Operator(2, '/')},
{'^', Operator(3, '^')},
};
Operator *Operator::get(char signature) {
if (!isOperator(signature)) {
throw invalid_argument("Unknown operator: " + to_string(signature));
}
return &operatorMap->at(signature);
}
OpeningBracket *OpeningBracket::instance = new OpeningBracket();