Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 KB | None | 0 0
  1. /* Crea un programa per realitzar la gestió dels socis
  2.  * d'un club. L'estructura del soci té id_soci, nom i
  3.  * edat.
  4. El programa tindrà les següents funcions:
  5.     • Alta soci
  6.     • Consulta un soci
  7.     • Consulta massiva (Mostra tots els socis)
  8.     • Esborra soci
  9.     • Ordenar per número
  10.     • Ordenar per inicial del nom
  11.     • Ordenar per edat
  12.     • Eliminar tots els socis
  13.     • Sortir
  14. Soci serà un vector de 40 posicions.
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. #define MAX 40
  22.  
  23. typedef struct {
  24.     int id_soci;
  25.     char nom[50];
  26.     int edat;
  27. } Soci;
  28.  
  29. void inicialitzar_club(Soci socis[]) {
  30.     int i;
  31.  
  32.     for (i = 0; i < MAX; i++) {
  33.         socis[i].id_soci = -2;
  34.     }
  35. }
  36.  
  37. int id_buit(Soci socis[]) {
  38.     int i;
  39.  
  40.     for (i = 0; i < MAX; i++) {
  41.         if (socis[i].id_soci == -2) {
  42.             return i;
  43.         }
  44.     }
  45.  
  46.     return -1;
  47. }
  48.  
  49. void alta_soci(Soci socis[]) {
  50.     int nou_id = id_buit(socis);
  51.  
  52.     if (nou_id != -1) {
  53.         socis[nou_id].id_soci = nou_id;
  54.  
  55.         setbuf(stdin, NULL);
  56.         printf("Introdueix el nom del soci: ");
  57.         gets(socis[nou_id].nom);
  58.  
  59.         setbuf(stdin, NULL);
  60.         printf("Introdueix l'edat del soci: ");
  61.         scanf("%d", &socis[nou_id].edat);
  62.  
  63.     } else {
  64.         printf("El club esta ple :( \n");
  65.     }
  66.  
  67. }
  68.  
  69. void consulta_soci(Soci socis[]) {
  70.     int id;
  71.  
  72.     printf("Introdueix l'id del soci: ");
  73.     scanf("%d", &id);
  74.  
  75.     // Comprovem que existeix el id_soci
  76.  
  77.     if (socis[id].id_soci >= 0) {
  78.         printf("Nom del soci: %s\n", socis[id].nom);
  79.         printf("L'edat del soci: %d\n", socis[id].edat);
  80.     } else {
  81.         printf("No existeix un soci amb aquest id\n");
  82.     }
  83. }
  84.  
  85. void mostrar_socis(Soci socis[]) {
  86.     int i;
  87.  
  88.     for (i = 0; i < MAX; i++) {
  89.         if (socis[i].id_soci >= 0) {
  90.             printf("Id del soci: %d\n", socis[i].id_soci);
  91.             printf("Nom del soci: %s\n", socis[i].nom);
  92.             printf("L'edat del soci: %d\n", socis[i].edat);
  93.         }
  94.     }
  95. }
  96.  
  97. int main() {
  98.     Soci socis[MAX];
  99.     inicialitzar_club(socis);
  100.  
  101.     int opcio;
  102.  
  103.     do {
  104.         printf("\n\nMENU CLUB\n");
  105.         printf("---------\n");
  106.         printf("1. Alta soci\n");
  107.         printf("2. Consulta un soci\n");
  108.         printf("3. Mostra tots els socis\n");
  109.         printf("4. Esborra soci\n");
  110.         printf("5. Ordenar per numero de soci\n");
  111.         printf("6. Ordenar per inicial del nom\n");
  112.         printf("7. Ordenar per edat\n");
  113.         printf("8. Eliminar tots els socis\n");
  114.         printf("9. Sortir\n\n");
  115.         printf("Introdueix la teva opcio: ");
  116.         scanf("%d", &opcio);
  117.  
  118.         switch (opcio) {
  119.             case 1: alta_soci(socis);
  120.                 break;
  121.             case 2: consulta_soci(socis);
  122.                 break;
  123.             case 3: mostrar_socis(socis);
  124.                 break;
  125.             case 4: //esborrar_soci(socis);
  126.                 break;
  127.             case 5: //ordenar_numero(socis);
  128.                 break;
  129.             case 6: //ordenar_inicial(socis);
  130.                 break;
  131.             case 7: //ordenar_edat(socis);
  132.                 break;
  133.             case 8: //eliminar_tots(socis);
  134.                 break;
  135.         }
  136.  
  137.     } while (opcio > 0 && opcio < 9);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement