-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondition.cpp
71 lines (67 loc) · 2.08 KB
/
condition.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "condition.h"
#include "table.h"
#include "utils.h"
#include <assert.h>
using namespace std;
bool isnum(condition *cur) {
assert(cur->type==3);
if(cur->opt == 1) return true;
if(cur->opt == 3 && **(cur->colval) == 0)
return true;
return false;
}
int getnum(condition *cur) {
if(cur->opt == 1) return cur->num;
else if(cur->opt == 3 && **(cur->colval) == 0)
return atoi(parse(*(cur->colval)).c_str());
assert(0);
}
string getstr(condition *cur) {
if(cur->opt == 2) return cur->str;
else if(cur->opt == 3 && **(cur->colval) > 0)
return parse(*(cur->colval));
assert(0);
}
bool calc(condition *cur) {
// cout<<"DEBUG: type "<<cur->type<<endl;
// cout<<"DEBUG: opt "<<cur->opt<<endl;
if(cur->type)
assert(cur->type > 0);
if(cur->type == 1) {
if(isnum(cur->left) && isnum(cur->right)) {
switch (cur->opt)
{
case 1:
return getnum(cur->left)<getnum(cur->right);
case 2:
return getnum(cur->left)>getnum(cur->right);
case 3:
return getnum(cur->left)<=getnum(cur->right);
case 4:
return getnum(cur->left)>=getnum(cur->right);
case 5:
return getnum(cur->left)==getnum(cur->right);
case 6:
return getnum(cur->left)!=getnum(cur->right);
default:
break;
}
} else if(!isnum(cur->left) && !isnum(cur->right)) {
switch (cur->opt)
{
case 5:
return getstr(cur->left)==getstr(cur->right);
case 6:
return getstr(cur->left)!=getstr(cur->right);
default:
break;
}
}
cout<<"ERROR: cant compare this two types"<<endl;
} else if(cur->type == 2) {
assert(cur->opt > 0);
if(cur->opt == 1) return calc(cur->left) || calc(cur->right);
else if(cur->opt == 2) return calc(cur->left) && calc(cur->right);
else return !calc(cur->right);
}
}