-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
1081 lines (787 loc) · 29.5 KB
/
functions.c
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 "Header.h"
void buildBinaryTreeFromFile(char* filename, InstrumentTree *tr) {
char* instrument;
int size,flage;
unsigned short id=0;
flage = 0;
FILE* fp;
fp = fopen(filename, "r");
chekeIfOpen(fp);
instrument=getTheInstrument(fp);
tr->root = creatTreeNode(instrument, &id, NULL, NULL);
while (instrument != NULL) {
instrument=getTheInstrument(fp);
helperBuild(tr->root, instrument,&flage,&id);
flage = 0;
}
//after building the tree can left and free the instrumet
free(instrument);
fclose(fp);
}
TreeNode* creatTreeNode(char *instrument, unsigned short *insID, TreeNode *left, TreeNode *right) {
int size;
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
chekeAlloction(newNode);
newNode->insID = *insID;
size = strlen(instrument);
newNode->instrument = (char*)malloc(size+1);
chekeAlloction(newNode->instrument);
strcpy(newNode->instrument, instrument);
newNode->instrument[size + 1] = '\0';
newNode->left = left;
newNode->right = right;
(*insID)++;
return newNode;
}
//FILE with names- dont forget to add '\0' to the end of the name
// and use the fscanf function for flage
//when the function success to scan on name return 1 if he dont success to scan a name so he return -1
char* getTheInstrument(FILE* fp) {
char* instrument = (char*)malloc(sizeof(char)* MAX_INSTRUMENT_NAME);
chekeAlloction(instrument);
int flage,sizeOfname;
flage=fscanf(fp, "%s", instrument);
if ( flage == -1 ){
return NULL;
}
sizeOfname = strlen(instrument);
instrument[sizeOfname+1] = '\0';
instrument = (char*)realloc(instrument, (sizeOfname+1));
chekeAlloction(instrument);
return instrument;
}
void chekeIfOpen(FILE *fp) {
if (fp == NULL) {
printf("the file no success to open");
}
}
void chekeAlloction(void* ptr) {
if (ptr == NULL) {
printf("the allocation is failed.");
exit(-1);
}
}
void helperBuild(TreeNode *root,char* instrument,int *flage,int *id) {
int numCompare;
if (root == NULL || instrument == NULL) {
return;
}
else {
numCompare = strcmp(instrument, root->instrument);
if (numCompare >0) {
helperBuild(root->right, instrument, flage,id);
if (*flage == 0) {
root->right = creatTreeNode(instrument, id, NULL, NULL);
*flage = 1;
}
}else if (numCompare < 0) {
helperBuild(root->left, instrument, flage, id);
if (*flage == 0) {
root->left = creatTreeNode(instrument, id, NULL, NULL);
*flage = 1;
}
}
else {
return;
}
}
}
void freeTree(InstrumentTree *tr) {
FreeTreehelper(tr->root);
}
void FreeTreehelper(TreeNode* root) {
if (root == NULL) {
return;
}
else {
FreeTreehelper(root->left);
FreeTreehelper(root->right);
// free(root->instrument);
free(root);
}
}
//purpose: search the ID of an instrument
//if not find return NULL if find return the ID
int findInsId(InstrumentTree tree, char* instrument){
int flage, id;
flage = 0;
helperFindInsId(tree.root, instrument,&flage,&id);
if (flage == 0) {
return NOT_FOUND;
}else{
return id;
}
}
void helperFindInsId(TreeNode *root,char* instrument,int *flage,int *id) {
if (root == NULL || *flage == 1) {
return;
}
else {
helperFindInsId(root->left, instrument, flage,id);
helperFindInsId(root->right, instrument, flage,id);
if (strcmp(instrument, root->instrument) == 0) {
*flage = 1;
*id = root->insID;
}
}
}
//go over the file
//for every line save the data and count how many i have singer.
//the amount will be the size of my array of musician
void saveMusicianFromFile(char* filename, InstrumentTree* tr) {
FILE* fp;
fp = fopen(filename, "r");
chekeIfOpen(fp);
char line[150];
char* ptr;
int psize, lsize, lsizeForMusicianArray, psizeForMusicianArray, lsizeName = 0;
char** name;
lsizeForMusicianArray = 0;
psizeForMusicianArray = 2;
Musician** musicianGroup;
musicianGroup = (Musician**)malloc(sizeof(Musician*) * psizeForMusicianArray);
chekeAlloction(musicianGroup);
ptr = fgets(line, 150, fp);
//this while run on every line in the file per one line every time
while (ptr != NULL) {
if (lsizeForMusicianArray == psizeForMusicianArray) {
psizeForMusicianArray = psizeForMusicianArray * 2;
musicianGroup = (Musician**)realloc(musicianGroup, psizeForMusicianArray * (sizeof(Musician*)));
chekeAlloction(musicianGroup);
}
musicianGroup[lsizeForMusicianArray] = (Musician*)malloc(sizeof(Musician));
chekeAlloction(musicianGroup[lsizeForMusicianArray]);
makeEmptyList(&(musicianGroup[lsizeForMusicianArray]->instrument));
name = buildNameAndList(&musicianGroup, tr, lsizeForMusicianArray, line, &lsizeName);
musicianGroup[lsizeForMusicianArray]->name = name;
musicianGroup[lsizeForMusicianArray]->chozen = 0;
musicianGroup[lsizeForMusicianArray]->lenghtName = lsizeName;
lsizeName = 0;
lsizeForMusicianArray++;
ptr = fgets(line, 150, fp);
}
fclose(fp);
buildArrayOfMusicianCollection(musicianGroup, tr, lsizeForMusicianArray);
}
////////////////////////////////////////////////////////////////////////////////////////////
/////////// FUNCTION HELP MPILIST : LINKED LIST (MAKE EMPTY-INSERTNODE-CREAT NODE)//////////
void makeEmptyList(MPIList* lst) {
lst->head = lst->tail = NULL;
}
void insertNodeToTail(MPIList* lst, ListNode* cur) {
if (lst->head==NULL&&lst->tail == NULL) {
lst->head = lst->tail = cur;
}
else {
cur->next = NULL;
lst->tail->next = cur;
lst->tail = cur;
}
}
void creatNodeAndInsertToTail(MPIList* lst,ListNode *next, unsigned short insID,float price) {
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
chekeAlloction(newNode);
newNode->next = next;
newNode->mpi.insID = insID;
newNode->mpi.price = price;
insertNodeToTail(lst, newNode);
}
//******************************************************************************************************
//******************************************************************************************************
char **buildNameAndList(Musician ***musicianGroup,InstrumentTree *tr,int lsizeForMusicianArray,char *line,int *lsizeName) {
int id,thisIsaName,lsize,psize,nameLenght,saveID;
char* specialChar = " ,.;:?!-\t'()[]{}<>~_\n";
char* token;
thisIsaName = 0;
psize = 2;
char** name;
name = (char**)malloc(sizeof(char*) * psize);
/* get the first token */
token = strtok(line, specialChar);
/* walk through other tokens */
while (token != NULL) {
//get the id of the different instruments
id = findInsId(*tr, token);
//the name is in the begine so every token at the begining is a part of the name
//ang when i arrive to a instrument i change the fkage beacaus
//from now the token that are without id number is a *number*!!
if (id == -1 && thisIsaName == 0) {
if (*lsizeName == psize) {
psize = psize * 2;
name = (char**)realloc(name, psize * (sizeof(char*)));
chekeAlloction(name);
}
nameLenght = strlen(token);
name[*lsizeName] = (char*)malloc(sizeof(char) * (nameLenght + 1));
strcpy(name[*lsizeName], token);
name[*lsizeName][nameLenght + 1] = '\0';
(*lsizeName)++;
}else {
thisIsaName = 1;
if (id == -1 && token != "\n") {
creatNodeAndInsertToTail(&((*musicianGroup)[lsizeForMusicianArray]->instrument), NULL, saveID, atoi(token));
}
saveID = id;
}
//strtok continue on the same line if you put NULL
token = strtok(NULL, specialChar);
}
return name;
}
//the amount of musician describe the size of the array group
//this function will over on the array of instrument and add
//every musician to the appropriate place
void buildArrayOfMusicianCollection(Musician** musicianGroup, InstrumentTree* tr, int size) {
int numOfInstrument = getNumOfInstrument(tr);
int* arrOfSize = (int*)malloc(sizeof(int) * numOfInstrument);
Musician*** musicianCollection = (Musician***)malloc(sizeof(Musician**) * numOfInstrument);
chekeAlloction(arrOfSize);
chekeAlloction(musicianCollection);
int id;
for (id = 0; id < numOfInstrument; id++) {
musicianCollection[id] = getArrayOfMusicianWhoPlayTheSameInstrument(musicianGroup, id, size,arrOfSize);
}
getConcertFromTheUserAndShowConcert(*tr,musicianCollection,numOfInstrument,arrOfSize);
}
//purpose: get the num of instrument in all the file
//because my tree compose all the instrument i can run over the tree
//the amount of nodes in the tree is the amount of instrument.
int getNumOfInstrument(InstrumentTree* tr) {
int counter = 0;
helperGetNumsOfInstrument(tr->root, &counter);
return counter;
}
void helperGetNumsOfInstrument(TreeNode *root,int *counter) {
if (root == NULL) {
return;
}
else {
helperGetNumsOfInstrument(root->left,counter);
helperGetNumsOfInstrument(root->right, counter);
(*counter)++;
}
}
//3 different argument
//1-the array of musician group
//2- the id who i search for choose the singer who use this instrument(id)
// the size of the array of musician group
//return an array of pointer to musician with the same instrument
Musician** getArrayOfMusicianWhoPlayTheSameInstrument(Musician **musicianGroup, int id,int size,int *arrofsize) {
int lsizeForMusicianArray, psizeForMusicianArray,i;
lsizeForMusicianArray = 0;
psizeForMusicianArray = 2;
Musician** musicianGroupofTheSameInstrument;
musicianGroupofTheSameInstrument= (Musician**)malloc(sizeof(Musician*) * psizeForMusicianArray);
chekeAlloction(musicianGroupofTheSameInstrument);
//loop that run one the musician group for search the ID on the array
//every one who have this ID one this array so a get him for my new array
for (i = 0; i < size; i++) {
Musician* tmp;
if (lsizeForMusicianArray == psizeForMusicianArray) {
psizeForMusicianArray = psizeForMusicianArray * 2;
musicianGroupofTheSameInstrument = (Musician**)realloc(musicianGroupofTheSameInstrument, psizeForMusicianArray * (sizeof(Musician*)));
chekeAlloction(musicianGroupofTheSameInstrument);
}
tmp=getTheMusicianWhoPlayTheSameInstrument(musicianGroup, i,id);
//if i found a instrument so add to array and add one place else do nothing
if (tmp != NULL) {
musicianGroupofTheSameInstrument[lsizeForMusicianArray] = tmp;
lsizeForMusicianArray++;
}
}
musicianGroupofTheSameInstrument = (Musician**)realloc(musicianGroupofTheSameInstrument, lsizeForMusicianArray * (sizeof(Musician*)));
chekeAlloction(musicianGroupofTheSameInstrument);
arrofsize[id] = lsizeForMusicianArray;
return musicianGroupofTheSameInstrument;
}
// search for me on the array in place index over the list if the ID exist
//if yes send me back this musician
Musician* getTheMusicianWhoPlayTheSameInstrument(Musician** musicianGroup,int index, int id) {
MPIList lst = musicianGroup[index]->instrument;
ListNode* cur = lst.head;
Musician *musicianGroupofTheSameInstrument = (Musician*)malloc(sizeof(Musician));
chekeAlloction(musicianGroupofTheSameInstrument);
while (cur != NULL) {
if (cur->mpi.insID == id) {
musicianGroupofTheSameInstrument = musicianGroup[index];
return musicianGroupofTheSameInstrument;
}
cur = cur->next;
}
//free the data because he dont find the ID in the list
free(musicianGroupofTheSameInstrument);
return NULL;
}
//arrofsize-this is an array that save my size of every array
//example - arrofsize[0] save the size of array musician in place 0 of array collection
void getConcertFromTheUserAndShowConcert(InstrumentTree tr, Musician*** musicianCollection, int numOfInstrument, int* arrOfSize) {
int lenghtLine, saveSizeArray = 0, sizeArray = 0;
int* arrOfsizeOfMusicianWhoPlay = NULL;
char* str, * name;
IDName* arrInstrument = NULL;
Date* date;
date = (Date*)malloc(sizeof(Date));
chekeAlloction(date);
str = getStringe();
if (str != NULL) {
lenghtLine = strlen(str);
CIList lst;
Musician*** allMusicianWhoPlayInConcert = NULL;
while (str != NULL) {
lenghtLine = strlen(str);
name = (char*)malloc(sizeof(char) * lenghtLine);
chekeAlloction(name);
sscanf(str, "%s %d %d %d %f:%f", name, &date->day, &date->month, &date->year, &date->hour, &date->min);
lenghtLine = strlen(name);
name = (char*)realloc(name, lenghtLine + 1);
chekeAlloction(name);
name[lenghtLine + 1] = '\0';
arrInstrument = getListOfInstrumentInTheConcert(str, tr, &lst);
arrangeArrayAppropriateToTheImportance(lst, musicianCollection, arrOfSize);
//array whit number of every musician i need to a concert for veri instrument
arrOfsizeOfMusicianWhoPlay = getArrOfSizeOfEvryInstrument(lst);
allMusicianWhoPlayInConcert = arrangeMusicianToConcert(lst, musicianCollection, arrOfSize, &sizeArray, arrOfsizeOfMusicianWhoPlay);
if (allMusicianWhoPlayInConcert == NULL) {
printError(name);
}
else {
//allmusicianwhoplayinconcert- this is the new array collection whit the musician that playing in the concert
//sizearray- is my size of this array
printTheConcert(allMusicianWhoPlayInConcert, sizeArray, name, date, arrOfsizeOfMusicianWhoPlay, arrInstrument);
}
saveSizeArray = sizeArray;
sizeArray = 0;
free(str);
freeCIlist(lst);
//free the arr IDName structur
freeARRIDName(arrInstrument, saveSizeArray);
str = getStringe();
}
}
freeData(arrOfSize, musicianCollection, numOfInstrument, date, saveSizeArray, arrInstrument);
}
char* getStringe() {
char* str;
int logSize = 0, phySize = 1, flage = 0;
char c;
printf(" Please enter the Concert woul you like to cheke : \n ");
str = (char*)malloc(sizeof(char) * phySize);
chekeAlloction(str);
c = getchar();
while (c != '\n')
{
if (logSize == phySize)
{
phySize *= 2;
str = (char*)realloc(str, sizeof(char) * phySize);
}
str[logSize] = c;
logSize++;
c = getchar();
}
str = (char*)realloc(str, sizeof(char) * (logSize +1));
chekeAlloction(str);
str[logSize] = '\0';
flage=chekeIfLineBlanc(str);
if (flage == 0) {
free(str);
return NULL;
}
return str;
}
int chekeIfLineBlanc(char* str) {
int flage = 1;
if (strlen(str) == 0) {
flage = 0;
return flage;
}
return flage;
}
IDName* getListOfInstrumentInTheConcert(char *str,InstrumentTree tr,CIList *alst) {
CIList lst;
makeEmptyCIList(&lst);
int id,nameLen,psize,lsize=0,counter=0;
psize = 2;
char* token;
IDName* arrInstrument = (IDName*)malloc(sizeof(IDName) * psize);
chekeAlloction(arrInstrument);
ConcertInstrument* CInstrument=NULL;
token = strtok(str, " ");
while (token != NULL) {
id=findInsId(tr,token);
if (id != -1 && counter == 0) {
if (lsize == psize) {
psize = psize * 2;
arrInstrument = (IDName*)realloc(arrInstrument,psize*(sizeof(IDName)));
chekeAlloction(arrInstrument);
}
CInstrument = (ConcertInstrument*)malloc(sizeof(ConcertInstrument));
chekeAlloction(CInstrument);
CInstrument->inst = id;
nameLen = strlen(token);
arrInstrument[lsize].nameOfInstrument= (char*)malloc((nameLen+1) * sizeof(char));
chekeAlloction(arrInstrument[lsize].nameOfInstrument);
strcpy(arrInstrument[lsize].nameOfInstrument, token);
arrInstrument[lsize].nameOfInstrument[nameLen] = '\0';
arrInstrument[lsize].idOfInstrument = id;
lsize++;
counter++;
}else if (counter == 1) {
CInstrument->num = atoi(token);
counter++;
}else if (counter == 2) {
CInstrument->importance = atoi(token);
creatNodeAndInsertToCIListTail(&lst, NULL, CInstrument);
counter = 0;
}
token = strtok(NULL, " ");
}
arrInstrument = (IDName*)realloc(arrInstrument,(sizeof(IDName)*lsize));
chekeAlloction(arrInstrument);
*alst = lst;
return arrInstrument;
}
////////////////***********************************////////////////////////////////////////////
/// FUNTION HELP CILIST : LIKEND LIST (MAKE EMPTY ,INSER NODE,CREATE NODE //////////
void makeEmptyCIList(CIList* lst) {
lst->head = lst->tail = NULL;
}
void insertNodeToCIListTail(CIList* lst, CIListNode* cur) {
if (lst->head == NULL && lst->tail == NULL) {
lst->head = lst->tail = cur;
}
else {
cur->next = NULL;
lst->tail->next = cur;
lst->tail = cur;
}
}
void creatNodeAndInsertToCIListTail(CIList* lst, CIListNode* next,ConcertInstrument *CInstrument) {
CIListNode* newNode = (CIListNode*)malloc(sizeof(CIListNode));
chekeAlloction(newNode);
newNode->next = next;
newNode->CInstrument.importance = CInstrument->importance;
newNode->CInstrument.inst = CInstrument->inst;
newNode->CInstrument.num = CInstrument->num;
insertNodeToCIListTail(lst, newNode);
}
////////////////************************////////////////////////////////////////////////
///***************************************************************************//////////
//function for arrange the array accordinate to the importance
//if the importance is 1 so arrange the array of musician from high to low if 0 from low to zero
//use an array of salary for help the qsort .
void arrangeArrayAppropriateToTheImportance(CIList lst, Musician*** musicianCollection,int *arrOfSize) {
CIListNode* cur;
cur = lst.head;
int id;
while (cur != NULL) {
id = cur->CInstrument.inst;
int* arrhelper = (int*)malloc(sizeof(int) * arrOfSize[id]);
copySalaryToArr(musicianCollection[id], arrhelper, arrOfSize[id], id);
if (cur->CInstrument.importance == 1) {
sortArrayMusicianOfThisIDInstrumentToLower(musicianCollection[id],arrOfSize[id], id,arrhelper);
}else {
sortArrayMusicianOfThisIDInstrumentToHigher(musicianCollection[id], arrOfSize[id], id,arrhelper);
}
cur = cur->next;
}
}
//sorting the array salary of an ID musician
//sort with qsort
//after sorting -> arrange the musician ID accordinate to the order of the salary array
void sortArrayMusicianOfThisIDInstrumentToLower(Musician **musician,int size,int id,int *arrhelper) {
qsort(arrhelper,size,sizeof(int),cmpToLower);
copySortingSalaryToMusicianArr(musician, arrhelper, size,id);
free(arrhelper);
}
//function compare for the qsort function
int cmpToLower(void *a ,void *b) {
int* firstSalary = (int*)a;
int* secondSalary = (int*)b;
return ( (*secondSalary)- (*firstSalary));
}
//function compare for the qsort function this function return a num if the nume is positive so the first num is
//higher then the second in the array.
int cmpToHigher(void* a, void* b) {
int* firstSalary = (int*)a;
int* secondSalary = (int*)b;
return ((*firstSalary) - (*secondSalary));
}
//sorting the array salary of an ID musician
//sort with qsort
//after sorting -> arrange the musician ID accordinate to the order of the salary array
void sortArrayMusicianOfThisIDInstrumentToHigher(Musician** musician, int size, int id ,int *arrhelper) {
qsort(arrhelper, size, sizeof(int), cmpToHigher);
copySortingSalaryToMusicianArr(musician, arrhelper, size, id);
free(arrhelper);
}
//simple function swap with pointer swaping
void swap(Musician** a, Musician** b) {
Musician* tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void printCIList(CIList lst) {
CIListNode *cur;
cur = lst.head;
while (cur != NULL) {
printf("%d ", cur->CInstrument.importance);
printf("%d ", cur->CInstrument.num);
printf("%d ", cur->CInstrument.inst);
cur = cur->next;
}
}
//purpose :
//to put every musician in a new array collection
//go over evry the array for every instrumnet and put my musicain in the new array colection
//arrOfsizeWMP-meaning the array of num of musician that playing an instrument in the concert
Musician*** arrangeMusicianToConcert(CIList lst, Musician*** musicianCollection, int* arrOfSize,int *lsize,int *arrOfsizeWMP) {
CIListNode* cur;
cur = lst.head;
int id,psize;
psize = 2;
Musician** musicianplayinconcert;
Musician*** allMusicianWhoPlayInConcert=(Musician***)malloc(sizeof(Musician**)*psize);
while (cur != NULL) {
id = cur->CInstrument.inst;
if (*lsize == psize) {
psize = psize * 2;
allMusicianWhoPlayInConcert = (Musician***)realloc(allMusicianWhoPlayInConcert, sizeof(Musician**) * psize);
chekeAlloction(allMusicianWhoPlayInConcert);
}
//this function will get the the musician who will play in the concert
//for every instrument with go over the array collection
//return the musician array that was not chosen
//musiciancollection[id]-this is the musician who playing with the instrument that have an this ID ont the NLSIT
musicianplayinconcert=getMusicianWhoWillPlayInConcert(musicianCollection[id], arrOfSize[id],cur->CInstrument.num);
if (musicianplayinconcert == NULL) {
//this function make the choosen musician 0 and free the array because the concert not going on.
makeZeroOnChoosenMusician(allMusicianWhoPlayInConcert, *lsize, arrOfsizeWMP);
return NULL;
}else {
allMusicianWhoPlayInConcert[*lsize] = musicianplayinconcert;
(*lsize)++;
}
cur = cur->next;
}
//this function make zero the choosen because i finish to chooze my musician but not free the arrray
makeZeroOnChoosenMusician(allMusicianWhoPlayInConcert, *lsize, arrOfsizeWMP);
allMusicianWhoPlayInConcert = (Musician***)realloc(allMusicianWhoPlayInConcert, sizeof(Musician**) * (*lsize));
chekeAlloction(allMusicianWhoPlayInConcert);
return allMusicianWhoPlayInConcert;
}
//purpose: to put the musician in a new array musician who will play in the concert
//if ther are not enough musician for my concert reutnr null if yes return the array
//musician-the musician that playing an instrument
//size-the size array of this musician array
//num-the amount of how many i need for my concert
Musician** getMusicianWhoWillPlayInConcert(Musician** musician, int size,int num) {
ListNode* cur;
int j = 0, counter = 0;
Musician** musicianplayinconcert = (Musician**)malloc(sizeof(Musician*) * num);
chekeAlloction(musicianplayinconcert);
while (counter != num && j < size) {
if (musician[j]->chozen == 0) {
musician[j]->chozen = 1;
//theye point to the same pointer so i need be carfule whene i free my arr (musiciainplayinconcert)
musicianplayinconcert[counter] = musician[j];
counter++;
}
j++;
}
if (counter == num) {
return musicianplayinconcert;
}else {
j--;
while (counter != 0 && 0 < j) {
if (musician[j]->chozen == 1) {
musician[j]->chozen = 0;
counter--;
}
j--;
}
free(musicianplayinconcert);
return NULL;
}
}
//function puprose - a function for build an array that save me the number of every musician i need of
//one instrument in the concert
int* getArrOfSizeOfEvryInstrument(CIList lst) {
CIListNode* cur;
cur = lst.head;
int lsize = 0;
int psize = 2;
int* arrOfsizeOfEvryInstrument = (int*)malloc(sizeof(int) * psize);
chekeAlloction(arrOfsizeOfEvryInstrument);
while (cur != NULL) {
if (psize == lsize) {
psize = psize * 2;
arrOfsizeOfEvryInstrument = (int*)realloc(arrOfsizeOfEvryInstrument, sizeof(int*) * psize);
chekeAlloction(arrOfsizeOfEvryInstrument);
}
arrOfsizeOfEvryInstrument[lsize] = cur->CInstrument.num;
lsize++;
cur = cur->next;
}
arrOfsizeOfEvryInstrument = (int*)realloc(arrOfsizeOfEvryInstrument, sizeof(int*) * lsize);
chekeAlloction(arrOfsizeOfEvryInstrument);
return arrOfsizeOfEvryInstrument;
}
//purpose : to make the chosen zero for the musician that successe to be save in the concert
//but the concert not have enough musician so i need to "arrange back" the musician
void makeZeroOnChoosenMusician(Musician*** allMusicianWhoPlayInConcert, int size, int* arrSize) {
int i, j;
for (i = 0; i < size; i++) {
for (j = 0; j < arrSize[i]; j++) {
allMusicianWhoPlayInConcert[i][j]->chozen = 0;
}
}
}
//function puprose - copy the salary to the arr
//this arr will help me for the sorting challenge
void copySalaryToArr(Musician** musician,int *arrhelper,int size,int id) {
ListNode* cur;
int i;
for (i = 0; i < size; i++) {
cur = musician[i]->instrument.head;
while (cur != NULL) {
if (cur->mpi.insID == id) {
arrhelper[i] = cur->mpi.price;
}
cur = cur->next;
}
}
}
//function purpose - function that get an sorting array with the salary of an ID instrument
//and accordinate to the array salary I sort my musician array
void copySortingSalaryToMusicianArr(Musician** musician, int* arrhelper, int size, int id ) {
ListNode* cur;
int i=0,j=0;
while (i < size) {
cur = musician[j]->instrument.head;
while (cur != NULL) {
if (cur->mpi.insID == id && cur->mpi.price == arrhelper[i]) {
swap(&musician[i] ,&musician[j]);
i++;
j = -1;
}
cur = cur->next;
}
j++;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
//******************* FUNCTION FOR PRINTING DATA **********************************///////////
void printError(char* name) {
printf("Could not find musicians for the concert %s \n", name);
}
//function that is purpose is print the information.
//the function get :
//name-nameofconcert ____allMusician-array of musician who play in the concert________size-size of th array allMusician
//date-the day,hour,year,min to print.
//arrofsize-an array with the different size of array in the array all musician colection
//nameofinstrument-an array of the names of the instrument that musician use in this concert
void printTheConcert(Musician*** allMusician,int size, char* name, Date *date,int *arrOfsizeOfMusicianWhoPlay,IDName* arrInstrument) {
int totalePrice=0;
printf("*********************************************************************** \n");
printf("***** THE CONCERT : %s ****** \n", name);
if (date->min >= 0 && date->min <= 9) {
printf("***** TIME OF THE CONCERT : %d/%d/%d %.f:0%.f ****** \n", date->day, date->month, date->year, date->hour, date->min);
}
else {
printf("***** TIME OF THE CONCERT : %d/%d/%d %.f:%.f ****** \n", date->day, date->month, date->year, date->hour, date->min);
}
printf("*********************************************************************** \n");
printArrayCollection(allMusician, size, arrOfsizeOfMusicianWhoPlay, arrInstrument,&totalePrice);
printf(" The total Price for this concert is : %d \n", totalePrice);
printf("\n");
}
void printArrayCollection(Musician*** musicianCollection, int numOfInstrument, int* arrOfsizeOfMusicianWhoPlay, IDName* arrInstrument,int *totalPrice) {
int i;
for (i = 0; i < numOfInstrument; i++) {
printf(" The musician who will play on : %s \n", arrInstrument[i].nameOfInstrument);
printArrayOfMusician(musicianCollection[i], arrOfsizeOfMusicianWhoPlay[i],arrInstrument[i].idOfInstrument,totalPrice, arrInstrument[i].nameOfInstrument);
}
}
void printArrayOfMusician(Musician** musicianGroup, int size,int id,int *totalPrice,char *nameInstrument) {
int i;
for (i = 0; i < size; i++) {
printName(musicianGroup[i]->lenghtName, musicianGroup[i]->name);
printPrice(musicianGroup[i]->instrument,id,totalPrice,nameInstrument, musicianGroup[i]->name, musicianGroup[i]->lenghtName);
}
}
void printPrice(MPIList lst, int id,int *totalPrice, char* nameInstrument,char** name,int lenName) {
ListNode* cur;
cur = lst.head;
while (cur != NULL) {
if (cur->mpi.insID==id) {
printf(" The price for that ");
printName(lenName, name);
printf(" play on the %s is :%f \n", nameInstrument, cur->mpi.price);
*totalPrice = *totalPrice + cur->mpi.price;
}
cur = cur->next;
}
}
void printName(int legnthName,char **name) {
int i;
printf(" The musician : ");
for (i = 0; i < legnthName; i++) {
printf(" %s ",name[i]);
}