This repository was archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1397 lines (1227 loc) · 39.6 KB
/
main.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
/**
@name Modelisation Formation
@authors Eva GENTILHOMME, Isra DHIAB
@copyright Copyright (c) 2022, Eva GENTILHOMME, Isra DHIAB
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:\n
1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.\n
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.\n\n
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <assert.h>
#pragma warning (disable:4996)
/**
* Limits for every var and structures. \n
* MAX_*, MIN_*, NB_*, *_MIN, *_MAX = limits for each value. \n
* *_SID = IDs generated for each command name. \n
* CODE_* = error codes.
*/
enum
{
MAX_CHAR = 31,
NB_SEMESTRES = 2,
MIN_UE = 3,
MAX_UE = 6,
MAX_MATIERES = 10,
MAX_EPREUVES = 5,
MAX_ETUDIANTS = 100,
NOTE_MIN = 0,
NOTE_MAX = 20,
// str IDs
FORMATION_SID = 975,
EPREUVE_SID = 764,
COEFFICIENTS_SID = 1266,
NOTE_SID = 438,
NOTES_SID = 553,
RELEVE_SID = 643,
DECISION_SID = 846,
// error codes
CODE_SUCCESS = 0,
CODE_SEM_INCORRECT = 1,
CODE_COEFF_INCORRECT = 2,
CODE_MAT_INCORRECT = 4,
CODE_UE_INCORRECT = 3,
CODE_ETU_INCORRECT = 7,
CODE_NOTE_MISSING = 8,
CODE_EPR_INCORRECT = 5,
CODE_NOTE_INCORRECT = 6,
CODE_UE_ALREADY_DEFINED = 9,
CODE_EPR_ALREADY_DEFINED = 10,
CODE_NOTE_ALREADY_DEFINED = 11,
CODE_COEFF_ALL_ZERO = 12,
CODE_SEM_EMPTY = 13
};
/**
* A linked list structure, where each element is called a "node".
* Each node is composed of its value (a string with a max
* size of MAX_CHAR) and a pointer that points towards the next
* node.
*/
typedef struct Node
{
/*@{*/
char szValue[MAX_CHAR]; /**< value of the node */
struct Node * ndpNext; /**< address of the next node */
/*@}*/
} Node;
/**
* Epreuve structure.
*/
typedef struct {
/*@{*/
char szNom[MAX_CHAR]; /**< name of the Epreuve */
float fCoef[MAX_UE]; /**< list of the coefficients of the Epreuve */
/*@}*/
} Epreuve;
/**
* Matiere structure.
*/
typedef struct {
char szNom[MAX_CHAR]; /**< name of the Matiere */
unsigned int uNbEpreuves; /**< number of Epreuve in the Matiere */
Epreuve epreuves[MAX_EPREUVES]; /**< list of Epreuve in the Matiere */
} Matiere;
/**
* Semestre structure.
*/
typedef struct {
unsigned int uNbMatieres; /**< number of Matiere in the Semestre */
Matiere matieres[MAX_MATIERES]; /**< list of Matiere in the Semestre */
} Semestre;
/**
* Formation structure.
*/
typedef struct {
unsigned int uNbUE; /**< Number of UE in the Formation */
Semestre semestres[NB_SEMESTRES]; /**< list of Semestre in the Formation */
} Formation;
/**
* Note structure.
*/
typedef struct {
Epreuve * pEpreuve; /**< address of the Epreuve referenced by the Note */
float fNote; /**< the grade */
} Note;
/**
* EtuMatiere structure.
*/
typedef struct {
Matiere * pMatiere; /**< address of the Matiere referenced by the EtuMatiere */
unsigned int uNbNotes; /**< number of Notes in the EtuMatiere */
Note notes[MAX_EPREUVES]; /**< list of Notes in the EtuMatiere */
} EtuMatiere;
/**
* EtuSemestre structure.
*/
typedef struct {
unsigned int uNbMatieres; /**< number of Matiere in the EtuSemestre */
EtuMatiere matieres[MAX_MATIERES]; /**< list of Matiere in the EtuSemestre */
} EtuSemestre;
/**
* Etudiant structure.
*/
typedef struct {
char szNom[MAX_CHAR]; /**< name of the Etudiant */
EtuSemestre semestres[NB_SEMESTRES]; /**< number of EtuSemestre */
} Etudiant;
/**
* Promotion structure.
*/
typedef struct {
unsigned int uNbEtudiants; /**< number of Etudiant */
Etudiant etudiants[MAX_ETUDIANTS]; /**< list of Etudiant */
} Promotion;
/** Creates a new node in the linked list.
* @param ndp The address of the node to link.
* @param szValue The value to add in the new node
* @return The node created by the function
*/
Node * newNode(Node * ndp, const char szValue[MAX_CHAR])
{
// Memory allocation for the new node
Node * ndNewNode = (Node *) malloc(sizeof(Node));
// Fill the memory allocated for the node value by zeros.
memset(&ndNewNode->szValue, 0, sizeof(char[MAX_CHAR]));
strcpy(ndNewNode->szValue, szValue);
// Adds the address of the next node in the current node.
ndNewNode->ndpNext = ndp;
return ndNewNode;
}
/** Deletes the last node put in the linked list.
* @param ndp The address of the node to delete.
* @return The node after the one deleted.
*/
Node * delNode(Node * ndp)
{
Node * ndpNodeToDelete = ndp;
// Sets the next node in the linked list as the current one
ndp = (ndp->ndpNext != NULL) ? ndp->ndpNext
: NULL;
// Deletes the node.
free(ndpNodeToDelete);
return ndp;
}
/** Deletes every node in the linked list.
* @param ndp : The address of the first node to delete.
* @return NULL
*/
Node * delAllNodes(Node * ndp)
{
while (ndp != NULL) {
ndp = delNode(ndp);
}
return NULL;
}
/** Creates a node with a trimmed string as the value.
* @param ndp the first node's address of a linked list
* @param szInput the string to trim
* @param iMin the address of the value where the first index of the new string
* is stored. Will change if iMin is > 2.
* @param iMax the address of the value where the last index of the new string
* is stored. Will change if iMin is > 2.
* @return the address of the last node added to the linked list
*
*/
Node * newNodeValueFromStr(Node * ndp, const char szInput[], int * iMin, int * iMax)
{
char szCurrentStr[MAX_CHAR] = {0};
assert (*iMax - *iMin < MAX_CHAR);
if (*iMin <= *iMax) {
memcpy(&szCurrentStr, &szInput[*iMin], (*iMax - *iMin)+1 * sizeof(char));
ndp = newNode(ndp, szCurrentStr);
memset(&szCurrentStr, 0, sizeof(char) * MAX_CHAR);
}
if (*iMin >= 3) {
*iMax = *iMin-2;
*iMin = *iMax-1;
}
return ndp;
}
/** Parses a string: separates every value (defined by every character
* sequence that doesn't include the cSeparator)
* @param ndp The first node's address of a linked list
* @param szInput the string to parse
* @param cSeparator the delimitation of every value
* @return the address of the last node added to the linked list
*
*/
Node * parseInput(Node * ndp, const char szInput[], char cSeparator)
{
int iMin = (int) strlen(szInput)-1,
iMax = (int) strlen(szInput);
int iDelimiterCount = 0;
for (int i = iMax; i >= 0; --i) {
iMin = i+1;
if (szInput[i] == 39 || szInput[i] == 34)
{
++iDelimiterCount;
if (iDelimiterCount%2 != 0) --iMax;
}
if (szInput[i] == cSeparator && iDelimiterCount%2 == 0 || i == 1) {
if (i == 1) {
iMin = 0;
}
bool isQuoted = (((szInput[i+1] == 39 || szInput[i+1] == 34) && i > 1)
|| ((szInput[i] == 39 || szInput[i] == 34) && i == 1));
if (isQuoted) ++iMin;
ndp = newNodeValueFromStr(ndp, szInput, &iMin, &iMax);
if (isQuoted) {
--iMin;
--iMax;
}
}
}
return ndp;
}
/** Returns the length of a linked list.
* @param ndp: the first node's address of a linked list
* @return the length of the linked list
*/
unsigned int getLinkedListLength(const Node * ndp)
{
unsigned int uCount = 0;
while (ndp != NULL) {
ndp = ndp->ndpNext;
++uCount;
}
return uCount;
}
/**
* Gives the value of the node uIndex, and returns NULL if the nodes doesn't exist.
* @param ndp the first node's address of a linked list
* @param uIndex the index of the node needed
* @return the value of the node uIndex, or NULL if the node doesn't exist.
*
* @pre uIndex >= 0
*
* @related getNodeValueAsFloat()
* @related getNodeValueAsInt()
*/
char * getNodeValue(Node * ndp, unsigned int uIndex)
{
assert (uIndex >= 0);
while (uIndex --> 0 && ndp != NULL) {
ndp = ndp->ndpNext;
}
if (ndp != NULL) {
return ndp->szValue;
} else return NULL;
}
/**
* Gives the value of the node uIndex as a float.
* @param ndp the first node's address of a linked list
* @param uIndex the index of the node needed
* @return the value of the node uIndex as a float.
*
* @pre uIndex >= 0
* @pre You must check if the node's value is a correct representation of a number before!!
*/
float getNodeValueAsFloat(Node * ndp, unsigned int uIndex)
{
assert (uIndex >= 0);
char * szValue = getNodeValue(ndp, uIndex);
int i = 0, iDotIndex = -1;
float fValue = 0;
bool isNegative = (szValue[0] == 45);
for (i; szValue[i+1] != 0; ++i) {
assert (szValue[i] >= 48 && szValue[i] <= 59);
if (szValue[i] == 46) {
iDotIndex = i;
}
}
if (iDotIndex == -1) iDotIndex = i+1;
for (i; i >= (isNegative) ? 1 : 0; --i) {
if (i > iDotIndex)
{
// multiply by 10^-1, -2... if we're in the decimal part
fValue += (float)(szValue[i]-48)*powf(10.f,(float)-(i-iDotIndex));
}
else if (i < iDotIndex)
{
// multiply by 9^1, 2... if we're in the integer part
fValue += (float)(szValue[i]-48)*powf(10.f, (float)(iDotIndex-1-i));
}
}
return (isNegative) ? -fValue : fValue;
}
/**
* Gives the value of the node uIndex as an int.
* @param ndp the first node's address of a linked list
* @param uIndex the index of the node needed
* @return the value of the node uIndex as an int.
* @pre uIndex >= 0
* @pre You must check if the node's value is a correct representation of a number before!!
*/
int getNodeValueAsInt(Node * ndp, unsigned int uIndex)
{
// preconditions will be checked in getNodeValueAsFloat()
return (int) getNodeValueAsFloat(ndp, uIndex);
}
/** Gives an ID to the string specified
* @param szUserInput the string
* @return an ID (as an unsigned long)
*
* @warning
* This function is not secure. Multiple sentences can have the same ID, which makes it
* unreliable for a lot of usecases.
*/
unsigned long getStringId(const char szUserInput[])
{
unsigned long uWeightString = 0;
for (int i = 0; i < strlen(szUserInput); i++)
{
uWeightString += szUserInput[i];
}
return uWeightString;
}
/**
* Checks if there's at least one coefficient greater than 0
* @param uNbUE the number of UE (= number of relevant items in fCoefs array)
* @param fCoefs array of float
* @return true, false depending on the validity of the coefficients
*/
bool coefCorrect(unsigned int uNbUE, const float fCoefs[MAX_UE])
{
for (int iCoef = 0; iCoef < uNbUE; ++iCoef) {
if (fCoefs[iCoef] < 0) return false;
if (fCoefs[iCoef] > 0) return true;
}
return false;
}
/**
* Search a Matiere in a semestre, and returns its address if there's a match.
* @param sp pointer to the Semestre to check
* @param szMatiereName the name to search in the semestre
* @return pointer to the Epreuve (or NULL if it doesn't exist)
*/
Matiere * getMatiereByName(Semestre * semp, const char szMatiereName[MAX_CHAR])
{
for (int iMat = 0; iMat < semp->uNbMatieres; ++iMat) {
if (!strcmp(semp->matieres[iMat].szNom, szMatiereName))
return &semp->matieres[iMat];
}
return NULL;
}
/**
* Creates a Matiere in a given Semestre.
* @param semp pointer to the Semestre
* @param szMatiereName the name of the new matiere
* @return the newly created Matiere
*/
Matiere * newMatiere(Semestre * semp, const char szMatiereName[MAX_CHAR])
{
Matiere * matp = &semp->matieres[semp->uNbMatieres++];
char * szDest = matp->szNom;
strcpy(szDest, szMatiereName);
matp->uNbEpreuves = 0;
printf("Matiere ajoutee a la formation\n");
return matp;
}
/**
* Search a Matiere in a semestre, and returns its address if there's a match.
* @param matp pointer to the Matiere to check
* @param szMatiereName the name to search in the semestre
* @return pointer to the Epreuve (or NULL if it doesn't exist)
*/
Epreuve * getEpreuveByName(Matiere * matp, const char szEpreuveName[MAX_CHAR])
{
for (int iEpr = 0; iEpr < matp->uNbEpreuves; ++iEpr) {
if (!strcmp(matp->epreuves[iEpr].szNom, szEpreuveName))
return &matp->epreuves[iEpr];
}
return NULL;
}
/**
* Creates an Epreuve in a given Matiere.
* @param matp pointer to the Matiere
* @param szEpreuveName the name of the new Epreuve
* @return the newly created Epreuve
*/
Epreuve * newEpreuve(Matiere * matp, const char szEpreuveName[MAX_CHAR],
const float fCoef[MAX_UE], unsigned int uNbUE)
{
Epreuve * eprp = &matp->epreuves[matp->uNbEpreuves++];
char * szDest = eprp->szNom;
strcpy(szDest, szEpreuveName);
for (int iUE = 0; iUE < uNbUE; ++iUE)
eprp->fCoef[iUE] = fCoef[iUE];
return eprp;
}
/**
* Search an Etudiant in a Promotion, and returns its address if there's a match.
* @param pp pointer to the Promotion to check
* @param szEtudiantName the name to search in the Promotion
* @return pointer to the Etudiant (or NULL if it doesn't exist)
*/
Etudiant * getEtudiantByName(Promotion * pp, const char szEtudiantName[MAX_CHAR])
{
for (int iEtu = 0; iEtu < pp->uNbEtudiants; ++iEtu)
{
if (!strcmp(pp->etudiants[iEtu].szNom, szEtudiantName))
return &pp->etudiants[iEtu];
}
return NULL;
}
/**
* Creates an Etudiant in a given Promotion.
* @param pp pointer to the Promotion
* @param szEtudiantName the Etudiant name
* @return the newly created Etudiant
*/
Etudiant * newEtudiant(Promotion * pp, const char szEtudiantName[MAX_CHAR])
{
Etudiant * etup = &pp->etudiants[pp->uNbEtudiants++];
strcpy(etup->szNom, szEtudiantName);
for (int iSem = 0; iSem < NB_SEMESTRES; ++iSem) {
etup->semestres[iSem].uNbMatieres = 0;
}
printf("Etudiant ajoute a la formation\n");
return etup;
}
/**
* Search an EtuMatiere in an EtuSemestre, and returns its address if there's a match.
* @param etuSemp pointer to the EtuSemestre to check
* @param szMatiereName the name to search in the EtuSemestre
* @return pointer to the EtuMatiere (or NULL if it doesn't exist)
*/
EtuMatiere * getEtuMatiereByName(EtuSemestre * etuSemp,
const char szMatiereName[MAX_CHAR])
{
for (int iEtuMat = 0; iEtuMat < etuSemp->uNbMatieres; ++iEtuMat)
{
Matiere * matp = etuSemp->matieres[iEtuMat].pMatiere;
if (!strcmp(matp->szNom, szMatiereName))
return &etuSemp->matieres[iEtuMat];
}
return NULL;
}
/**
* Creates an EtuMatiere in a given EtuSemestre, corresponding to an existing Matiere.
* @param matp pointer to the Matiere
* @param EtuSemestre pointer to the EtuSemestre
* @return the newly created Epreuve
*/
EtuMatiere * newEtuMatiere(Matiere * matp, EtuSemestre * etuSemp)
{
unsigned int uIndexMatiere = etuSemp->uNbMatieres++;
EtuMatiere * etuMatp = &etuSemp->matieres[uIndexMatiere];
etuMatp->pMatiere = matp;
etuMatp->uNbNotes = 0;
return etuMatp;
}
/**
* Search an EtuMatiere in an EtuSemestre, and returns its address if there's a match.
* @param etuSemp pointer to the EtuSemestre to check
* @param szMatiereName the name to search in the EtuSemestre
* @return pointer to the EtuMatiere (or NULL if it doesn't exist)
*/
Note * getNoteByEpreuveName(EtuMatiere * etuMatp,
const char szEpreuveName[MAX_CHAR])
{
for (int iNote = 0; iNote < etuMatp->uNbNotes; ++iNote)
{
Epreuve * eprp = etuMatp->notes[iNote].pEpreuve;
if (!strcmp(eprp->szNom, szEpreuveName))
return &etuMatp->notes[iNote];
}
return NULL;
}
/**
* Creates a Note in a given EtuMatiere, corresponding to an existing Epreuve.
* @param eprp pointer to the Epreuve to link
* @param etuMatp pointer to the EtuMatiere
* @param fNote: the Note to add
* @return the newly created Note
*/
Note * newNote(Epreuve * eprp, EtuMatiere * etuMatp, float fNote) {
unsigned int uIndexNote = etuMatp->uNbNotes++;
Note * note = &etuMatp->notes[uIndexNote];
note->pEpreuve = eprp;
note->fNote = fNote;
return note;
}
/**
* Counts the number of Epreuve in a Semestre, and adds every coefficients of
* each UE in an array.
* @param fp pointer to the Formation
* @param semp pointer to the Semestre
* @param fCoefs array of floats, representing a sum of every coefficients in each UE. Is changed by this function.
* @return the number of epreuves, as an unsigned int
*/
unsigned int countEpreuve(const Formation * fp, const Semestre * semp,
float fCoefs[MAX_UE])
{
unsigned int uCountEpreuve = 0;
for (int iMat = 0; iMat < semp->uNbMatieres; ++iMat)
{
const Matiere * matp = &semp->matieres[iMat];
for (int iEpr = 0; iEpr < matp->uNbEpreuves; ++iEpr)
{
++uCountEpreuve;
const Epreuve * eprp = &matp->epreuves[iEpr];
for (int iUE = 0; iUE < fp->uNbUE; ++iUE)
{
fCoefs[iUE] += eprp->fCoef[iUE];
}
}
}
return uCountEpreuve;
}
/**
* Checks if the Etudiant have a Note for every Epreuve
* @param semp
* @param etuSemp
* @return true or false
*/
bool checkNotes(Semestre * semp, EtuSemestre * etuSemp) {
if (semp->uNbMatieres == etuSemp->uNbMatieres)
{
for (int iMat = 0; iMat < etuSemp->uNbMatieres; ++iMat)
{
Matiere * matp = &semp->matieres[iMat];
EtuMatiere * etuMatp = &etuSemp->matieres[iMat];
if (etuMatp->uNbNotes != matp->uNbEpreuves) return false;
}
} else return false;
return true;
}
/**
* Trims a number.
* @param fValue The number to trim
* @param trimAfter The number of digits to trim. 0 = the dot
* @return trimmed number
*/
float trim(float fValue, int trimAfter)
{
float fRank = powf(10, (float)trimAfter);
return floorf(fValue*fRank)/fRank;
}
/**
* Returns the moyenne for a given UE and EtuMatiere.
* @param etuMatp
* @param uIndexUE
* @return moyenne (float)
*/
float getMoyenneMatiere(EtuMatiere * etuMatp, unsigned int uIndexUE)
{
float fDivid = 0, fDivis = 0;
for (int iNotes = 0; iNotes < etuMatp->uNbNotes; iNotes++)
{
fDivid += etuMatp->notes[iNotes].fNote
* etuMatp->notes[iNotes].pEpreuve->fCoef[uIndexUE];
fDivis += etuMatp->notes[iNotes].pEpreuve->fCoef[uIndexUE];
}
if (fDivis == 0) return -1.f;
return fDivid / fDivis;
}
/**
* Returns the moyenne of every Epreuve in a Semestre for a given UE
* @param etuSemp
* @param uIndexUE
* @return moyenne (float)
*/
float getMoyenneUE(EtuSemestre * etuSemp, unsigned int uIndexUE)
{
float fDivid = 0, fDivis = 0;
for (int iIndMat = 0; iIndMat < etuSemp->uNbMatieres; ++iIndMat)
{
EtuMatiere *etuMatp = &etuSemp->matieres[iIndMat];
for (int iIndNote = 0; iIndNote < etuMatp->uNbNotes; ++iIndNote)
{
Note *note = &etuMatp->notes[iIndNote];
fDivid += note->fNote * note->pEpreuve->fCoef[uIndexUE];
fDivis += note->pEpreuve->fCoef[uIndexUE];
}
}
if (fDivis == 0) return -1.f;
return fDivid / fDivis;
}
/**
* Returns the moyenne of every Semestre for a given UE
* @param etup
* @param uIndexUE
* @return moyenne (float)
*/
float getMoyenneUEYear(Etudiant * etup, unsigned int uIndexUE)
{
float fDivid = 0;
for (int iSem = 0; iSem < NB_SEMESTRES; ++iSem)
{
EtuSemestre * etuSemp = &etup->semestres[iSem];
fDivid += getMoyenneUE(etuSemp, uIndexUE);
}
return fDivid / NB_SEMESTRES;
}
/**
* Returns the maximum between two unsigned int
* @param u1
* @param u2
* @return The maximum between u1 and u2
*/
unsigned int maxi(unsigned int u1, unsigned int u2)
{
if (u1 > u2)
return u1;
return u2;
}
/**
* Gets the length of the longest name for a Matiere in a Semestre
* @param semp Pointer to Semestre
* @return Length of the longest Matiere name in semp
*/
unsigned int getLongestMatiereName(Semestre * semp)
{
unsigned int uLength = 0;
for (int iMat = 0; iMat < semp->uNbMatieres; ++iMat)
{
Matiere * matp = &semp->matieres[iMat];
uLength = maxi(uLength, strlen(matp->szNom));
}
return uLength;
}
/** Sub-function of releve().
* Prints the UE line
* @param uMaxName The longest matiere name
* @param uNbUE Number of UE
*/
void printUELine(unsigned int uMaxName, unsigned int uNbUE) {
printf("%*s", uMaxName+1, " ");
for (int iUE = 1; iUE <= uNbUE; ++iUE)
printf(" UE%d ", iUE);
printf("\n");
}
/**
* Sub-function of releve().
* Prints a moyenne line for a given Matiere
* @param matp
* @param etuMatp
* @param uMaxName
* @param uNbUE
*/
void printReleveLine(Matiere * matp, EtuMatiere * etuMatp, unsigned int uMaxName,
unsigned int uNbUE)
{
printf("%s%*s", matp->szNom, (int)(uMaxName-strlen(matp->szNom)+1), " ");
for (int iUE = 0; iUE < uNbUE; ++iUE)
{
float fMoyenne = trim(getMoyenneMatiere(etuMatp, iUE), 1);
if (fMoyenne != -1) {
printf("%4.1f ", fMoyenne);
} else {
printf(" ND ");
}
}
printf("\n");
}
/** Sub-function of decision() and releve().
* Prints UE moyenne for a Semestre.
* @param etuSemp
* @param uMaxName
* @param uNbUE
* @param szTitle Name of this line
*/
void printMoyenneUELine(EtuSemestre * etuSemp, unsigned int uMaxName,
unsigned int uNbUE, const char szTitle[])
{
printf("%s%*s", szTitle, (int)(uMaxName-strlen(szTitle)+1), " ");
for (int iUE = 0; iUE < uNbUE; ++iUE)
{
float fMoyenne = trim(getMoyenneUE(etuSemp, iUE), 1);
printf("%4.1f ", fMoyenne);
}
printf("\n");
}
/** Sub-function of decision().
* Prints every UE moyenne.
* @param etup
* @param isUEValidated
* @param uNbUEValidated
* @param uNbUE
*/
void printGenMoyenne(Etudiant * etup, bool isUEValidated[MAX_UE], unsigned int * uNbUEValidated,
unsigned int uNbUE)
{
printf("Moyennes annuelles ");
for (int iIndUE = 0; iIndUE < uNbUE; ++iIndUE)
{
float fMoySem = trim(getMoyenneUEYear(etup, iIndUE), 1);
printf("%4.1f ", fMoySem);
isUEValidated[iIndUE] = (fMoySem >= 10) ? true : false;
if (isUEValidated[iIndUE]) ++*uNbUEValidated;
}
printf("\n");
}
/** Sub-function of decision().
* Prints every UE validated.
* @param uMaxName
* @param uNbUEValidated
* @param isUEValidated
* @param uNbUE
*/
void printAcquisitionLine(unsigned int uMaxName, unsigned int uNbUEValidated,
const bool isUEValidated[MAX_UE], unsigned int uNbUE)
{
printf("Acquisition%*s", (int)(uMaxName-strlen("Acquisition")+1), " ");
if (uNbUEValidated == 0)
{
printf("Aucune");
}
else
{
unsigned int uCount = 0;
for (int iUE = 0; iUE < uNbUE; ++iUE)
{
if (isUEValidated[iUE])
{
++uCount;
char szName[4] = {'U', 'E', (char) (iUE + 49), 0};
printf("%s%s", szName, (uNbUEValidated != uCount) ? ", " : "");
}
}
}
printf("\n");
}
/** Sub-function of decision().
* Prints every line corresponding to Semestre's moyennes.
* @param etup
* @param uMaxName
* @param uNbUE
*/
void printSemestreLines(Etudiant * etup, unsigned int uMaxName, unsigned int uNbUE) {
for (int iIndSem = 0; iIndSem < 2; ++iIndSem)
{
char szSemestre[3] = {'S', (char)(iIndSem+49), 0};
EtuSemestre * etuSemp = &etup->semestres[iIndSem];
printMoyenneUELine(etuSemp, uMaxName, uNbUE, szSemestre);
}
}
/** Defines the formation's UE number if it's not already defined.
* @param fp Pointer to Formation
* @param uArgNbUe Number of UE, between MIN_UE and MAX_UE.
* @return error code
*/
int formation(Formation * fp, unsigned int uArgNbUE)
{
if (fp->uNbUE < MIN_UE || fp->uNbUE > MAX_UE)
{ // UE number is not defined in the formation
if (uArgNbUE < MIN_UE || uArgNbUE > MAX_UE)
return CODE_UE_INCORRECT;
fp->uNbUE = uArgNbUE;
return CODE_SUCCESS;
} else return CODE_UE_ALREADY_DEFINED;
}
/**
* Creates a new Epreuve.
*
* @param fp Pointer to Formation
* @param uArgIndSemestre the semestre number (starts at 1)
* @param szArgMatiereName matiere name
* @param szArgEpreuveName epreuve name
* @param fCoef coefficients list
*
* @return error code
*/
int epreuve(Formation * fp, unsigned int uArgIndSemestre, const char szArgMatiereName[MAX_CHAR],
const char szArgEpreuveName[MAX_CHAR], const float fCoef[MAX_UE])
{ // Semestre check
if (uArgIndSemestre > NB_SEMESTRES || uArgIndSemestre < 1)
return CODE_SEM_INCORRECT;
Semestre * semp = &fp->semestres[uArgIndSemestre-1];
Matiere * matp = getMatiereByName(semp, szArgMatiereName);
// Matiere check, if it doesn't exist, create it
if (matp != NULL) {
// Checks if the Epreuve already exists
if (getEpreuveByName(matp, szArgEpreuveName) != NULL)
return CODE_EPR_ALREADY_DEFINED;
} else {
// Coefficients check
if (!coefCorrect(fp->uNbUE, fCoef))
return CODE_COEFF_INCORRECT;
matp = newMatiere(semp, szArgMatiereName);
}
Epreuve * eprp = newEpreuve(matp, szArgEpreuveName, fCoef, fp->uNbUE);
return CODE_SUCCESS;
}
/**
* Checks the validity of a Semestre's coefficients.
* @param fp Pointer to Formation
* @param uArgIndSemestre Semestre to check
* @return error code
*/
int coefficients(const Formation * fp, unsigned int uArgIndSemestre) {
// Semestre check
if (uArgIndSemestre > NB_SEMESTRES || uArgIndSemestre < 1)
return CODE_SEM_INCORRECT;
const Semestre * semp = &fp->semestres[uArgIndSemestre-1];
float fCoefs[MAX_UE] = {0};
unsigned int uCountEpreuve = countEpreuve(fp, semp, fCoefs);
if (uCountEpreuve == 0 || semp->uNbMatieres == 0)
return CODE_SEM_EMPTY;
for (int iCoef = 0; iCoef < fp->uNbUE; ++iCoef)
if (fCoefs[iCoef] == 0) return CODE_COEFF_ALL_ZERO;
return CODE_SUCCESS;
}
/**
* Adds a new note to an Etudiant
* @param fp Pointer to Formation
* @param pp Pointer to Promotion
* @param uArgIndSemestre Semestre to add
* @param szArgEtudiantName Etudiant name
* @param szArgMatiereName Matiere name
* @param szArgEpreuveName Epreuve name
* @param fArgNote The grade to add
* @return error code
*/
int note(Formation * fp, Promotion * pp, unsigned int uArgIndSemestre,
const char szArgEtudiantName[MAX_CHAR], const char szArgMatiereName[MAX_CHAR],
const char szArgEpreuveName[MAX_CHAR], float fArgNote)
{ // Semestre check
if (uArgIndSemestre > NB_SEMESTRES || uArgIndSemestre < 1)
return CODE_SEM_INCORRECT;
Semestre * semp = &fp->semestres[uArgIndSemestre-1];
// Matiere check
Matiere * matp = getMatiereByName(semp, szArgMatiereName);
if (matp == NULL) return CODE_MAT_INCORRECT;
// Epreuve check
Epreuve * eprp = getEpreuveByName(matp, szArgEpreuveName);
if (eprp == NULL) return CODE_EPR_INCORRECT;
// Checks if the note is between NOTE_MIN and NOTE_MAX
if (fArgNote < NOTE_MIN || fArgNote > NOTE_MAX) return CODE_NOTE_INCORRECT;
// Etudiant check
// Creates the Etudiant if it doesn't already exist
Etudiant * etup = getEtudiantByName(pp, szArgEtudiantName);
if (etup == NULL) etup = newEtudiant(pp, szArgEtudiantName);
EtuSemestre * etuSemp = &etup->semestres[uArgIndSemestre-1];
// EtuMatiere check
// Creates the EtuMatiere if it doesn't already exist
EtuMatiere * etuMatp = getEtuMatiereByName(etuSemp, szArgMatiereName);
if (etuMatp == NULL) etuMatp = newEtuMatiere(matp, etuSemp);
// Note check
// Returns an error code if the Note already exists
Note * notep = getNoteByEpreuveName(etuMatp, szArgEpreuveName);
if (notep != NULL) return CODE_NOTE_ALREADY_DEFINED;
notep = newNote(eprp, etuMatp, fArgNote);
return CODE_SUCCESS;