Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct unite_enseignement {
- char identifiant[10 + 1];
- char nom[200 + 1];
- int credits;
- char responsable[60 + 1];
- struct unite_enseignement *prerequis[5];
- struct unite_enseignement *corequis[5];
- };
- void encoder_ue(void);
- void print_prerequis(struct unite_enseignement *ue);
- void ajouter_prerequis(struct unite_enseignement *ue1, struct unite_enseignement *ue2);
- /**
- * Renvoie l'index du premier élément libre ou length si tab est plein.
- */
- static int premier_libre(struct unite_enseignement *tab[], int length) {
- for (int i = 0; i < length; i++) {
- if (tab[i] == NULL)
- return i;
- }
- return length;
- }
- int main() { encoder_ue(); }
- void encoder_ue() {
- struct unite_enseignement u;
- strcpy(u.identifiant, "AAA-1");
- strcpy(u.nom, "unite_enseignement 1");
- u.credits = 20;
- strcpy(u.responsable, "responsable 1");
- for(int i = 0; i < 5; i++) {
- u.corequis[i] = NULL;
- u.prerequis[i] = NULL;
- }
- struct unite_enseignement u2;
- strcpy(u2.identifiant, "BBB-2");
- strcpy(u2.nom, "unite_enseignement 2");
- u2.credits = 30;
- strcpy(u2.responsable, "responsable 2");
- for(int i = 0; i < 5; i++) {
- u2.corequis[i] = NULL;
- u2.prerequis[i] = NULL;
- }
- print_prerequis(&u2);
- ajouter_prerequis(&u, &u2);
- print_prerequis(&u2);
- }
- void print_prerequis(struct unite_enseignement *ue) {
- printf("--------- Prérequis de %s ---------\n", ue->nom);
- int max_index = premier_libre(ue->prerequis, 5);
- if (max_index == 0) {
- printf("Aucune.\n");
- } else {
- for (int i = 0; i < max_index; i++) {
- struct unite_enseignement *pr = ue->prerequis[i];
- printf("%d) %s (%dc)\n", i, pr->nom, pr->credits);
- }
- }
- }
- void ajouter_prerequis(struct unite_enseignement *ue1, struct unite_enseignement *ue2) {
- if (ue1 == NULL || ue2 == NULL) {
- return;
- }
- int first_null = premier_libre(ue2->prerequis, 5);
- if (first_null == 5) {
- return;
- }
- ue2->prerequis[first_null] = ue1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement