-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.cpp
48 lines (46 loc) · 1.19 KB
/
expression.cpp
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
#include "expression.h"
void ComExp::sendTree(int curBias)
{
string opStr(1, op);
if (opStr == "%") opStr = "MOD";
if (opStr == "^") opStr = "**";
emit printTreeLineSignal(curBias, opStr);
if (lson) lson->sendTree(curBias + 4);
if (rson) rson->sendTree(curBias + 4);
}
int ComExp::calc(VarContext* context)
{
int rightAns = rson->calc(context);
if (op == '=')
{
context->setValue(lson->getIdentifier(), rightAns);
return rightAns;
}
int leftAns = lson->calc(context);
if (op == '+') return leftAns + rightAns;
if (op == '-') return leftAns - rightAns;
if (op == '*') return leftAns * rightAns;
if (op == '/')
{
if (rightAns == 0) throw int(2);
return leftAns / rightAns;
}
if (op == '%')
{
int tmp = leftAns % rightAns;
int bias = abs(rightAns);
if (tmp > 0 && rightAns < 0) tmp -= bias;
else if (tmp < 0 && rightAns > 0) tmp += bias;
return tmp;
}
if (op == '^')
{
if (rightAns < 0) return 0;
int ans = 1;
for (int i = 1; i <= rightAns; i++)
ans *= leftAns;
return ans;
}
throw int (3);
return 0;
}