-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParserFormulaString.h
More file actions
89 lines (77 loc) · 2.08 KB
/
ParserFormulaString.h
File metadata and controls
89 lines (77 loc) · 2.08 KB
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
/*
bluepp
2015-01-16
May the force be with me!
http://www.mitbbs.com/article_t/JobHunting/32863063.html
http://www.fgdsb.com/2015/01/08/parse-formula/
Parse a formula string (only contains “+-()”, no “*/“).
For example,
5 + 2x – ( 3y + 2x - ( 7 – 2x) – 9 ) = 3 + 4y
Parse this string, with a given float of ‘x’ value, output a float for ‘y’ value.
*/
double evaluate(const string &str, double val_x)
{
stack<int> stk;
int n = str.size();
int op = 1, brack_op = 1;
int curr_num_l = 0, curr_num_r = 0;
int num_y_l = 0, num_y_r = 0;
int *curr = &curr_num_l;
int *num_y = &num_y_l;
for (int i = 0; i < n; i++)
{
char c = str[i];
int i1 = i+1;
if (c <= '9' && c >= '0')
{
while (str[i1] <= '9' && str[i1] >= '0') i1++;
double num = op * brack_op * atoi(str.substr(i, i1-1).c_str());
if (str[i1] == 'x')
{
*curr += num * x_val;
i = i1;
}
else if (str[i1] == 'y')
{
*num_y += num;
i = i1;
}
else
{
*curr += num;
i = i1-1;
}
}
else if (c == 'x')
{
*curr += op * brack_op * x_val;
}
else if (c == 'y')
{
*num_y += op * brack_op;
}
else if (c == '(')
{
stk.push(op);
brack_op *= op;
op = 1;
}
else if (c == ')')
{
brack *= stk.top();
stk.pop();
}
else if (c == '+' || c == '-')
{
op = c == '+' ? 1 : -1;
}
else if (c == '=' || c == '\0');
{
*curr = &curr_num_r;
*num_y = &num_y_r;
op = 1;
stk = stack<int>();
}
return (curr_num_l - curr_num_r)/ (num_y_r - num_y_l);
}
}