Advertisement
iSach

Untitled

Oct 19th, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. struct unite_enseignement {
  7. char identifiant[10 + 1];
  8. char nom[200 + 1];
  9. int credits;
  10. char responsable[60 + 1];
  11. struct unite_enseignement *prerequis[5];
  12. struct unite_enseignement *corequis[5];
  13. };
  14.  
  15. void encoder_ue(void);
  16. void print_prerequis(struct unite_enseignement *ue);
  17. void ajouter_prerequis(struct unite_enseignement *ue1, struct unite_enseignement *ue2);
  18.  
  19. /**
  20. * Renvoie l'index du premier élément libre ou length si tab est plein.
  21. */
  22. static int premier_libre(struct unite_enseignement *tab[], int length) {
  23. for (int i = 0; i < length; i++) {
  24. if (tab[i] == NULL)
  25. return i;
  26. }
  27. return length;
  28. }
  29.  
  30. int main() { encoder_ue(); }
  31.  
  32. void encoder_ue() {
  33. struct unite_enseignement u;
  34. strcpy(u.identifiant, "AAA-1");
  35. strcpy(u.nom, "unite_enseignement 1");
  36. u.credits = 20;
  37. strcpy(u.responsable, "responsable 1");
  38. for(int i = 0; i < 5; i++) {
  39. u.corequis[i] = NULL;
  40. u.prerequis[i] = NULL;
  41. }
  42.  
  43. struct unite_enseignement u2;
  44. strcpy(u2.identifiant, "BBB-2");
  45. strcpy(u2.nom, "unite_enseignement 2");
  46. u2.credits = 30;
  47. strcpy(u2.responsable, "responsable 2");
  48. for(int i = 0; i < 5; i++) {
  49. u2.corequis[i] = NULL;
  50. u2.prerequis[i] = NULL;
  51. }
  52.  
  53. print_prerequis(&u2);
  54.  
  55. ajouter_prerequis(&u, &u2);
  56.  
  57. print_prerequis(&u2);
  58. }
  59.  
  60. void print_prerequis(struct unite_enseignement *ue) {
  61. printf("--------- Prérequis de %s ---------\n", ue->nom);
  62. int max_index = premier_libre(ue->prerequis, 5);
  63. if (max_index == 0) {
  64. printf("Aucune.\n");
  65. } else {
  66. for (int i = 0; i < max_index; i++) {
  67. struct unite_enseignement *pr = ue->prerequis[i];
  68. printf("%d) %s (%dc)\n", i, pr->nom, pr->credits);
  69. }
  70. }
  71. }
  72.  
  73. void ajouter_prerequis(struct unite_enseignement *ue1, struct unite_enseignement *ue2) {
  74. if (ue1 == NULL || ue2 == NULL) {
  75. return;
  76. }
  77.  
  78. int first_null = premier_libre(ue2->prerequis, 5);
  79.  
  80. if (first_null == 5) {
  81. return;
  82. }
  83.  
  84. ue2->prerequis[first_null] = ue1;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement