-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasinaturinguniversala.cpp
286 lines (251 loc) · 8.67 KB
/
masinaturinguniversala.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
282
283
284
285
286
/*
* Copyright (C) 2015 Floris Andrei Stoica Marcu <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "masinaturinguniversala.h"
#include <QDebug>
#include <QStringList>
MasinaTuringUniversala::MasinaTuringUniversala()
{
stepI = 0;
stepJ = 0;
stepK = 0;
}
QList<Stare*> MasinaTuringUniversala::Stari() const
{
return m_Stari;
}
void MasinaTuringUniversala::setStari(QList<Stare*> Stari)
{
m_Stari = Stari;
}
QList<Banda*> MasinaTuringUniversala::Benzi() const
{
return m_Benzi;
}
void MasinaTuringUniversala::setBenzi(QList<Banda*> Benzi)
{
m_Benzi = Benzi;
}
int MasinaTuringUniversala::nrBenzi() const
{
return m_nrBenzi;
}
void MasinaTuringUniversala::setNrBenzi(int nrBenzi)
{
m_nrBenzi = nrBenzi;
}
void MasinaTuringUniversala::addBanda(Banda* banda)
{
}
void MasinaTuringUniversala::addStare(int stare1, int stare2, Trecere *aux)
{
qDebug()<<"rulez: "<<stare1<<" "<<stare2;
int stare1i=-1,stare2i=-1;
for(int i=0;i<m_Stari.count();i++){
if(m_Stari.at(i)->numar == stare1)
{
stare1i = i;
}
if(m_Stari.at(i)->numar == stare2)
{
stare2i = i;
}
}
Stare *starea1;
Stare *starea2;
if(stare1i>=0){
qDebug()<<"Stare existenta "<<stare1;
starea1 = m_Stari.at(stare1i);
} else {
starea1 = new Stare();
starea1->numar = stare1;
m_Stari.append(starea1);
}
if(stare2i>=0){
qDebug()<<"Stare existenta "<<stare1;
starea2 = m_Stari.at(stare2i);
} else {
starea2 = new Stare();
starea2->numar = stare2;
m_Stari.append(starea2);
}
aux->setStareOrig(starea1);
aux->setStareDest(starea2);
starea1->addTrecere(aux);
}
//incarca starile din codare
void MasinaTuringUniversala::decodare()
{
//iterare inteligenta
QStringList real = m_continut.split(QString("000"));
//este masina pusa intre 000 si are ceva in ea?
if(real.count()==3 && !real.at(1).isEmpty()){
qDebug()<<"Verificare preliminara a masinii ok.";
//gasesc partile stringului intre separatorii de tip 00
QStringList treceri = real.at(1).split(QString("00"));
if(treceri.count())
for(int j=0; j<treceri.count(); j++){
qDebug()<<"Pentru indexul "<<j<<" : "<<treceri.at(j);
QStringList partiTreceri = treceri.at(j).split(QString("0"));
if(partiTreceri.count()){
//stare1,s1,directie,s2,stare2
int stare1 = partiTreceri.at(0).length()-1;
if(stare1<0){
qDebug()<<QString("Nu am gasit starea 1 la trecerea ").append('0'+j);
return;
}
QChar s1 = decodareSimbol(partiTreceri.at(1).length());
if(s1 == QChar('N')){
qDebug()<<QString("Nu am gasit simbol 1 la trecerea ").append('0'+j);
return;
}
int dir = decodareDirectie(partiTreceri.at(2).length());
if(dir == -3){
qDebug()<<QString("Nu am gasit directia la trecerea ").append('0'+j);
qDebug()<<partiTreceri.at(2);
return;
}
QChar s2 = decodareSimbol(partiTreceri.at(3).length());
if(s2 == QChar('N')){
qDebug()<<QString("Nu am gasit simbolul 2 la trecerea ").append('0'+j);
return;
}
int stare2 = partiTreceri.at(4).length()-1;
if(stare1<0){
qDebug()<<QString("Nu am gasit starea 1 la trecerea ").append('0'+j);
return;
}
Trecere *newTrecere = new Trecere(s1,dir,s2);
addStare(stare1,stare2,newTrecere);
}
}
} else {
qDebug()<<"Lipsesc separatorii de tip 000, vezi manual.";
}
}
void MasinaTuringUniversala::stepByStep()
{
QList<Trecere*> treceriCurente = m_Stari.at(stepI)->getTreceri();
if(treceriCurente.count()%m_Benzi.count()!=0){
qDebug()<<"La starea "<<stepI<<" nu aveti destule treceri pentru toate benzile.";
return;
}
stepK=0;
qDebug()<<"Treceri la starea curenta("<<stepI<<") :"<<treceriCurente.count();
for(int z=stepJ;z<treceriCurente.count();z++){
Trecere* trecereCurenta = treceriCurente.at(z);
qDebug()<<" "<<trecereCurenta->stareOrig()->numar<<" -> "<<trecereCurenta->stareDest()->numar;
int stareUrm = m_Benzi.at(stepK)->trecere(trecereCurenta);
if(stareUrm<0) continue;
if(stareUrm == 0 && trecereCurenta->directie() == 0){
emit terminat();
}
emit step(stepK,stareUrm);
if(stareUrm != stepI){
stepI = stareUrm;
qDebug()<<"Am trecut la starea: "<<stepI;
stepJ = 0;
} else {
qDebug()<<"Am ramas la starea: "<<stepI;
stepJ++;
}
stepK++;
if(stepK>m_Benzi.count()-1){
stepK=0;
}
break;
//qDebug()<<" "<<trecereCurenta->stareOrig()->numar<<" -> "<<trecereCurenta->stareDest()->numar;
}
stepJ=0;
}
void MasinaTuringUniversala::porneste()
{
bool ok = true;
int i = 0;
int k = 0;
qDebug()<<"Countul de stari: "<<m_Stari.count();
qDebug()<<"Continut: "<<m_Benzi.at(0)->continut();
while(ok)
{
QList<Trecere*> treceriCurente = m_Stari.at(i)->getTreceri();
if(treceriCurente.count()%m_Benzi.count()!=0){
qDebug()<<"La starea "<<i<<" nu aveti destule treceri pentru toate benzile.";
return;
}
k=0;
qDebug()<<"Treceri la starea curenta("<<i<<") :"<<treceriCurente.count();
for(int j=0;j<treceriCurente.count();j++){
Trecere* trecereCurenta = treceriCurente.at(j);
qDebug()<<" "<<trecereCurenta->stareOrig()->numar<<" -> "<<trecereCurenta->stareDest()->numar;
int stareUrm = m_Benzi.at(k)->trecere(trecereCurenta);
k++;
if(k>m_Benzi.count()-1){
k=0;
}
if(stareUrm<0) continue;
if(stareUrm == 0 && trecereCurenta->directie() == 0){
emit terminat();
ok=false;
break;
}
if(stareUrm != i){
i = stareUrm;
qDebug()<<"Am trecut la starea: "<<i;
} else {
qDebug()<<"Am ramas la starea: "<<i;
}
//qDebug()<<" "<<trecereCurenta->stareOrig()->numar<<" -> "<<trecereCurenta->stareDest()->numar;
}
}
qDebug()<<"Continut: "<<m_Benzi.at(0)->continut();
}
QString MasinaTuringUniversala::continut() const
{
return m_continut;
}
void MasinaTuringUniversala::setContinut(QString continut)
{
m_continut = continut;
}
QChar MasinaTuringUniversala::decodareSimbol(int a)
{
switch(a){
case 1: return QChar('0');
case 2: return QChar('1');
case 3: return QChar(' ');
}
return QChar('N');
}
int MasinaTuringUniversala::decodareDirectie(int a)
{
switch(a){
case 1: return -1;
case 2: return 1;
case 3: return 0;
}
return -3;
}
Stare* MasinaTuringUniversala::getStare(int nr)
{
for(int i=0;i<m_Stari.count();i++){
if(m_Stari.at(i)->numar == nr){
return m_Stari.at(i);
}
}
return new Stare();
}
#include "masinaturinguniversala.moc"