-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp5.c
More file actions
260 lines (204 loc) · 5.85 KB
/
p5.c
File metadata and controls
260 lines (204 loc) · 5.85 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// tree.c
/*
* 여러 파라미터일 때 elements 여러 개 받기
* checkCNF 구현한 뒤 CNF에 넣기
* checkSyntax 구현 // 괄호 문제가 아니라 아예 다른 인풋일 때에도 가능한 함수
*/
#include "p5.h"
#if 1
int main(){
struct Tree* root = NULL;
// read input
printf("input == ");
root = parseToTree(root);
if(root == NULL) printf("error!\n");
// print read
printf("read == ");
printTree(root);
printf("\n");
// do NNF and print
printf("NNF == ");
root = NNF(root);
printTree(root);
printf("\n");
// do CNF and print
printf("CNF == ");
root = CNF(root);
printTree(root);
printf("\n\n\n");
// change form of the output
printAnswer(root);
DPrintf(printf("> main root=%p \n",root););
}
#endif
struct Tree* newTree(int sign, int prop){
DPrintf(printf("< newTree sign=%d, prop=%d\n",sign,prop););
struct Tree* t = (struct Tree*) malloc(sizeof(struct Tree));
t->sign = sign;
t->prop = prop;
t->left = NULL;
t->right = NULL;
DPrintf(printf("> newTree=%p, sign=%d, prop=%d\n", t, sign,prop);
printf("t=%p, t=%p, t=%p \n\n", t, t->left ,t->right););
return t;
}
struct Tree* parseToTree(struct Tree* t){ // close the bracket or demorgan true: close; false: still open;
DPrintf(printf("< parseToTree t=%p\n",t));
char temp;
char ss[MAX_STR];
int sign=0;
int prop=0;
scanf("%s",ss);
DPrintf(printf("- parseToTree temp=%c, ss=%s\n", temp, ss););
if(ss[0] == '('){
if(!strcmp(ss,"(and")) sign = _AND;
else if(!strcmp(ss,"(or")) sign = _OR;
else if(!strcmp(ss,"(not")) sign = _NOT;
t = newTree(sign, prop);
t->left = parseToTree(t->left);
if(sign != _NOT)
t->right = parseToTree(t->right);
}
else{
sign = _NUM;
prop = ss[1] - '0';
t = newTree(sign, prop);
}
DPrintf(printf("> parseToTree t=%p\n",t));
return t;
}
void printTree(struct Tree* t){
DPrintf(printf("< printTree tree=%p\n",t););
if(t == NULL) return ;
switch(t->sign){
case _NUM: printf(" a%d", t->prop); break;
case _AND: printf(" (and"); break;
case _OR: printf(" (or"); break;
case _NOT: printf(" (not"); break;
}
printTree(t->left);
printTree(t->right);
if(t->sign !=_NUM)
printf(")");
DPrintf(printf("> printTree tree=%p\n",t););
}
struct Tree* demorgan(struct Tree* t){
DPrintf(printf("< demorgan tree=%p\n",t););
if(t->sign == _NUM) t->prop *= -1;
else if(t->sign == _AND || t->sign == _OR) {
t->sign = 3- t->sign;
t->left = demorgan(t->left);
t->right = demorgan(t->right);
}
else if(t->sign == _NOT) t = t->left;
DPrintf(printf("> demorgan tree=%p\n",t););
return t;
}
struct Tree* NNF(struct Tree* t){
DPrintf(printf("< NNF tree=%p\n",t););
if(t == NULL){
//printf("error! nullptr!\n");
return t;
}
if(t->sign == _NOT){ // meet 'demorgan'
t = demorgan(t->left);
}
else if(t->sign==_AND || t->sign==_OR){ // and || or
t->left = NNF(t->left);
t->right = NNF(t->right);
}
DPrintf(printf("> NNF tree=%p\n",t););
return t;
}
struct Tree* distribute(struct Tree* t){
DPrintf(printf("< distribute tree=%p\n",t););
struct Tree* p = NULL;
struct Tree* pp = NULL;
if((t->left->sign == _NUM && t->right->sign == _NUM) || t == NULL){
DPrintf(printf("> distribute tree t=%p\n",t););
return t;
}
else if(t->left->sign == _NUM){
p = t->right;
t->right = NULL;
pp = p->left;
p->left = copyTree(t);
p->left->right = pp;
pp = p->right;
p->right = copyTree(t);
p->right->right = pp;
deleteTree(t);
}
else {
p = t->left;
t->left = NULL;
pp = p->left;
p->left = copyTree(t);
p->left->left = pp;
pp = p->right;
p->right = copyTree(t);
p->right->left = pp;
deleteTree(t);
}
DPrintf(printf("> distribute tree p=%p\n",p););
return p;
}
struct Tree* CNF(struct Tree* t){
DPrintf(printf("< CNF tree=%p\n",t););
if(t == NULL || t->sign == _NUM){
//printf("error! nullptr!\n");
return t;
}
if(t->sign == _OR){ // meet 'or'
t = distribute(t);
t->left = CNF(t->left);
t->right = CNF(t->right);
}
else if(t->sign==_AND){ // if 'and'
t->left = CNF(t->left);
t->right = CNF(t->right);
}
DPrintf(printf("> CNF tree=%p\n",t););
return t;
}
struct Tree* copyTree(struct Tree* t){
DPrintf(printf("< copyTree tree=%p\n",t););
if(t==NULL) return t;
struct Tree* nt = newTree(t->sign,t->prop); // new tree
nt->left = copyTree(t->left);
nt->right = copyTree(t->right);
DPrintf(printf("> copyTree tree=%p\n",nt););
return nt;
}
void deleteTree(struct Tree* t){
DPrintf(printf("< deleteTree tree=%p\n",t););
if(t == NULL) return ;
deleteTree(t->left);
deleteTree(t->right);
free(t);
DPrintf(printf("> copyTree\n"););
}
void printAnswer(struct Tree*t){
DPrintf(printf("< printAnswer tree=%p",t););
if(t == NULL) return ;
switch(t->sign){
case _NUM: printf("%d ", t->prop);
printAnswer(t->left);
printAnswer(t->right);
break;
case _AND: printAnswer(t->left);
printf("\n");
printAnswer(t->right);
break;
case _OR: printAnswer(t->left);
printf(" ");
printAnswer(t->right);
break;
case _NOT: printf("-");
printAnswer(t->left);
printAnswer(t->right);
break;
default: break;
}
DPrintf(printf("> printAnswer tree=%p",t););
}