-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj.cc
155 lines (137 loc) · 2.95 KB
/
obj.cc
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "obj.h"
// obj defs
static int getprec(char c) {
if (c == '+' || c == '-') {return 0;}
else if (c == '*' || c == '/') {return 1;}
else if (c == '(') {return 2;}
else {return -1;}
}
Obj::Obj(int type) {
num = 0;
rchild = NULL; lchild = NULL;
this->type = (Objtype) type;
}
// new obj from string
Obj::Obj(char *tok) {
rchild = NULL; lchild = NULL;
if (isdigit(*tok)) {
type = TYPE_NUM;
num = atoi(tok); // num
} else {
type = TYPE_OP;
op = tok[0];
prec = getprec(op);
}
}
// recursive free
Obj::~Obj() {
if (rchild != NULL) {delete rchild;}
if (lchild != NULL) {delete lchild;}
}
string *Obj::prefix() {
string *str = new string();
prefix(str);
return str;
}
void Obj::prefix(string *str) {
if (this != NULL) {
if (type == TYPE_OP) {
str->append(toStr());
str->push_back(' ');
lchild->prefix(str);
str->push_back(' ');
rchild->prefix(str);
str->push_back(' ');
} else {str->append(toStr());}
} else {str->append(toStr());}
}
string *Obj::postfix() {
string *str = new string();
postfix(str);
return str;
}
void Obj::postfix(string *str) {
if (this != NULL) {
if (type == TYPE_OP) {
lchild->postfix(str);
str->push_back(' ');
rchild->postfix(str);
str->push_back(' ');
str->append(toStr());
str->push_back(' ');
} else {str->append(toStr());}
} else {str->append(toStr());}
}
string *Obj::infix() {
string *str = new string();
infix(str);
return str;
}
void Obj::infix(string *str) {
if (this != NULL) {
if (type == TYPE_OP) {
str->push_back('(');
lchild->infix(str);
str->push_back(' ');
str->append(toStr());
str->push_back(' ');
rchild->infix(str);
str->push_back(')');
} else {str->append(toStr());}
} else {str->append(toStr());}
}
string *Obj::tree_print() {
string *str = new string();
tree_print(str);
return str;
}
void Obj::tree_print(string *str) {
if (this != NULL) {
if (type == TYPE_OP) {
str->push_back('{');
str->append(toStr());
str->append(": (");
lchild->tree_print(str);
str->append(", ");
rchild->tree_print(str);
str->push_back('}');
str->push_back(')');
} else {str->append(toStr());}
} else {str->append(toStr());}
}
bool Obj::isValid() {
if (this == NULL) {return false;}
else if (type == TYPE_OP) {
if (lchild->isValid() && rchild->isValid()) {return true;}
else {return false;}
} else {
return true;
}
}
int64_t Obj::eval() {
if (this == NULL) {return 0;}
if (type == TYPE_OP) {
if (op == '+') {return lchild->eval() + rchild->eval();}
else if (op == '-') {return lchild->eval() - rchild->eval();}
else if (op == '*') {return lchild->eval() * rchild->eval();}
else if (op == '/') {return lchild->eval() / rchild->eval();}
else{return 0;}
} else {
return num;
}
}
string Obj::toStr() {
if (this != NULL) {
if (type == TYPE_OP) {
return string(1, op);
} else {
return to_string(num);
}
} else {
return "nil";
}
}