-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionEvaluator.h
51 lines (42 loc) · 1.03 KB
/
ExpressionEvaluator.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
//
// Created by FLM on 2015/12/5 0005.
//
#ifndef EXPRESSIONEVALUATOR_H
#define EXPRESSIONEVALUATOR_H
#include "function.h"
enum FunctionName {
Sin, Cos, Tan, Exp, Log,
};
struct Symbol {
enum Type {
Add, Sub,
Multi, Divide,
Power,
Positive, Negative,
LeftParenthese,
RightParenthese,
FunName,
Number,
theVariableX,
ApplyFunction,
} type;
double number;
FunctionName funName;
Symbol(Type type) : type(type) { assert(type != FunName && type != Number); }
Symbol(double x) : number(x), type(Number) { }
Symbol(FunctionName name) : funName(name), type(FunName) { }
OperatorPrecedence::Order precedence();
};
class ExpressionEvaluator {
private:
std::string expression;
int pos;
std::queue<Symbol> symbolQueue;
std::queue<Symbol> symbolPolish;
void stringDecomposition();
void reversePolishNotation();
Expression *constructTree();
public:
Expression *evaluate(const std::string &s);
};
#endif //EXPRESSIONEVALUATOR_H