• Source
    1. /**
    2.   Scrivere una function C che ha come input i dati che identificano uno studente (nome,
    3.   cognome, matricola) e che restituisce in output una struttura dati opportuna, che
    4.   contiene i dati di identificazione e il libretto universitario con al massimo 20 esami.
    5.   Ogni esame è caratterizzato da denominazione, cfu e voto.
    6. **/
    7. #include <stdio.h>
    8. #include <stdlib.h>
    9. #include <string.h>
    10.  
    11. // DEFINISCO LE STRUCT
    12.  
    13. struct libretto {
    14. char desame[10];
    15. int cfu;
    16. int voto;
    17. };
    18. typedef struct libretto libretto;
    19.  
    20. struct studente{
    21. char nome[10];
    22. char cognome[10];
    23. char matricola[10];
    24. int esami_svolti;
    25. libretto esami[20];
    26. };
    27. typedef struct studente studente;
    28.  
    29. void stampa(char a[],char b[],char c[], studente studenti[]) {
    30. int i=0,n=10,ie=0;
    31. // Controllo in tutti i record
    32. for (i=0;i<n;i++) {
    33. // Quando le credenziali inserite corrispondono al record all'interno dell'array
    34. if( strcmp(a,studenti[i].nome)==0 && strcmp(b,studenti[i].cognome)==0 && strcmp(c,studenti[i].matricola)==0) {
    35. // Ristampo le credenziali
    36. printf("Nome : %s\n",studenti[i].nome);
    37. printf("Cognome : %s\n",studenti[i].cognome);
    38. printf("Matricola : %s\n",studenti[i].matricola);
    39. // E gli esami svolti
    40. for(ie=0;ie<studenti[i].esami_svolti;ie++) {
    41. printf("Denominazione Esame : %s\n",studenti[i].esami[ie].desame);
    42. printf("CFU : %d\n",studenti[i].esami[ie].cfu);
    43. printf("VOTO : %d\n",studenti[i].esami[ie].voto);
    44. }
    45. // Trovato lo studente, brecko e interrompo il for di ricerca
    46. break;
    47. }
    48. }
    49. }
    50.  
    51. int main () {
    52. // Inizializzo array con i record definiti dalle struct
    53.  
    54. studente studenti[10];
    55.  
    56. // strcpy per le stringhe e = per valori numerici
    57.  
    58. strcpy(studenti[0].nome, "Antonio");
    59.  
    60. strcpy(studenti[0].cognome,"Lee");
    61. strcpy(studenti[0].matricola,"800023");
    62. studenti[0].esami_svolti=2;
    63.  
    64. strcpy(studenti[0].esami[0].desame,"PROG1");
    65. studenti[0].esami[0].cfu = 12;
    66. studenti[0].esami[0].voto = 24;
    67. strcpy(studenti[0].esami[1].desame,"ARCH1");
    68. studenti[0].esami[1].cfu = 12;
    69. studenti[0].esami[1].voto = 25;
    70.  
    71. // Definisco gli array di char che manderò nella procedura
    72.  
    73. char a[10],b[10],c[10];
    74. // Leggo i dati
    75. printf("Inserisci il nome dello studente:\n");
    76. scanf("%s",a);
    77. printf("Inserisci il cognome:\n");
    78. scanf("%s",b);
    79. printf("Inserisci la matricola");
    80. scanf("%s",c);
    81.  
    82. // LI mando nella procedura
    83. stampa(a,b,c,studenti);
    84.  
    85. }
    86.  
    87.