-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexp_solver.h
104 lines (76 loc) · 2.36 KB
/
exp_solver.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
exp_solver.h
Author: Jingyun Yang
Date Created: 1/16/15
Description: Header file for ExpSolver class
that takes in algebraic expressions as strings
and solves them.
*/
#include <string>
#include <vector>
#include <stack>
#include "value.h"
using namespace std;
#ifndef EXP_SOLVER_H
#define EXP_SOLVER_H
struct Variable {
string name;
Value value;
Variable(string nm, Value val)
: name(nm), value(val) {}
};
struct Function {
string name;
double (*func)(double);
Function(string nm, double (*f)(double))
: name(nm), func(f) {}
};
enum BlockType {
Num, Sym, Func, Constant, Var, BracL, BracR, Nil
};
struct Block {
int start, end, level;
BlockType type;
Block(): start(0), end(0), level(0), type(Nil) {}
Block(int s, int e, int l, BlockType tp)
: start(s), end(e), level(l), type(tp) {}
};
class ExpSolver {
public:
// Constructor to initialize the ExpSolver object
ExpSolver(void);
// Inputs a string of expression and outputs the result
string solveExp(string);
private:
// The partition of expression that the object is
// currently working on
vector<Block> blocks;
// Vector of variables, constants and functions
// that might be used in calculations
vector<Variable> variables;
vector<Variable> constants;
vector<Function> functions;
// Add predefined constants and functions
void addPredefined(void);
// Discard spaces in the input string
string discardSpaces(string str);
// Check whether the input includes variable declaration
// If so, extract the lhs and rhs of the equal sign
bool checkDeclaration(string &exp, string &newVarName, bool &isDec);
// This is similar to lexical analysis in a compiler
// Partition an expression into blocks of different types
bool groupExp(string exp);
// Analyze whether a string Block is of BlockType Func, Constant or Var
BlockType analyzeStrType(string str);
// Determine the type of one single character
BlockType charType(char c);
// Replace every "-" as negative sign by "0-"
void dealWithNegativeSign(string &exp);
// Calculate expression in block range [startBlock,endBlock)
Value calculateExp(string exp, int startBlock, int endBlock);
// Given the block id of ')', find the block id of corresponding '('
int findIndexOfBracketEnding(int blockId);
// This is for debug: print out the contents in the stacks
void printStacks(stack<Value> values,stack<char> ops);
};
#endif