/
parser.h
44 lines (37 loc) · 1.01 KB
/
parser.h
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
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
class Token {
public:
enum class Type { // one token is a character in the given string
Unknown,
Number,
Operator,
LeftParen,
RightParen,
};
// fields
const Type type;
const std::string str;
const int precedence;
const bool rightAssociative;
// constructor
Token(Type t, const std::string& s, int prec = -1, bool ra = false)
: type{t}, str(s), precedence(prec), rightAssociative{ra}
{ }
};
// doublen ended queue to store numbers and operators
std::deque<Token> expressToTokens(const std::string& s);
// implement calculation from deque
std::deque<Token> shutingYard(const std::deque<Token>& tokens);
// do the calculations
float doCalculation(const std::string& str);
// clear empty spaces and tabs from string
void removeSpaces(std::string& str);