----- TP31 : Concepts en vrac ----- 1. #include #include typedef struct { unsigned int durabilite : 10; unsigned int deplacement : 4; unsigned int defense : 7; unsigned int degats : 7; unsigned int distance : 4; } unite; int main(void) { unite test; test.durabilite = 7; test.defense = 7; test.degats = 7; test.deplacement = 7; test.distance = 7; return EXIT_SUCCESS; } 2. #include #include enum wedge { PITCHING = 1, GAP = 2, SAND = 3, LOP = 4, FLIP = 5 }; enum putter { REGULAR = 1, BELLY = 2, LONG = 3 }; typedef struct { char* marque; char* modele; char* categorie; unsigned int sous_categorie : 4; } club; void affiche_catalogue(int taille_catalogue, club* catalogue) { int i; char* wedge[5] = {"Pitching", "Gap", "Sand", "Lop", "Flip"}; char* putter[3] = {"Regular", "Belly", "Long"}; printf("| Marque | Modèle | Catégorie | Sous-catégorie |\n"); printf("------------------------------------------------\n"); for (i = 0; i != taille_catalogue; i++) { if (filtre(catalogue[i])) { printf("| %6s | %6s | %9s | %14d |\n", catalogue[i].marque, catalogue[i].modele, catalogue[i].categorie, catalogue[i].sous_categorie); } } } int filtre(club article) { return article.categorie == "Fer" && article.sous_categorie <= 4; } int main(void) { int i; club catalogue[100]; char* marques[4] = {"A", "B", "C", "D"}; char* modeles[4] = {"M", "N", "O", "P"}; char* categories[4] = {"Bois", "Fer", "Wedge", "Putter"}; srand(time(NULL)); for(i = 0; i != 100; i++) { catalogue[i].marque = marques[rand() % 4]; catalogue[i].modele = modeles[rand() % 4]; catalogue[i].categorie = categories[rand() % 4]; if (catalogue[i].categorie == "Wedge") { catalogue[i].sous_categorie = rand() % 5; } else if (catalogue[i].categorie == "Putter") { catalogue[i].sous_categorie = rand() % 3; } else { catalogue[i].sous_categorie = rand() % 9 + 1; } } affiche_catalogue(100, catalogue); return EXIT_SUCCESS; } 3.