-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCcode.c
More file actions
1534 lines (1317 loc) · 51.2 KB
/
Copy pathCcode.c
File metadata and controls
1534 lines (1317 loc) · 51.2 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
/**--------------------------------------------------------**/
/** C o n v e r s i o n Z vers C (Standard) **/
/** Realisee par Pr D.E ZEGOUR **/
/** E S I - Alger **/
/** Copywrite 2014 **/
/**--------------------------------------------------------**/
/*////////////////////////////////////////////////////////////////////////*/
/*////////////////////////////////////////////////////////////////////////*/
/*Second Lab by :Kouah Akram (Group 02) & Korchi Adam (Group15)*/
/*////////////////////////////////////////////////////////////////////////*/
/*////////////////////////////////////////////////////////////////////////*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef int bool ;
typedef char * string255 ;
typedef char * string2 ;
#define True 1
#define False 0
/** Implementation **\: FICHIER **/
/* Traitement des fichiers ouverts */
struct _Noeud
{
FILE * Var_fich ;
char * Nom_fich ;
int Sauv_pos;
struct _Noeud *Suiv ;
} ;
typedef struct _Noeud * _Ptr_Noeud;
_Ptr_Noeud _Pile_ouverts = NULL;
/* Teste si un fichier est ouvert */
_Ptr_Noeud _Ouvert ( char * Fp)
{
_Ptr_Noeud P;
bool Trouv ;
P = _Pile_ouverts; Trouv = False ;
while ((P != NULL) && ! Trouv )
if ( strcmp(P->Nom_fich, Fp) == 0)
Trouv = True;
else P = P->Suiv;
return P;
}
/* Ajouter un fichier ouvert */
void _Empiler_ouvert ( char *Fp, FILE *Fl)
{
_Ptr_Noeud P ;
P = (_Ptr_Noeud) malloc( sizeof( struct _Noeud)) ;
P->Nom_fich = Fp;
P->Var_fich = Fl;
P->Suiv = _Pile_ouverts;
_Pile_ouverts = P;
}
/* Supprimer un fichier ouvert et rendre son nom*/
char * _Depiler_ouvert ( FILE *Fl)
{
char * Fp = malloc (100);
_Ptr_Noeud P, Prec ;
P= _Pile_ouverts;
Prec = NULL;
while (P->Var_fich != Fl )
{ Prec = P ; P = P->Suiv ;}
strcpy(Fp, P->Nom_fich);
if (Prec != NULL)
Prec->Suiv = P->Suiv;
else _Pile_ouverts = P->Suiv;
free (P);
return Fp ;
}
/** Fichiers **/
typedef char _Tx[255];
typedef string255 Typestruct1_s;
typedef _Tx Typestruct1_s_Buf;
/** Machine abstaite sur les fichiers **/
void Ouvrir_s ( FILE **s , char *Fp , char * Mode )
{
_Ptr_Noeud P = _Ouvert(Fp);
if ( P != NULL )
/* Le fichier est deja ouvert */
{
P->Sauv_pos = ftell (P->Var_fich);
fclose(P->Var_fich);
}
/* Le fichier est non ouvert */
if ( strcmp(Mode,"A") == 0)
*s = fopen(Fp, "r+b");
else
*s = fopen(Fp, "w+b");
_Empiler_ouvert( Fp, *s);
}
void Fermer_s ( FILE * s )
{
char * Fp = malloc(100);
_Ptr_Noeud P;
strcpy(Fp, _Depiler_ouvert(s));
fclose(s) ;
/* Ya-til un fichier ouvert avec le m?me nom ? */
/* Si Oui, le Reouvrir a la position sauvegardee */
P = _Ouvert (Fp);
if ( P != NULL)
{
s = fopen(P->Nom_fich, "r+b");
fseek(s, P->Sauv_pos, 0);
}
}
void Ecrireseq_s ( FILE * s, Typestruct1_s Buf )
{
Typestruct1_s_Buf Buffer ;
int I, J;
for(J=0; J<= strlen(Buf); ++J)
Buffer[J] = Buf [J];
fwrite(&Buffer, sizeof( Typestruct1_s_Buf), 1, s) ;
}
void Ecriredir_s ( FILE * s, Typestruct1_s Buf, int N )
{
Typestruct1_s_Buf Buffer ;
int I, J;
for(J=0; J<= strlen(Buf); ++J)
Buffer[J] = Buf [J];
fseek(s, (long) (N-1)* sizeof( Typestruct1_s_Buf), 0);
fwrite(&Buffer, sizeof( Typestruct1_s_Buf), 1, s) ;
}
void Lireseq_s ( FILE * s, Typestruct1_s Buf )
{
Typestruct1_s_Buf Buffer ;
int I, J;
if (fread(&Buffer, sizeof( Typestruct1_s_Buf), 1, s) != 0) {
for(J=0; J<= strlen(Buffer); ++J)
Buf [J] = Buffer[J] ;
}
}
void Liredir_s ( FILE * s, Typestruct1_s Buf, int N)
{
Typestruct1_s_Buf Buffer ;
int I, J;
fseek(s, (long) (N-1)* sizeof( Typestruct1_s_Buf), 0 );
fread(&Buffer, sizeof( Typestruct1_s_Buf), 1, s);
for(J=0; J<= strlen(Buffer); ++J)
Buf [J] = Buffer[J] ;
}
void Rajouter_s ( FILE * s, Typestruct1_s Buf )
{
Typestruct1_s_Buf Buffer ;
int I, J;
for(J=0; J<= strlen(Buf); ++J)
Buffer[J] = Buf [J];
fseek(s, 0, 2); /* Fin du fichier */
fwrite(&Buffer, sizeof( Typestruct1_s_Buf), 1, s) ;
}
bool Finfich_s (FILE * s)
{
long K = ftell(s);
fseek(s, 0, 2); /* Fin du fichier */
long K2 = ftell(s); /* position a partir du debut */
if (K==K2)
{ fseek(s, K, 0); return 1;}
else
{ fseek(s, K, 0); return 0;}
}
int Alloc_bloc_s (FILE * s)
{
long K;
fseek(s, 0, 2); /* Fin du fichier */
K = ftell(s); /* position a partir du debut */
K = K / sizeof (Typestruct1_s_Buf);
K ++;
return(K);
}
/** Implementation **\: ARBRE BINAIRE DE CHAINES DE CARACTERES**/
/** Arbres de recherche binaire **/
typedef string255 Typeelem_As ;
typedef struct Noeud_As * Pointeur_As ;
struct Noeud_As
{
Typeelem_As Val ;
Pointeur_As Fg ;
Pointeur_As Fd ;
Pointeur_As Pere ;
} ;
Typeelem_As Info_As( Pointeur_As P )
{ return P->Val; }
Pointeur_As Fg_As( Pointeur_As P)
{ return P->Fg ; }
Pointeur_As Fd_As( Pointeur_As P)
{ return P->Fd ; }
Pointeur_As Pere_As( Pointeur_As P)
{ return P->Pere ; }
void Aff_info_As ( Pointeur_As P, Typeelem_As Val)
{
strcpy( P->Val , Val );
}
void Aff_fg_As( Pointeur_As P, Pointeur_As Q)
{ P->Fg = Q; }
void Aff_fd_As( Pointeur_As P, Pointeur_As Q)
{ P->Fd = Q ; }
void Aff_pere_As( Pointeur_As P, Pointeur_As Q)
{ P->Pere = Q ; }
void Creernoeud_As( Pointeur_As *P)
{
*P = (struct Noeud_As *) malloc( sizeof( struct Noeud_As)) ;
(*P)->Val = malloc(255 * sizeof(string255));
(*P)->Fg = NULL;
(*P)->Fd = NULL;
(*P)->Pere = NULL;
}
void Liberernoeud_As( Pointeur_As P)
{ free( P ) ; }
/** Implementation **\: FILE DE ARBRES BINAIRES DE CHAINES DE CARACTERES**/
/** Files d'attente **/
typedef Pointeur_As Typeelem_FAs ;
typedef struct Filedattente_FAs * Pointeur_FAs ;
typedef struct Maillon_FAs * Ptliste_FAs ;
struct Maillon_FAs
{
Typeelem_FAs Val ;
Ptliste_FAs Suiv ;
};
struct Filedattente_FAs
{
Ptliste_FAs Tete, Queue ;
};
void Creerfile_FAs ( Pointeur_FAs *Fil )
{
*Fil = (struct Filedattente_FAs *) malloc( sizeof( struct Filedattente_FAs)) ;
(*Fil)->Tete = NULL ;
(*Fil)->Queue = NULL ;
}
bool Filevide_FAs (Pointeur_FAs Fil )
{ return Fil->Tete == NULL ;}
void Enfiler_FAs ( Pointeur_FAs Fil , Typeelem_FAs Val )
{
Ptliste_FAs Q;
Q = (struct Maillon_FAs *) malloc( sizeof( struct Maillon_FAs)) ;
Q->Val = Val ;
Q->Suiv = NULL;
if ( ! Filevide_FAs(Fil) )
Fil->Queue->Suiv = Q ;
else Fil->Tete = Q;
Fil->Queue = Q;
}
void Defiler_FAs (Pointeur_FAs Fil, Typeelem_FAs *Val )
{
if (! Filevide_FAs(Fil) )
{
*Val = Fil->Tete->Val ;
Fil->Tete = Fil->Tete->Suiv;
}
else printf ("%s \n", "File d'attente vide");
}
/** Variables du programme principal **/
FILE *F;
FILE *results_single;
FILE *results_range;
Typestruct1_s S ;
int I;
int N;
int Random;
int Singlesearchcounter;
int Rangesearchcounter;
int Choice;
string2 X;
string2 Y;
string2 Z;
bool Quit;
bool Numvalid;
string255 Word;
string255 Word1;
string255 Word2;
Pointeur_As Bst0=NULL;
Pointeur_As Bst1=NULL;
Pointeur_As Bst2=NULL;
Pointeur_As Bst3=NULL;
Pointeur_As Resultat=NULL;
string255 _Sx;
/** Fonctions standards **/
int Aleanombre( int N )
{ return ( rand() % N ); }
char *Aleachaine ( int N )
{
int k;
char * Chaine = malloc(N+1);
char Chr1[26] = "abcdefghijklmnopqrstuvwxyz";
char Chr2[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (k=0;k<N; k++)
switch ( rand() % 2 ){
case 0 : *(Chaine+k) = Chr1[rand() % 26] ; break ;
case 1 : *(Chaine+k) = Chr2[rand() % 26] ; break ;
}
Chaine[k] = '\0' ;
return (Chaine);
}
char *Caract ( string255 Ch , int I )
{
char *s = malloc(2);
s[0] = Ch[I-1];
s[1] = '\0';
return s;
}
int Max (int a, int b)
{
if (a > b) return(a);
else return(b);
}
/** Prototypes des fonctions **/
void Displaymenu (Pointeur_As *Bst0,Pointeur_As *Bst1 , Pointeur_As *Bst2 , Pointeur_As *Bst3 , string2 *X , string2 *Y , string2 *Z , bool *Quit , int *Choice , string255 *Word , Pointeur_As *Resultat , string255 *Word1 , string255 *Word2 , int *Singlesearchcounter , int *Rangesearchcounter);
int Cmp_string (string255 *Str1 , string255 *Str2) ;
void Choice1 (Pointeur_As *Bst1 , Pointeur_As *Bst2 , Pointeur_As *Bst3 , string2 *X , string2 *Y , string2 *Z);
Pointeur_As Insertbst (Pointeur_As *Root , string255 *Str) ;
bool Beginwithxyz (string255 *S , string2 *X , string2 *Y , string2 *Z) ;
void Leftrotation (Pointeur_As *Node);
void Rightrotation (Pointeur_As *Node);
void Constructbst1 (Pointeur_As *Bst1 , FILE *F , string2 *X , string2 *Y , string2 *Z);
void Constructbst2 (Pointeur_As *Bst2 , FILE *F , string2 *X , string2 *Y , string2 *Z);
void Constructbst3 (Pointeur_As *Bst3 , FILE *F , string2 *X , string2 *Y , string2 *Z);
void Inordertraversal (Pointeur_As *Bst);
int Branchlength (Pointeur_As *Bst , Pointeur_As *Node) ;
int Startwithxyzcount (Pointeur_As *Bst , string2 *X , string2 *Y , string2 *Z) ;
int Startwithcharcount (Pointeur_As *Bst , string2 *Character) ;
int Treedepth (Pointeur_As *Bst) ;
void Nodesxyzlevel (Pointeur_As *Bst , string2 *X , string2 *Y , string2 *Z);
void Queuelengthxyzcount (Pointeur_FAs *Queue1 , int *Length , int *Countxyz);
Pointeur_As Singlewordsearch (Pointeur_As *Bst1 , Pointeur_As *Bst2 , Pointeur_As *Bst3 , string2 *X , string2 *Y , string2 *Z , string255 *String , int *Singlesearchcounter) ;
Pointeur_As Searchbst1 (Pointeur_As *Bst , string2 *X , string2 *Y , string2 *Z , string255 *String , int *Singlesearchcounter) ;
Pointeur_As Searchbst2 (Pointeur_As *Bst , string2 *X , string2 *Y , string2 *Z , string255 *String , int *Singlesearchcounter) ;
Pointeur_As Searchbst3 (Pointeur_As *Bst , string2 *X , string2 *Y , string2 *Z , string255 *String , int *Singlesearchcounter) ;
void Rangesearch (Pointeur_As *Bst1 , Pointeur_As *Bst2 , Pointeur_As *Bst3 , string2 *X , string2 *Y , string2 *Z , string255 *Word1 , string255 *Word2 , int *Rangesearchcounter);
void Rangesearchbst (Pointeur_As *Bst , string255 *Word1 , string255 *Word2 , int *Rangesearchcounter);
Pointeur_As Searchleastgreater (Pointeur_As *Bst , string255 *Word1 , int *Rangesearchcounter) ;
Pointeur_As Nextinorder (Pointeur_As *Node , int *Rangesearchcounter) ;
void Constructbst0 (Pointeur_As *Bst0 , FILE *F , string2 *X , string2 *Y , string2 *Z);
void Constructandtestbst0(Pointeur_As *Bst0 , FILE *F , string2 *X , string2 *Y , string2 *Z);
/*////////////////////////////////////////////////////////////////////////*/
void Displaymenu (Pointeur_As *Bst0,Pointeur_As *Bst1 , Pointeur_As *Bst2 , Pointeur_As *Bst3 , string2 *X , string2 *Y , string2 *Z , bool *Quit , int *Choice , string255 *Word , Pointeur_As *Resultat , string255 *Word1 , string255 *Word2 , int *Singlesearchcounter , int *Rangesearchcounter)
{
/** Corps du module **/
while( ( ! *Quit )) {
system("cls");
printf("\x1b[31m __ __ \n");
printf(" | \\/ | ___ _ __ _ _ \n");
printf(" | |\\/| |/ _ \\ '_ \\| | | |\n");
printf(" | | | | __/ | | | |_| |\n");
printf(" |_| |_|\\___|_| |_|\\____|\x1b[0m\n");
printf ( " %s", "\x1b[35m _____________________________________________________________________________________\n" ) ;
printf ( " %s", "|\x1b[33m1-\x1b[0mcount the number of words starting with x/y/z \x1b[35m|\n" ) ;
printf ( " %s", "|\x1b[33m2-\x1b[0mcompute the depth of the trees \x1b[35m|\n" ) ;
printf ( " %s", "|\x1b[33m3-\x1b[0mperform an inorder traversal \x1b[35m|\n" ) ;
printf ( " %s", "|\x1b[33m4-\x1b[0mcompute and display the number of nodes starting with x/y/z in each level \x1b[35m|\n" ) ;
printf ( " %s", "|\x1b[33m5-\x1b[0msearch for a single word \x1b[35m|\n" ) ;
printf ( " %s", "|\x1b[33m6-\x1b[0msearch for all words within a particular range \x1b[35m|\n" ) ;
printf ( " %s", "|\x1b[33m7-\x1b[0mbuild and test bst0 \x1b[35m|\n" ) ;
printf ( " %s", "|\x1b[33m8-\x1b[0msimulation algorithm for single word search efficiency \x1b[35m|\n" ) ;
printf ( " %s", "|\x1b[33m9-\x1b[0msimulation algorithm for range search efficiency \x1b[35m|\n" ) ;
printf ( " %s", "|\x1b[33m10-\x1b[0mexit \x1b[35m|\n" ) ;
printf ( " %s", "|_____________________________________________________________________________________|\n" ) ;
printf ( " %s", "\x1b[32mplease enter your choice:" );
scanf ( " %d", &*Choice ) ;
if( *Choice == 1) {
system("cls");
Choice1 ( & *Bst1 , & *Bst2 , & *Bst3 , & *X , & *Y , & *Z ) ;
system("pause");
}
else
{
if( *Choice == 2) {
system("cls");
printf ( " %s", "\x1b[35m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\n" ) ;
printf ( " %s", "\x1b[0mdepth of the trees:\n" ) ;
printf ( " %s", "bst1:" );
printf ( "\x1b[32m %d \x1b[0m\n", Treedepth(&*Bst1) ) ;
printf ( " %s", "bst2:" );
printf ( "\x1b[32m %d \x1b[0m \n", Treedepth(&*Bst2) ) ;
printf ( " %s", "bst3:" );
printf ( "\x1b[32m %d \x1b[0m \n", Treedepth(&*Bst3) ) ;
printf ( " %s", "\x1b[35m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[0m\n" ) ;
system("pause");
}
else
{
if( *Choice == 3) {
system("cls");
printf ( " %s", "\x1b[35m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[32m\n" ) ;
printf ( " %s", "inorder traversal:\x1b[0m\n" ) ;
printf ( " %s", "\x1b[35m--------------------------------\x1b[0m \n" ) ;
printf ( " %s", "bst1:\n" ) ;
Inordertraversal ( & *Bst1 ) ;
printf ( " %s", "\x1b[35m--------------------------------\x1b[0m \n" ) ;
printf ( " %s", "bst2:\n" ) ;
Inordertraversal ( & *Bst2 ) ;
printf ( " %s", "\x1b[35m--------------------------------\x1b[0m \n" ) ;
printf ( " %s", "bst3:\n" ) ;
Inordertraversal ( & *Bst3 ) ;
printf ( " %s", "-\x1b[35m-------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[0m \n" ) ;
system("pause");
}
else
{
if( *Choice == 4) {
system("cls");
printf ( " %s", "\x1b[35m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[32m \n" ) ;
printf ( " %s", "the number of nodes starting with x/y/z in each level:\x1b[0m\n" ) ;
printf ( " %s", "bst1:\n" ) ;
Nodesxyzlevel ( & *Bst1 , & *X , & *Y , & *Z ) ;
printf ( " %s", "\x1b[35m--------------------------------\x1b[0m\n" ) ;
printf ( " %s", "bst2:\n" ) ;
Nodesxyzlevel ( & *Bst2 , & *X , & *Y , & *Z ) ;
printf ( " %s", "\x1b[35m--------------------------------\x1b[0m\n" ) ;
printf ( " %s", "bst3:\n" ) ;
Nodesxyzlevel ( & *Bst3 , & *X , & *Y , & *Z ) ;
printf ( " %s", "\x1b[35m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[0m\n" ) ;
system("pause");
}
else
{
if( *Choice == 5) {
system("cls");
printf ( " %s", "\x1b[35m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[33m\n" ) ;
printf ( " %s", "search for a word:\x1b[0m" ) ;
scanf ( " %[^\n]", *Word ) ;
*Resultat = Singlewordsearch ( & *Bst1 , & *Bst2 , & *Bst3 , & *X , & *Y , & *Z , & *Word , & *Singlesearchcounter ) ;
if( *Resultat != NULL) {
printf ( " %s", "\x1b[32m----------\n" ) ;
printf ( " %s", "exists\n" ) ;
printf ( " %s", "----------\x1b[0m\n" ) ;
}
else
{
printf ( " %s", "\x1b[31m-----------------\n" ) ;
printf ( " %s", "doesnt exist\n" ) ;
printf ( " %s", "-----------------\x1b[0m\n" ) ;
} ;
printf ( " %s", "\x1b[35m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[0m\n" ) ;
system("pause");
}
else
{
if( *Choice == 6) {
system("cls");
printf ( " %s", "\x1b[35m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[32m\n" ) ;
printf ( " %s", "Range Search [word1,word2]: \x1b[0m \n" ) ;
printf ( " %s", "enter word1:\n" ) ;
scanf ( " %[^\n]", *Word1 ) ;
printf ( " %s", "enter word2:\n" ) ;
scanf ( " %[^\n]", *Word2 ) ;
printf ( " %s", "\x1b[35m-------------------------------\x1b[0m\n" ) ;
Rangesearch ( & *Bst1 , & *Bst2 , & *Bst3 , & *X , & *Y , & *Z , & *Word1 , & *Word2 , & *Rangesearchcounter ) ;
printf ( " %s", "\x1b[35m--------------------------------\x1b[0m\n" ) ;
system("pause");
}
else
{
if( *Choice == 7) {
Constructandtestbst0(& *Bst0 , & F , & X , & Y , & Z);
}
else
{
if( *Choice == 8){
system("cls");
printf("Haven't been constructed\n");
system("pause");
}
else{
if( *Choice == 9){
system("cls");
printf("Haven't been constructed\n");
system("pause");
}
else{
if( *Choice == 10){
*Quit = True ;
printf ( " \n%s", "\x1b[36m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\n" ) ;
printf ( " %s", "see you next time!\n" ) ;
printf ( " %s", "--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[0m\n" ) ;
}
else{
printf ( "\n%s", "\x1b[31m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\n" ) ;
printf ( " %s", "enter a valid choice!\n" ) ;
printf ( " %s", "--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[0m\n" ) ;
};
};
};
} ;
} ;
} ;
} ;
} ;
} ;
} ;
} ;
}
/*////////////////////////////////////////////////////////////////////////*/
int Cmp_string (string255 *Str1 , string255 *Str2)
{
/** Variables locales **/
int Cmp_string2 ;
/** Corps du module **/
if( (strcmp( *Str1, *Str2) > 0 )) {
Cmp_string2 = 1 ;
}
else
{
if( (strcmp( *Str1, *Str2) == 0 )) {
Cmp_string2 = 0 ;
}
else
{
Cmp_string2 = - 1 ;
} ;
} ;
return Cmp_string2 ;
}
/*////////////////////////////////////////////////////////////////////////*/
void Choice1 (Pointeur_As *Bst1 , Pointeur_As *Bst2 , Pointeur_As *Bst3 , string2 *X , string2 *Y , string2 *Z)
{
/** Corps du module **/
system("cls");
printf ( " %s", "\x1b[35m--------------------------------\n" ) ;
printf ( " %s", "--------------------------------\x1b[32m\n" ) ;
printf ( " %s", "number of words starting with x/y/z:\x1b[0m\n" ) ;
printf ( " %s", "bst1:" );
printf ( " %d", Startwithxyzcount(&*Bst1,&*X,&*Y,&*Z) ) ;
printf ( "\n %s", "start with" );
printf ( " %s", *X );
printf ( " %s", ":" );
printf ( " %d", Startwithcharcount(&*Bst1,&*X) ) ;
printf ( "\n %s", "start with" );
printf ( " %s", *Y );
printf ( " %s", ":" );
printf ( " %d", Startwithcharcount(&*Bst1,&*Y) ) ;
printf ( "\n %s", "start with" );
printf ( " %s", *Z );
printf ( " %s", ":" );
printf ( " %d", Startwithcharcount(&*Bst1,&*Z) ) ;
printf ( "\n %s", "bst2:" );
printf ( " %d", Startwithxyzcount(&*Bst2,&*X,&*Y,&*Z) ) ;
printf ( "\n %s", "start with" );
printf ( " %s", *X );
printf ( " %s", ":" );
printf ( " %d", Startwithcharcount(&*Bst2,&*X) ) ;
printf ( "\n %s", "start with" );
printf ( " %s", *Y );
printf ( " %s", ":" );
printf ( " %d", Startwithcharcount(&*Bst2,&*Y) ) ;
printf ( "\n %s", "start with" );
printf ( " %s", *Z );
printf ( " %s", ":" );
printf ( " %d", Startwithcharcount(&*Bst2,&*Z) ) ;
printf ( "\n %s", "bst3:" );
printf ( " %d", Startwithxyzcount(&*Bst3,&*X,&*Y,&*Z) ) ;
printf ( "\n %s", "start with" );
printf ( " %s", *X );
printf ( " %s", ":" );
printf ( " %d", Startwithcharcount(&*Bst3,&*X) ) ;
printf ( "\n %s", "start with" );
printf ( " %s", *Y );
printf ( " %s", ":" );
printf ( " %d", Startwithcharcount(&*Bst3,&*Y) ) ;
printf ( "\n %s", "start with" );
printf ( " %s", *Z );
printf ( " %s", ":" );
printf ( " %d", Startwithcharcount(&*Bst3,&*Z) ) ;
printf ( "\n %s", "\x1b[35m--------------------------------" ) ;
printf ( "\n %s", "--------------------------------\x1b[0m\n" ) ;
}
/*////////////////////////////////////////////////////////////////////////*/
Pointeur_As Insertbst (Pointeur_As *Root , string255 *Str)
{
/** Variables locales **/
Pointeur_As Insertbst2 =NULL;
Pointeur_As Temp=NULL;
Pointeur_As P=NULL;
Pointeur_As Paren=NULL;
string255 _Px1;
string255 _Px2;
/** Corps du module **/
_Px1 = malloc(255 * sizeof(char));
_Px2 = malloc(255 * sizeof(char));
Creernoeud_As (& P ) ;
Aff_info_As ( P , *Str ) ;
if( ( *Root == NULL )) {
*Root = P ;
}
else
{
Temp = *Root ;
Paren = NULL ;
while( ( Temp != NULL )) {
Paren = Temp ;
strcpy(_Px1, Info_As ( Temp ) );
if( ( Cmp_string ( &_Px1, & *Str ) == 1 )) {
;
Temp = Fg_As ( Temp ) ;
}
else
{
Temp = Fd_As ( Temp ) ;
} ;
} ;
strcpy(_Px2, Info_As ( Paren ) );
if( Cmp_string ( &_Px2, & *Str ) == 1) {
Aff_fg_As ( Paren , P ) ;
Aff_pere_As ( P , Paren ) ;
}
else
{
Aff_fd_As ( Paren , P ) ;
Aff_pere_As ( P , Paren ) ;
} ;
} ;
Insertbst2 = P ;
return Insertbst2 ;
}
/*////////////////////////////////////////////////////////////////////////*/
bool Beginwithxyz (string255 *S , string2 *X , string2 *Y , string2 *Z)
{
/** Variables locales **/
bool Beginwithxyz2 ;
bool Result;
/** Corps du module **/
Result = (strcmp( Caract ( *S , 1 ), *X) == 0 ) || (strcmp( Caract ( *S , 1 ), *Y) == 0 ) || (strcmp( Caract ( *S , 1 ), *Z) == 0 ) ;
Beginwithxyz2 = Result ;
return Beginwithxyz2 ;
}
/*////////////////////////////////////////////////////////////////////////*/
void Leftrotation (Pointeur_As *Node)
{
/** Variables locales **/
Pointeur_As Temp=NULL;
Pointeur_As Parent=NULL;
/** Corps du module **/
Parent = Pere_As ( *Node ) ;
Temp = Fd_As ( *Node ) ;
Aff_fd_As ( *Node , Fg_As ( Temp ) ) ;
if( Fg_As ( Temp ) != NULL) {
Aff_pere_As ( Fg_As ( Temp ) , *Node );
} ;
Aff_fg_As ( Temp , *Node ) ;
Aff_pere_As ( *Node , Temp ) ;
Aff_pere_As ( Temp , Parent ) ;
if( Parent != NULL) {
if( Fg_As ( Parent ) == *Node) {
Aff_fg_As ( Parent , Temp ) ;
}
else
{
Aff_fd_As ( Parent , Temp ) ;
} ;
} ;
}
/*////////////////////////////////////////////////////////////////////////*/
void Rightrotation (Pointeur_As *Node)
{
/** Variables locales **/
Pointeur_As Temp=NULL;
Pointeur_As Parent=NULL;
/** Corps du module **/
Parent = Pere_As ( *Node ) ;
Temp = Fg_As ( *Node ) ;
Aff_fg_As ( *Node , Fd_As ( Temp ) ) ;
if( Fd_As ( Temp ) != NULL) {
Aff_pere_As ( Fd_As ( Temp ) , *Node ) ;
} ;
Aff_fd_As ( Temp , *Node ) ;
Aff_pere_As ( *Node , Temp ) ;
Aff_pere_As ( Temp , Parent ) ;
if( Parent != NULL) {
if( Fg_As ( Parent ) == *Node) {
Aff_fg_As ( Parent , Temp ) ;
}
else
{
Aff_fd_As ( Parent , Temp ) ;
} ;
} ;
}
/*////////////////////////////////////////////////////////////////////////*/
void Constructbst1 (Pointeur_As *Bst1 , FILE *F , string2 *X , string2 *Y , string2 *Z)
{
/** Variables locales **/
Typestruct1_s S ;
Pointeur_As Bst=NULL;
Pointeur_As Node=NULL;
Pointeur_As Current=NULL;
/** Corps du module **/
S = malloc(255 * sizeof(char));
Ouvrir_s (&F , "F2.z" , "A" ) ;
while( ( ! Finfich_s ( F ) )) {
Lireseq_s ( F , S ) ;
Current = Insertbst ( & *Bst1 , & S ) ;
if( ( Beginwithxyz ( & S , & *X , & *Y , & *Z ) )) {
Node = Pere_As ( Current ) ;
while( ( Node != NULL )) {
if( Fg_As ( Node ) == Current) {
Rightrotation ( & Node ) ;
}
else
{
Leftrotation ( & Node ) ;
} ;
Node = Pere_As ( Current ) ;
} ;
*Bst1 = Current ;
} ;
} ;
Fermer_s ( F ) ;
}
/*////////////////////////////////////////////////////////////////////////*/
void Constructbst2 (Pointeur_As *Bst2 , FILE *F , string2 *X , string2 *Y , string2 *Z)
{
/** Variables locales **/
Typestruct1_s S ;
int J;
int Length;
Pointeur_As Bst=NULL;
Pointeur_As Node=NULL;
Pointeur_As Current=NULL;
/** Corps du module **/
S = malloc(255 * sizeof(char));
Ouvrir_s (&F , "F2.z" , "A" ) ;
while( ( ! Finfich_s ( F ) )) {
Lireseq_s ( F , S ) ;
Current = Insertbst ( & *Bst2 , & S ) ;
if( ( Beginwithxyz ( & S , & *X , & *Y , & *Z ) )) {
Node = Pere_As ( Current ) ;
Length = Branchlength ( & *Bst2 , & Current ) / 2 ;
for( J = 1 ;J <= Length ; ++J){
if( Fg_As ( Node ) == Current) {
Rightrotation ( & Node ) ;
}
else
{
Leftrotation ( & Node ) ;
} ;
Node = Pere_As ( Current ) ;
} ;
} ;
} ;
Fermer_s ( F ) ;
}
/*////////////////////////////////////////////////////////////////////////*/
void Constructbst3 (Pointeur_As *Bst3 , FILE *F , string2 *X , string2 *Y , string2 *Z)
{
/** Variables locales **/
Typestruct1_s S ;
Pointeur_As Node=NULL;
Pointeur_As Current=NULL;
/** Corps du module **/
S = malloc(255 * sizeof(char));
Ouvrir_s (&F , "F2.z" , "A" ) ;
while( ( ! Finfich_s ( F ) )) {
Lireseq_s ( F , S ) ;
Current = Insertbst ( & *Bst3 , & S ) ;
if( ( ! ( Beginwithxyz ( & S , & *X , & *Y , & *Z ) ) )) {
Node = Pere_As ( Current ) ;
while( ( Node != NULL )) {
if( Fg_As ( Node ) == Current) {
Rightrotation ( & Node ) ;
}
else
{
Leftrotation ( & Node ) ;
} ;
Node = Pere_As ( Current ) ;
} ;
*Bst3 = Current ;
} ;
} ;
Fermer_s ( F ) ;
}
/*////////////////////////////////////////////////////////////////////////*/
void Inordertraversal (Pointeur_As *Bst)
{
/** Variables locales **/
Pointeur_As _Px1=NULL;
Pointeur_As _Px2=NULL;
/** Corps du module **/
if( ( *Bst != NULL )) {
_Px1 = Fg_As ( *Bst ) ;
Inordertraversal ( &_Px1) ;
printf ( " %s", Info_As(*Bst) );
printf ( " %s\n", " " ) ;
_Px2 = Fd_As ( *Bst ) ;
Inordertraversal ( &_Px2) ;
} ;
}
/*////////////////////////////////////////////////////////////////////////*/
int Branchlength (Pointeur_As *Bst , Pointeur_As *Node)
{
/** Variables locales **/
int Branchlength2 ;
Pointeur_As Temp=NULL;
int Result;
/** Corps du module **/
Result = 0 ;
Temp = *Node ;
while( Temp != *Bst) {
Result = Result + 1 ;
Temp = Pere_As ( Temp ) ;
} ;
Branchlength2 = Result ;
return Branchlength2 ;
}
/*////////////////////////////////////////////////////////////////////////*/
int Startwithxyzcount (Pointeur_As *Bst , string2 *X , string2 *Y , string2 *Z)
{
/** Variables locales **/
int Startwithxyzcount2 ;
string255 _Px1;
Pointeur_As _Px2=NULL;
Pointeur_As _Px3=NULL;
Pointeur_As _Px4=NULL;
Pointeur_As _Px5=NULL;
/** Corps du module **/
_Px1 = malloc(255 * sizeof(char));
if( *Bst == NULL) {
Startwithxyzcount2 = 0 ;
}
else
{
strcpy(_Px1, Info_As ( *Bst ) );
if( Beginwithxyz ( &_Px1, & *X , & *Y , & *Z )) {
_Px2 = Fg_As ( *Bst ) ;
_Px3 = Fd_As ( *Bst ) ;
Startwithxyzcount2 = 1 + Startwithxyzcount ( &_Px2, & *X , & *Y , & *Z ) + Startwithxyzcount ( &_Px3, & *X , & *Y , & *Z ) ;
}
else
{
_Px4 = Fg_As ( *Bst ) ;
_Px5 = Fd_As ( *Bst ) ;
Startwithxyzcount2 = Startwithxyzcount ( &_Px4, & *X , & *Y , & *Z ) + Startwithxyzcount ( &_Px5, & *X , & *Y , & *Z ) ;
} ;
} ;
return Startwithxyzcount2 ;
}
/*////////////////////////////////////////////////////////////////////////*/
int Startwithcharcount (Pointeur_As *Bst , string2 *Character)
{
/** Variables locales **/
int Startwithcharcount2 ;
Pointeur_As _Px1=NULL;
Pointeur_As _Px2=NULL;
Pointeur_As _Px3=NULL;