-
Notifications
You must be signed in to change notification settings - Fork 1
/
source.cpp
281 lines (236 loc) · 6.36 KB
/
source.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
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
#include <iostream>
#include <string>
#include "haci_hasan_savan_1901042704.h"
#include <fstream>
int main()
{
simulateCircuit("circuit.txt","input.txt");
return 0;
}
Gate::Gate(){}
string Gate::getName()const{return name;}
string Gate::getInput1()const{return "error";}
void Gate::setName(const string& n){name = n;}
string Gate::getInput2()const{return "error";}
int Gate::eval(){return out;}
void Gate::setOut(int o){out = o;}
int Gate::get_out(){return out;}
void Gate::setCheck(int s){check = s;}
int Gate::getCheck(){return check;}
Output::Output(){}
Output::Output(const string& s1)
: Gate(0,s1) {/*-------*/}
int Output::eval(){out = inp1->eval(); return out;}
And::And(){}
int And::eval(){return inp1->eval() && inp2->eval();}
string And::getInput1()const{return input1;}
string And::getInput2()const{return input2;}
void And::setInput1(const string& n){input1 = n;}
void And::setInput2(const string& n){input2 = n;}
Or::Or(){}
int Or::eval(){return inp1->eval() || inp2->eval();}
string Or::getInput1()const{return input1;}
string Or::getInput2()const{return input2;}
void Or::setInput1(const string& n){input1 = n;}
void Or::setInput2(const string& n){input2 = n;}
Not::Not(){}
int Not::eval(){return !inp1->eval();}
string Not::getInput2()const{return input1;}
string Not::getInput1()const{return input1;}
void Not::setInput1(const string& n){input1 = n;}
FlipFlop::FlipFlop(){}
FlipFlop::FlipFlop(const string& s1, const string& s2)
: Gate(0,s1), input1(s2) {/*-------*/}
int FlipFlop::eval()
{
int r_value;
if(inp1->eval() == former_out)
r_value =0;
else r_value = 1;
if(FlipFlop::former_out==0)
FlipFlop::former_out=1;
else FlipFlop::former_out=0;
out = r_value;
return r_value;
}
string FlipFlop::getInput1()const{return input1;}
string FlipFlop::getInput2()const{return input1;}
void FlipFlop::setInput1(const string& n){input1 = n;}
int FlipFlop::former_out = 0;
//typedef Gate* gatePtr;
/*
counts the whitespace number in a given string and returns it
ex: "INPUT a b c d" --> 4 whitespaces (4 object to be created)
ex: "OUTPUT d1 d2 d3 d4" --> 4 whitespaces (4 object to be created)
*/
int WhiteSpaceCounter(const string& s)
{
int x = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == ' ')
x++;
}
return x;
}
/*counts input number, output number and gates number that is in the given file*/
void counter(int& inp, int& out, int& gates, const string& file)
{
ifstream fin;
fin.open(file);
if (fin.is_open())
{
string tempLine,inpLine,outLine;
getline(fin, inpLine);
inp = WhiteSpaceCounter(inpLine);
getline(fin, outLine);
out = WhiteSpaceCounter(outLine);
while (!fin.eof())
{
gates++;
getline(fin, tempLine);
}
fin.close();
}
else cout << "error while openning the file! " << endl;
}
//creats gate pointer array: ||--a--|--b--|--c--|--d--|--and1--|--or1--|--n1--|.....||
Gate** createGatePointer(int& inp, int& gates, int& out)
{
Gate** gateP = new gatePtr[inp + gates+out];
for (int i = 0; i < inp + gates + out; i++)
gateP[i] = nullptr;
return gateP;
}
Gate** readCircuit_txt(const string& circuit_txt, Gate** gateP)
{
ifstream fin;
fin.open(circuit_txt);
int i = 0;
if (fin.is_open())
{
string temp,x,y,z;
string temp2,tempstr;
while (!fin.eof())
{
fin >> temp;
if (temp == "INPUT" || temp == "OUTPUT" || temp == "AND" || temp == "OR" ||
temp == "NOT" || temp == "FLIPFLOP" || temp == "DECODER")
temp2 = temp;
if (temp2 == "INPUT")
{
if (temp != "INPUT")
{
Gate* i1 = new Gate(0,temp);
gateP[i] = i1;
i++;
}
}
else if (temp2 == "OUTPUT")
{
if (temp != "OUTPUT")
{
Gate* i1 = new Gate(0,temp);
gateP[i] = i1;
gateP[i]->setCheck(1);
i++;
}
}
else if (temp2 == "AND")
{
fin >> x >> y >> z;
And* i1 = new And(x,y,z);
gateP[i] = i1;
i++;
//cout<<"i :"<<i<<endl;
}
else if (temp2 == "OR")
{
fin >> x >> y >> z;
Or* i1 = new Or(x, y, z);
gateP[i] = i1;
i++;
}
else if (temp2 == "NOT")
{
fin >> x >> z;
Not *i1 = new Not(x, z);
gateP[i] = i1;
i++;
}
else if (temp2 == "FLIPFLOP")
{
fin >> x >> z;
FlipFlop *i1 = new FlipFlop(x, z);
gateP[i] = i1;
i++;
}
else if (temp2 == "DECODER")
{
cout << "gelecek" << endl;
}
}
fin.close();
}
else cout << "error while opening the file (counter)" << endl;
return gateP;
}
//fills the gate** pointer with input.txt file
void fillInputValues(Gate** gp, const string& input_txt,int inpCount,int size)
{
ifstream fin;
ofstream fout;
fout.open("output.txt");
fin.open(input_txt);
int inpValue;
int counter=0;
if(fin.is_open() && fout.is_open())
{
/* Reading input.txt and assigning numbers to gp[i].out */
while(!fin.eof())
{
for(int i=0; i<inpCount; i++)
{
fin >> inpValue;
gp[i]->setOut(inpValue);
}
for(int k=0; k<size; k++)
{
//gp[k]->eval();
if(gp[k]->getCheck()==1)
{
//cout<<gp[k]->getName()<<" ";
fout<< gp[k]->eval()<<endl;
}
}
}
fout.close();
fin.close();
}
}
void simulateCircuit(const string& circuit_txt, const string& input_txt)
{
//1-creating 1d gate pointer array
//thesee int values needed for keeping the number information:
int inpCount =0, outCount=0, gateCount=0;
Gate** gp;
counter(inpCount,outCount,gateCount,circuit_txt);//fills the veriables
int size = inpCount+gateCount + outCount;
gp = createGatePointer(inpCount,gateCount,outCount);
//2-reading the circuit file and filling the gate**in poter
gp = readCircuit_txt(circuit_txt,gp);
//3-connecting operation
for(int k=0; k <size; k++)
{
// If input1 or input2 is found, inp1 and inp2(struct pointers) holds the adress of that locations.
for(int m=0; m < size; m++)
{
if(gp[m]->getName() == gp[k]->getInput1())
gp[k]->setPoint1(gp[m]); // Adress transfer
if(gp[m]->getName() == gp[k]->getInput2())
gp[k]->setPoint2(gp[m]);
}
}
//4-filling input gates from input.txt
fillInputValues(gp, input_txt, inpCount, size);
}