DanieleCalisti

2 Intercorso 2020

Dec 16th, 2022
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | Software | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct{
  6.     int matricola;
  7.     char cognome[10];
  8.     char nome[10];
  9.     float mediaVoti;
  10. }studente;
  11.  
  12. typedef struct{
  13.     int matricola;
  14.     float mediaVoti;
  15. }newStudente;
  16.  
  17. int update (studente *);
  18. int cmpRec(char **, studente);
  19. void copyRec(studente, newStudente **);
  20. void stampa (studente*, newStudente **);
  21.  
  22. int main()
  23. {
  24.     studente uno;
  25.     newStudente *copiaUno;
  26.     char *elenco[]={"Bianchi","Rossi","Verde"};
  27.     if(update(&uno))
  28.     {
  29.         if(cmpRec(elenco,uno))
  30.         {
  31.             printf("Lo studente %s รจ incluso nell'elenco\n",uno.cognome);
  32.             copyRec(uno, &copiaUno);
  33.             stampa(&uno, &copiaUno);
  34.         }
  35.         else
  36.             printf("Nessuno studente trovato, copia non effettuata\n");
  37.     }
  38.     else
  39.         printf("Nessun dato inserito\n");
  40.        
  41.     return 0;
  42. }
  43.  
  44. int update(studente* s)
  45. {
  46.     int scelta;
  47.    
  48.     do{
  49.         printf("Si desidera inserire un nuovo alunno? [1-si/0-no]");
  50.         scanf("%d", &scelta);
  51.     }while(scelta != 1 && scelta != 0);
  52.    
  53.     if(scelta)
  54.     {
  55.         printf("Inserisci matricola: ");
  56.         scanf("%d", &s->matricola);
  57.        
  58.         printf("Inserisci nome: ");
  59.         scanf("%s", s->nome);
  60.        
  61.         printf("Inserisci cognome: ");
  62.         scanf("%s", s->cognome);
  63.        
  64.         printf("Inserisci media voti: ");
  65.         scanf("%f", &s->mediaVoti);
  66.     }
  67.    
  68.     return scelta;
  69. }
  70.  
  71. int cmpRec(char **elenco, studente s)
  72. {
  73.     int i;
  74.    
  75.     for(i=0;i<3;i++)
  76.     {
  77.         if(!strcmp( *(elenco + i), s.cognome))
  78.             return 1;
  79.     }
  80.    
  81.     return 0;
  82. }
  83.  
  84. void copyRec(studente s, newStudente ** n)
  85. {
  86.     (*n) = malloc(sizeof(newStudente));
  87.    
  88.     (*n)->mediaVoti = s.mediaVoti;
  89.     (*n)->matricola = s.matricola;
  90. }
  91.  
  92. void stampa (studente *s, newStudente ** n)
  93. {
  94.     printf("Nome: %s, Cognome: %s, Matricola: %d, Media Voti: %f", s->nome, s->cognome, (*n)->matricola, (*n)->mediaVoti);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment