-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.h
More file actions
1433 lines (1302 loc) · 45.9 KB
/
Copy pathfunction.h
File metadata and controls
1433 lines (1302 loc) · 45.9 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<string.h>
#include<mysql.h>
#include<sstream>
#include<stdlib.h>
#include<cstdio>
#include<string>
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL *connection, *mysql;
string query;
string command;
void SetColor(int Forg);
// add transition to array of linked list
void addToBeginList(List *list, char symbol, int next_state){
member *tmp;
tmp = new member;
tmp -> symbol = symbol;
tmp -> next_state = next_state;
if(list -> n == 0){
tmp -> next = NULL;
list -> front = tmp;
}else{
tmp -> next = list -> front;
list -> front = tmp;
}
list -> n += 1;
}
// queue is used to store new state in convert and minimize and it is used to store possible state in test string function
Queue* createEmptyQueue(){
Queue *Q;
Q = new Queue;
Q -> n = 0;
Q -> front = NULL;
Q -> rear = NULL;
return Q;
}
void enqueue(Queue *Q, int newData){
Element *e;
e = new Element;
e -> data = newData;
e -> next = NULL;
if(Q->n == 0){
Q->front = e;
Q->rear = e;
}else{
Q->rear->next = e;
Q->rear = e;
}
Q->n += 1;
}
void dequeue(Queue *Q){
Element *tmp;
tmp = Q->front;
Q->front = Q->front->next;
delete tmp;
Q->n -= 1;
}
// this function is used to test others function if they are correct such as test string
void displayQueue(Queue *Q){
Element *tmp;
tmp = Q->front;
while(tmp!=NULL){
cout <<" "<<tmp->data;
tmp = tmp->next;
}
cout<<"\n";
}
// this function is used to check a queue if it contains the final state
bool isFinal(Queue *Q, int n_final, int *final){
Element *tmp;
tmp = Q -> front;
while(tmp!=NULL){
for(int j=0;j<n_final;j++){
if(tmp->data == final[j]){
return true;
}
}
tmp = tmp -> next;
}
return false;
}
// this function is used to check if a state already exists in queue
bool isNotExistInQueue(Queue *Q, int state){
Element *tmp;
tmp = Q -> front;
while(tmp != NULL){
if(tmp -> data == state){
return false;
}
tmp = tmp -> next;
}
return true;
}
// in edit function we need this to delete transition from state
List *DeleteTranFromState(List **ArrayOfState, char symbol, int next_state, int state){
member* tmp = ArrayOfState[state] -> front;
member* prev = NULL;
while(tmp!=NULL){
if(tmp->symbol==symbol && tmp->next_state==next_state){
if(prev == NULL){
ArrayOfState[state] -> front = tmp -> next;
delete tmp;
break;
}else{
if(tmp->next == NULL){
prev->next = NULL;
}
prev -> next = tmp -> next;
delete tmp;
break;
}
}else{
prev = tmp;
tmp = tmp -> next;
}
}
ArrayOfState[state]->n -= 1;
return ArrayOfState[state];
}
// recursive function used to delete symbol from all states of FA
List *DeleteSymbolFromState(List **ArrayOfState, char symbol, int state){
member* tmp = ArrayOfState[state] -> front;
member* prev = NULL;
while(tmp!=NULL){
if(tmp->symbol==symbol){
if(prev == NULL){
ArrayOfState[state] -> front = tmp -> next;
delete tmp;
break;
}else{
if(tmp->next == NULL){
prev->next = NULL;
}
prev -> next = tmp -> next;
delete tmp;
break;
}
}
prev = tmp;
tmp = tmp -> next;
}
if(tmp!=NULL){
ArrayOfState[state] = DeleteSymbolFromState(ArrayOfState, symbol, state);
}
ArrayOfState[state]->n -= 1;
return ArrayOfState[state];
}
// this function used to copyQ in minimize, to code faster
Queue *copyQ(Queue *Q) {
Element *tmp = Q->front; //holds the current node
Queue *QueueCopy = createEmptyQueue();
//enqueue data into new Queue
while(tmp != NULL) {
enqueue(QueueCopy, tmp->data);
tmp = tmp->next;
}
return QueueCopy;
}
Queue *deleteQueue(Queue *Q){
Element *tmp = Q -> front;
while(tmp != NULL){
tmp = tmp->next;
Q->n -= 1;
delete Q->front;
Q->front=tmp;
}
return Q;
}
// this function is used to convert NFA to DFA. To define uniqueness of a set of states we need to do sum of square so that we can check later --
// if the set of states already exist
int checkIfExistSqSum(int * sumOfSquare, int p, int data){
for(int i=0;i<p;i++){
if(sumOfSquare[i]==data){
return i; // it is already exist and i is the state
break;
}
}
return -1; //not exist (create new state) increment new state keep looping
}
// display table of transitions of each states in FA
void displayTable(List **ArrayOfState,int n ,char* symbol,int n_symbol,int* final, int nf){
cout<<("\n\t\t\t\t =======");
for(int i=0; i<n_symbol+1; i++){
cout<<("================");
}
cout<<endl;
cout<<("\t\t\t\t|\t|");
for(int i=0; i<n_symbol+1; i++){
cout<<"\t"<<symbol[i]<<"\t|";
}
cout<<("\n\t\t\t\t =======");
for(int i=0; i<n_symbol+1; i++){
cout<<("================");
}
cout<<endl;
for(int i=0;i<n;i++){
cout<<("\t\t\t\t| ");
// int space=0;
for(int k=0; k<nf; k++){
if(final[k]==i){
cout<<"*";
}
// else{
// space = 1;
// }
}
if(i==0) cout<<("->");
// if(i!=0 && space==1){
// cout<<" ";
// }
cout<<"q"<<i<<"\t|\t";
for(int j=0; j<n_symbol+1; j++){
member* temp = ArrayOfState[i]->front;
member* temp1 = ArrayOfState[i]->front;
int count=0;
int count1=0;
while(temp!=NULL){
if(temp->symbol==symbol[j]){
// printf("q%d ",temp->next_state);
cout<<"q"<<temp->next_state;
if(temp->symbol=='e') count1++;
}else if(temp->symbol!='e'&&j==n_symbol){
count1++;
if(count1==1) cout<<("");
else cout<<("");
}
temp=temp->next;
}
cout<<("\t|\t");
}
cout<<("\n\t\t\t\t =======");
for(int i=0; i<n_symbol+1; i++){
cout<<("================");
}
cout<<endl;
}
}
// check if a state has epsilon transition
int hasEpsilonTran(List **ArrayOfState, int currentState){
member *tmp;
tmp = ArrayOfState[currentState] -> front;
while(tmp!= NULL){
if(tmp->symbol == 'e'){
return tmp->next_state; // if a state has epsilon transition return next state
}
tmp = tmp -> next;
}
return -1; // if it does not return -1 that means no epsilon transition
}
// the process of test string is to input one by one character in order to find next state
// We found next state by transition we enqueue one by one until the end of the list
// When we finished testing a symbol into a state we dequeue the old state that we have processed the symbol.
// Recursive function: we use it because it is easily do the transition by one index and it is also easy to enqueue and dequeue with correct segementation
void testString(List* ArrayOfState[], Queue *Q , string str, int n, int final[], int n_final ,int index){
// check if the index of string reaches the end, that means all char has process in functions
if(index == str.length()){
if(isFinal(Q, n_final, final)){ // check if the Queue contains final state
cout << "\n\t\t\t\tString";SetColor(2); // Queue contains final state then string accepted
cout << " accepted";SetColor(11);
//displayQueue(Q);
}else{
cout << "\n\t\t\t\tString";SetColor(4);
cout << " rejected";SetColor(11);
//displayQueue(Q);
}
}else{
int k = Q->n;
for(int i=0;i<k;i++){
Element* tmp1 = Q -> front;
member* tmp = ArrayOfState[tmp1->data] -> front;
while(tmp != NULL){
if(str[index] == tmp -> symbol){ // check if there are transitions with symbol that we input
enqueue(Q, tmp -> next_state); // if it contains the symbol we enqueue next state to Queue
//cout << "After enqueue: "; displayQueue(Q);
}
tmp = tmp -> next;
int a = hasEpsilonTran(ArrayOfState, tmp1->data); // check if there are epsilon transitions of the state
if(a!=-1){
enqueue(Q, a); // if it contains epsilon transition then we enqueue next state
}
//cout << "After enqueue: "; displayQueue(Q);
}
dequeue(Q); // after enqueue all next state we need to dequeue the state that we have already test symbol
//cout << "Afer dequeue: "; displayQueue(Q);
}
testString(ArrayOfState, Q, str, n, final , n_final, index+1); // the process is the same until it finishes the last character of the string
}
}
// to test that it is DFA or NFA:
// check e transition if it contains epsilon transition then it's NFA
// check number of transition in each state if it's different from number of symbol then it's NFA
// check each symbol in state if there are the same symbol in two transition then it's NFA
// We try to check if it is a NFA by characteristics of NFA
bool isDFA(List **ArrayOfState, int n, char *symbol, int n_symbol){
for(int i=0;i<n;i++){ // loop access to each state
int count = 0; // check if each states have equal transition and symbol
member * tmp = ArrayOfState[i] -> front;
while(tmp != NULL){
if(tmp -> symbol == 'e'){
return false;
}
tmp = tmp -> next;
count += 1;
}
if(count != n_symbol){
return false;
}
char sym_in_state[n_symbol];
int k=0;
tmp = ArrayOfState[i] -> front;
// copy symbol from state into 'sym_in_state' in order to compare
while(tmp != NULL){
sym_in_state[k] = tmp -> symbol;
tmp = tmp->next;
k++;
}
//compare all symbol in state to check if it is the same
// if it's the same it's NFA
for(int i=0;i<n_symbol;i++){
for(int j=i+1;j<n_symbol;j++){
if(sym_in_state[i] == sym_in_state[j]){
return false;
}
}
}
}
return true;
}
void convertNFAtoDFA(List **ArrayOfState1 ,int *final, int numFinal, char *symbol, int numSymbol){
Queue *Q[100];
List *NewArrayOfState[100];
int sq_sum[100];
//Allocate memory for Array of linked list
for(int i=0; i<100; i++){
NewArrayOfState[i] = createEmptyList();
}
Queue *Qtmp, *Qtmp1;
int newFinal[100], newNumF = 0;
// take Q = 0 as a new state;
Q[0] = createEmptyQueue();
enqueue(Q[0],0); // create a new start state
Qtmp1 = createEmptyQueue();
int next_state = hasEpsilonTran(ArrayOfState1, 0); // check if a new start state has epsilon transition
if(next_state!=-1){
enqueue(Q[0], next_state); // if it has epsilon transition to a state then we include the next state and state q0 as a new state
}
// initialize new q0 of DFA
int S = 0; // result sum of square in Queue after enqueue
int CurrentState=0, NewState=1; // this two variable is used to loop when there is no more new state it will stop the loop
// find sum of square in the start state
Element *Tmp = Q[0]->front;
while(Tmp !=NULL){
S += pow(2,Tmp->data);
Tmp = Tmp->next;
}// Start state;
sq_sum[0] = S;
// Check if q0 is the final state.
if(isFinal(Q[0], numFinal, final)){
newFinal[newNumF] = 0;
newNumF++;
}
while(CurrentState < NewState){
// Loop current symbol in FA
for(int i=0; i<numSymbol;i++){ // transition through all symbols
Qtmp = copyQ(Q[CurrentState]); // we need two queue, one to store states of current new state
Qtmp1 = deleteQueue(Qtmp1); // while is Qtmp1 is used to enqueue to create a possible new state
int count = Qtmp->n;
for(int j=0; j<count; j++){
// tmp1 for data in queue
// tmp for test current data in graph
Element *tmp1 = Qtmp->front;
member *tmp = ArrayOfState1[tmp1->data]->front;
while(tmp!=NULL){
if(tmp->symbol == symbol[i]){
if(isNotExistInQueue(Qtmp1,tmp->next_state)){
enqueue(Qtmp1,tmp->next_state);
// displayQueue(Qtmp1);
}
}
tmp = tmp->next;
}
dequeue(Qtmp);
}
Qtmp = copyQ(Qtmp1);
// Apply epsilon closure for the epsilon transition.
count = Qtmp->n;
for(int k = 0; k < count; k++){
Element *tmp1 = Qtmp->front;
member *tmp = ArrayOfState1[tmp1->data]->front;
while(tmp!=NULL){
if(tmp->symbol =='e'){
if(isNotExistInQueue(Qtmp1,tmp->next_state)){
enqueue(Qtmp1,tmp->next_state);
}
}
tmp = tmp -> next;
}
dequeue(Qtmp);
}
// finding value of S to test if the set of states already exist
int S=0;
Element *tmp4 = Qtmp1->front;
while(tmp4!=NULL){
S += pow(2,tmp4->data);
tmp4 = tmp4->next;
}
// if it exists then we add transition of the symbol to the i next state (i is a state that already exist)
int index = checkIfExistSqSum(sq_sum,NewState,S);
if(index==-1){// if it does not exist then it is a new state so that we save it as a new state
sq_sum[NewState] = S;
Q[NewState] = copyQ(Qtmp1);
addToBeginList(NewArrayOfState[CurrentState], symbol[i], NewState);
// check if the current state contains final state in NFA;
if(isFinal(Qtmp1,numFinal,final)){// If found in transition
// take it as final state of DFA
newFinal[newNumF] = NewState;
newNumF++;
}
NewState++; // when the new state found we increment to keep while loop working until the end of new states found
}else{
// if it exists
addToBeginList(NewArrayOfState[CurrentState], symbol[i], index);
}
}
CurrentState++;
}
cout<<endl<<"\t\t\t\tAn equivalent DFA!"<<endl;
n_final = newNumF;
n = NewState;
for(int i=0;i<NewState;i++){
ArrayOfState1[i] = NewArrayOfState[i];
}
for(int i=0;i<newNumF;i++){
final[i] = newFinal[i];
}
//displayTable(ArrayOfState, NewState, symbol, numSymbol, final, n_final);
}
bool checkIfMarked(int a, int b, int k, int **mat){
if(a>=b){
if( mat[a][b]==1 ){
return true;
}
else if(mat[a][b]==0){
return false;
}
}
else if(a<b){
if( mat[b][a]==1 ){
return true;
}
else if(mat[b][a]==0){
return false;
}
}
return false;
}
bool matrixChanged( int k, int** mat1, int** mat2){
for(int i=0;i<k-1;i++){
for(int j=i+1;j<k;j++){
if( mat1[j][i] != mat2[j][i]){
return true;
break;
}
}
}
return false;
}
//function for minimize a DFA
void minimizeDFA(List** ArrayOfState, int n_state, int* final, int nf ,char* symbol, int n_symbol){
//copy array of linked list
List* NewArrayOfState[n_state];
for(int i=0;i<n_state;i++){
NewArrayOfState[i] = ArrayOfState[i];
}
Queue *Q1 = createEmptyQueue();
Queue *Q2 = createEmptyQueue();
enqueue(Q1,0); // create start state
enqueue(Q2,0);
// step one: we need to find accessible states and delete non accessible states
//find all accessible state:
while(Q2->n!=0){
Element *tmp = Q2 -> front;
member* tmp1 = NewArrayOfState[tmp->data] -> front; //temp for adjacency list of graph
while (tmp1 != NULL){
if(isNotExistInQueue(Q1,tmp1->next_state)){
enqueue(Q1,tmp1->next_state); // save all next state that means the state is accessible
enqueue(Q2,tmp1->next_state);
}
tmp1=tmp1->next;
}
dequeue(Q2);
}
//check non accessible state
int as[n_state]; //array for store accessible states
int nas[n_state]; //array for store non accessible states
int n_nas=0; //number of non accessible state
int n_as=0; //number of accessible state
for(int i=0; i<n_state; i++){
if(isNotExistInQueue(Q1,i)){
nas[n_nas] = i;
n_nas++;
}
else{
as[n_as]=i;
n_as++;
}
}
//displayQueue(Q1);
//delete non accessible state
for (int i=0;i<n_nas;i++){ //delete graph of non accessible state
NewArrayOfState[nas[i]]= createEmptyList();
}
// create 2D array to do first second third iteration
int **mat = new int*[n_as];
for(int i=0;i<n_as;i++){
mat[i] = new int[n_as];
}
int **matcopy = new int*[n_as];
for(int i=0;i<n_as;i++){
matcopy[i] = new int[n_as];
}
//initail zero to lower triangle of matrix
for(int i=0;i<n_as;i++){ // where i:column j:row
for(int j=0;j<n_as;j++){
mat[j][i]=0;
matcopy[j][i]=0;
}
}
// for(int i=0;i<n_as;i++){
// for(int j=0;j<n_as;j++){
// cout << mat[i][j]<<" ";
// }
// cout<<"\n";
// }
//displayQueue(Q1);
//first iteration, mark all pairs that contain final state, but not include pair of 2 final states
for(int i=0;i<n_as-1;i++){
for(int j=i+1;j<n_as;j++){
for(int k=0;k<nf;k++){
if( as[i]==final[k]){
mat[j][i]=1; // mark it if it contains final state
}
}
}
}
for(int i=0;i<n_as-1;i++){
for(int j=i+1;j<n_as;j++){
for(int k=0;k<nf;k++){
if( as[j]==final[k]){
if(mat[j][i]==0){
mat[j][i]=1;
}
else if(mat[j][i]==1){ //exclude pair of 2 final states;
mat[j][i]=0;
}
}
}
}
}
//displayQueue(Q1);
while (matrixChanged(n_as, mat , matcopy)){
//copy matrix to compare
for(int i=0;i<n_as-1;i++){
for(int j=i+1;j<n_as;j++){
matcopy[j][i]=mat[j][i];
}
}
// for(int i=0;i<n_as;i++){
// for(int j=0;j<n_as;j++){
// cout << matcopy[i][j]<<" ";
// }
// cout<<"\n";
// }
int x1,x2,index_x1,index_x2;
for(int i=0;i<n_as-1;i++){
for(int j=i+1;j<n_as;j++){
if(mat[j][i]==0){ //for pair that is not marked yet
for(int t=0;t<n_symbol;t++){ //note that: each pair of state is ( as[i],as[j] ) i,j index of matrix
member* temp = NewArrayOfState[as[i]] -> front; //find as[i] transition to what?
while (temp != NULL){
if ( symbol[t] == temp->symbol){
x1=temp->next_state;
break;
}
temp=temp->next;
}
member* temp1 = NewArrayOfState[as[j]] -> front; //find as[j] transition to what?
while (temp1 != NULL){
if (symbol[t] == temp1->symbol){
x2=temp1->next_state;
break;
}
temp1=temp1->next;
}
//get index
for(int r=0;r<n_as;r++){
if(as[r]==x1){
index_x1=r;
break;
}
}
for(int r=0;r<n_as;r++){
if(as[r]==x2){
index_x2=r;
break;
}
}
//check if the pair of after-transition of ( as[i],as[j] ) is already marked?
if(checkIfMarked(index_x1, index_x2 ,n_as, mat)){
mat[j][i]=1;
break;
}
}
}
}
}
}
// displayQueue(Q1);
int deletestate[n_as];
int n_delstate=0;
for(int i=0;i<n_as-1;i++){
for(int j=i+1;j<n_as;j++){
if(mat[j][i]==0){
// display
for(int i=0;i<n_as;i++){
for(int j=0;j<n_as;j++){
// cout << matcopy[i][j]<<" ";
}
// cout<<"\n";
}
// display
deletestate[n_delstate]=as[j];
n_delstate++;
//delete one of the equivalent state : delete its graph
// here we delete big-number state which is as[j]
NewArrayOfState[as[j]]= createEmptyList();
// change all as[j] to as[i]
for(int k=0;k<n_as;k++){
member* temp = NewArrayOfState[as[k]] -> front;
while (temp != NULL){
if ( as[j] == temp->next_state){
temp->next_state = as[i];
}
temp=temp->next;
}
}
}
}
}
//error
int n_newstate=0; //number of accessible state - number of deleted state
int newstate[n_as];
int n_newfinal=0;
int newfinal[nf];
//new state:
for(int k=0;k<n_as;k++){
int r=0;
for(int j=0;j<n_delstate;j++){
if( as[k] == deletestate[j] ){
r=1; //meaning it is the deleted state
break;
}
}
if(r==0){
newstate[n_newstate]=as[k];
n_newstate++;
}
}
//find new final state after delete equivalent state;
for(int k=0;k<nf;k++){
int re=0;
for(int j=0;j<n_delstate;j++){
if( final[k] == deletestate[j] ){
re=1; //meaning it is the deleted state
break;
}
}
if(re==0){
newfinal[n_newfinal]=final[k];
n_newfinal++;
}
}
//change to new states to order index (0,1,2,3,....)
for(int i=0;i<n_as;i++){
member* temp = NewArrayOfState[i] -> front;
while (temp != NULL){
for(int k=0;k<n_newstate;k++){
if ( newstate[k] == temp->next_state){
temp->next_state = k;
break;
}
}
temp=temp->next;
}
}
////change index of new final state
for (int i=0;i<n_newfinal;i++){
for(int j=0;j<n_newstate;j++){
if(newfinal[i]==newstate[j]){
newfinal[i]=j;
break;
}
}
}
////
List* NewArrayOfState1[n_newstate];
for(int k=0;k<n_newstate;k++){
NewArrayOfState1[k] = createEmptyList();
}
// error here
for(int k=0;k<n_newstate;k++){
NewArrayOfState1[k]=NewArrayOfState[newstate[k]];
}
cout << "\n\n\t\t\t\tTransition of the minimize DFA:\n";
// for(int i=0;i<n_newstate;i++){
// member *tmpp = NewArrayOfState1[i] -> front;
// while(tmpp!=NULL){
// cout << "Symbol: " << tmpp -> symbol << " -> " << tmpp->next_state <<endl;
// tmpp=tmpp->next;
// }
// cout << "\n\n";
// }
//displayTable(NewArrayOfState1, n_newstate, symbol, n_symbol, newfinal, n_newfinal);
n_final = n_newfinal;
n = n_newstate;
for(int i=0;i<n_newstate;i++){
ArrayOfState[i] = NewArrayOfState1[i];
}
for(int i=0;i<n_newfinal;i++){
final[i] = newfinal[i];
}
}
//Database
string ConvertIntToString(int id){
string ID;
stringstream ss;
ss<<id;
ss>>ID;
return ID;
}
string ConvertCharToString(char symbol){
string ID;
stringstream ss;
ss<<symbol;
ss>>ID;
return ID;
}
//Load
void LoadDataFromDB(){
mysql_query (connection, "Select * from fa;");
result = mysql_store_result(connection);
printf("\n\t\t =======================================================================================\n");
printf("\t\t | %-2s | %-60s |", "ID", "Description");
printf("\n\t\t =======================================================================================\n");
while(row = mysql_fetch_row(result)){
printf("\t\t | %-2s | %-60s | \n", row[0], row[1]);
}
printf("\t\t =======================================================================================\n");
mysql_free_result(result);
}
int findnumberstate(int id){
mysql_query (connection, "Select * from states;");
result = mysql_store_result(connection);
int i=0;
while ((row = mysql_fetch_row(result))){
if(stoi(row[0]) == id){
i++;
}
}
return i;
mysql_free_result(result);
}
int findnumbersymbol(int id){
mysql_query (connection, "Select * from alphabet;");
result = mysql_store_result(connection);
int i=0;
while ((row = mysql_fetch_row(result))){
if(stoi(row[0]) == id){
i++;
}
}
return i;
mysql_free_result(result);
}
void LoadSymbol(int id,char *symbol){
mysql_query (connection, "Select * from alphabet;");
result = mysql_store_result(connection);
int i=0;
while ((row = mysql_fetch_row(result))){
if(stoi(row[0]) == id){
string s1;
s1 = row[1];
char n[2];
stringstream ss;
ss<<s1;
ss>>n;
symbol[i] = n[0];
i++;
}
}
mysql_free_result(result);
}
int findnumberfinalstate(int id){
int i=0;
mysql_query (connection, "Select * from states;");
result = mysql_store_result(connection);
while ((row = mysql_fetch_row(result))){
if(stoi(row[0]) == id){
if(stoi(row[3]) == 1){
i++;
}
}
}
return i;
mysql_free_result(result);
}
void LoadFinalState(int id, int *final){
mysql_query (connection, "Select * from states;");
result = mysql_store_result(connection);
int i=0;
while((row = mysql_fetch_row(result))){
if(stoi(row[0]) == id){
if(stoi(row[3]) == 1){
final[i] = stoi(row[1]);
i++;
}
}
}
mysql_free_result(result);
}
int findnumbertransition(int id, int state){
int i=0;
string ID = ConvertIntToString(id);
command = "SELECT * FROM transitions WHERE fa_id = "+ID+";";
mysql_query(connection, command.c_str());
result = mysql_store_result(connection);
while ((row = mysql_fetch_row(result))){
if(stoi(row[1]) == state){
i++;
}
}
return i;
mysql_free_result(result);
}
void LoadTransition(int id, int state, char *symboltran, int *nextstate){
int i=0;
string ID = ConvertIntToString(id);
command = "SELECT * FROM transitions WHERE fa_id = "+ID+";";
mysql_query (connection, command.c_str());
result = mysql_store_result(connection);
while ((row = mysql_fetch_row(result))){
if(stoi(row[1]) == state){
string s1;
s1 = row[2];
char n[2];
stringstream ss;
ss<<s1;
ss>>n;
symboltran[i] = n[0];
nextstate[i] = stoi(row[3]);
i++;
}
}
mysql_free_result(result);
}
void Loadtransition(int id, int state, List *ArrayOfState, int n_tran){
char symboltran[n_tran];
int nextstate[n_tran];
LoadTransition(id,state,symboltran,nextstate);
for(int i=0; i<n_tran; i++){
addToBeginList(ArrayOfState,symboltran[i],nextstate[i]);
}
}
//Insert
int InsertDataToFA(){
string des;
cout<<"\n\t\t\t\tEnter FA description: ";
getline(cin, des); fflush(stdin);
query = "Insert into fa (description) values ('"+des+"');";
mysql_query(connection, query.c_str());
mysql_query(connection, "Select MAX(id) from fa;");
result = mysql_store_result(connection);
int fa_id;
while(row = mysql_fetch_row(result)){
fa_id = stoi(row[0]);
}
return fa_id;
mysql_free_result(result);
}
void InsertSymbol(int id, char symbol){
string ID = ConvertIntToString(id);
string Sym = ConvertCharToString(symbol);
query = "Insert into alphabet (fa_id, symbol) values ('"+ID+"','"+Sym+"');";
mysql_query(connection, query.c_str());
}
void InsertState(int id, int state, bool start_state, bool final_state){
string ID = ConvertIntToString(id);
string StateString = ConvertIntToString(state);
if(start_state && final_state){
query = "Insert into states (fa_id,state,start_state,final_state) values ('"+ID+"','"+StateString+"','1','1');";
}
else if(start_state || final_state){
if(start_state){
query = "Insert into states (fa_id,state,start_state,final_state) values ('"+ID+"','"+StateString+"','1','0');";
}
else if(final_state){
query = "Insert into states (fa_id,state,start_state,final_state) values ('"+ID+"','"+StateString+"','0','1');";
}
}
else{
query = "Insert into states (fa_id,state,start_state,final_state) values ('"+ID+"','"+StateString+"','0','0');";
}
mysql_query(connection,query.c_str());
}
void InsertDataToTransition(int id, int current_state, char symbol, int next_State){
string ID = ConvertIntToString(id);
string firstState = ConvertIntToString(current_state);
string Sym = ConvertCharToString(symbol);
string lastState = ConvertIntToString(next_State);
query = "Insert into transitions (fa_id, state, symbol, next_state) values ('"+ID+"','"+firstState+"','"+Sym+"','"+lastState+"');";