-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
374 lines (231 loc) · 5.28 KB
/
main.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include<string>
#include <queue>
using namespace std;
//Element Of the Circuit.
struct Node{
int type; //Type of the element in the circuit.
/*(0)->Input (1)->Negate Gate (2)->OR Gate (3)->AND Gate (4)->XOR Gate */
int index; //Index number of this element.
bool data; //Truth value Of this element.
vector<Node*> children;//Children Of this node.
};
//Keep the nodes in an array.
vector<Node>nodes;//Nodes.
//Reprent circuit as adjacent matrix.
vector< vector< int > > adjmat;
int main(int argc, char **argv){
bool checkCycle(vector<vector<int> > &graph,int N,int M, int K);
int findchildren( int index,int N,int M,int K);
void calculate(Node& node);
void orOperate(Node& node);
void andOperate(Node& node);
void negateOperate(Node& node);
void exclusiveOperate(Node& node);
//Read the input file.
string i=argv[1];
string o=argv[2];
ifstream input;
input.open(i.c_str() );
int N;
int M;
int K;
input >> N;//Number Of Inputs.
input >> M;//Number Of Gates.
input >> K;//Number Of Outputs.
vector< vector< int > > adjmat2(N+M+K,vector< int >(N+M+K));
adjmat=adjmat2;
ofstream output;
output.open(o.c_str());
//Create input nodes.
for (int i = 0; i < N; i++){
Node node;
node.index = i;
node.type = 0;
int a;
input >> a;
if (a == 1)
node.data = true;
else if (a == 0)
node.data = false;
nodes.push_back(node);
}
//Create gates.
for (int i = N; i < N + M; i++){
Node node;
node.index = i;
string a;
input >> a;
if (a=="N"){
node.type = 1;
}
if (a=="A"){
node.type = 2;
}
if (a=="R"){
node.type = 3;
}
if (a=="X"){
node.type = 4;
}
nodes.push_back(node);
}
//Create output Nodes.
for (int i = N + M; i < N + M + K; i++){
Node node;
string q;
input>>q;
node.index = i;
node.type = 5;
nodes.push_back(node);
}
for (int i = 0; i < N + M + K;i++){
string sub;
int numb;
string s;
getline(input, s);
istringstream iss(s);
string consume;
iss>>consume;
while(iss>>sub){
stringstream ( sub ) >>numb ;
adjmat[i-1][numb] = 1;
}
adjmat[i][i] = 0;
}
//Check if there is a cycle in the circuit.
if(checkCycle(adjmat, N, M, K)){
output<<-1;
return 1;
}
//Create a parental relation starting from outputs.
for (int i = N + M; i < N + M + K; i++){
queue< Node*> q;
Node* root = &nodes[i];
q.push(root);
while ((q.empty())==false){
Node* node = q.front();
q.pop();
int size=findchildren(node->index,N,M,K);
for (int j = 0; j <size; j++){
if(node->children[j]->type!=0)
q.push((node->children[j]));
}
}
calculate(*(nodes[i].children[0]));
nodes[i].data=(nodes[i].children[0]->data);
}
for(int i=0;i<M+N+K;i++){
if(i!=M+N+K-1){
if(nodes[i].data==false){
output<<"0"<<" ";
}else if(nodes[i].data=true){
output<<"1"<<" ";
}
}else{
if(nodes[i].data==false){
output<<"0";
}else if(nodes[i].data=true){
output<<"1";
}
}
}
}
//Topological Sort.
bool checkCycle(vector<vector<int> >& graph,int N,int M,int K){
int indeg[N+M+K];
bool visited[N+M+K];
int count=0;
int cycle=0;
for(int i=0;i<N+M+K;i++){
indeg[i]=0;
visited[i]=false;
}
for(int i=0;i<N+M+K;i++){
for(int j=0;j<M+N+K;j++){
indeg[i]+=graph[j][i];
}
}
while(count<N+M+K){
int i=0;
for(i=0;i<N+M+K;i++){
if(indeg[i]==0 && visited[i]==false){
visited[i]=true;
count++;
break;
}
}
if(i==N+M+K && count<N+M+K){
return true;
}
for(int j=0; j<N+M+K;j++){
indeg[j]-=graph[i][j];
}
}
return false;
}
//Find the children of an element.
int findchildren(int index,int N,int M,int K){
for (int i = 0; i<N + M + K; i++){
if (adjmat[i][index] == 1){
Node* node = &nodes[i];
if(nodes[index].children.size()!=2)
nodes[index].children.push_back(node);
}
}
return nodes[index].children.size();
}
//Find the truth value of a node.
void calculate(Node& node){
void findchildren( int& index,int N,int M,int K);
void calculate(Node& node);
void orOperate(Node& node);
void andOperate(Node& node);
void negateOperate(Node& node);
void exclusiveOperate(Node& node);
if(node.type==0)
return;
else if (node.type == 1){
negateOperate(node);
}
else if (node.type == 3){
orOperate(node);
}
else if (node.type == 2){
andOperate(node);
}
else if (node.type == 4){
exclusiveOperate(node);
}
}
//Performs 'and' operation.
void andOperate(Node& node){
for(int i=0;i<node.children.size();i++){
calculate(*(node.children[i]));
}
node.data = node.children[0]->data && node.children[1]->data;
}
//Performs 'or' operation.
void orOperate(Node& node) {
for(int i=0;i<node.children.size();i++){
calculate(*(node.children[i]));
}
node.data = node.children[0]->data || node.children[1]->data;
}
//Performs the negation.
void negateOperate(Node& node){
for(int i=0;i<node.children.size();i++){
calculate(*(node.children[i]));
}
node.data = !((node.children[0]->data));
}
//Performs 'exclusive or' operation.
void exclusiveOperate(Node& node){
for(int i=0;i<node.children.size();i++){
calculate(*(node.children[i]));
}
node.data = node.children[0]->data xor node.children[1]->data;
}