-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator.h
68 lines (59 loc) · 1.66 KB
/
evaluator.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
#include "structs.h"
using namespace std;
class Operator{
int precedence;
//if operator is left assosiative
bool left;
string o;
static const string operator_list;
//count precedence
void countPrec();
public:
Operator(string value);
//get value
string value();
//get precedence
int getPrec();
// if operator is left assosiavite
bool isLeft();
};
/*
realizatiot of Shunting Yard
turns a math infix expression to postfix
evaluates postfix expression
utilizes queue ans stack
*/
class Evaluator{
Queue *qu;
Stack *st;
string input;
protected:
/// @brief deletes spaces and replaces unary minus with 'u'
void _prepareInput();
/// @brief Puts token either to queue (if number) or to stack (if operator)
/// when turning to postfix, takes operator precedence into account
/// @param string value - token to put
void _decideAndPut(const string&);
/// @brief determines if a token is a number
/// @param string value
/// @return true if value is a number
bool _ifNumber(const string&);
/// @brief based on operator, pop two operands from stack, count and return the result
/// @param token
/// @return result
string _countRes(string& token);
public:
Evaluator(string input);
~Evaluator();
Evaluator(const Evaluator& another);
/// @brief turns math expression from infix to postfix expression
void toPostfix();
/// @brief evaluates posfix expression
/// @return result
int PostfixEval();
};