Florii11

p1

Feb 23rd, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NUMBER 3
  5.  
  6. typedef struct
  7. {
  8.     char*nume;
  9.     char*prenume;
  10.     int varsta;
  11.     int nota[NUMBER];
  12.     float medie;
  13.  
  14. } STUDENT;
  15.  
  16. STUDENT conversie(char*nume, char*prenume, int varsta, int note[NUMBER])
  17. {
  18.     STUDENT s;
  19.     s.nume=nume;
  20.     s.prenume=prenume;
  21.     s.varsta=varsta;
  22.     for(int i=0; i<NUMBER; i++)
  23.         s.nota[i]=note[i];
  24.     return s;
  25. }
  26.  
  27. void incrementare(STUDENT *s)
  28. {
  29.     for(int i=0; i<NUMBER; i++)
  30.     {
  31.         if(s->nota[i]<10)
  32.         s->nota[i]++;
  33.     }
  34. }
  35.  
  36. void afisare(STUDENT s)
  37. {
  38.     printf("%s %s %d %d %d\n", s.nume, s.prenume, s.nota[0], s.nota[1],s.nota[2]);
  39. }
  40.  
  41. int main()
  42. {
  43.     FILE* f=fopen("nume.txt","r");
  44.     if(f==NULL)
  45.         return -1;
  46.     int n;
  47.     fscanf(f,"%d",&n);
  48.     printf("%d\n",n);
  49.  
  50.     STUDENT *lista=calloc(n,sizeof(STUDENT));
  51.  
  52.  
  53.     for(int i=0; i<n; i++)
  54.     {
  55.         char* nume = malloc(20*sizeof(char));
  56.         char* prenume = malloc(20*sizeof(char));
  57.         int varsta;
  58.         fscanf(f,"%s %s %d",nume, prenume, &varsta);
  59.         int note[NUMBER];
  60.         for(int j=0; j<NUMBER; j++)
  61.             fscanf(f,"%d", &note[j]);
  62.         STUDENT s=conversie(nume,prenume,varsta,note);
  63.         lista[i]=s;
  64.         lista[i].medie=(float)(note[0]+note[1]+note[2])/NUMBER;
  65.         printf("%s %s %f\n", lista[i].nume, lista[i].prenume, lista[i].medie);
  66.     }
  67.  
  68.     for(int j=0; j<n; j++)
  69.     {
  70.         incrementare(&lista[j]);
  71.         afisare(lista[j]);
  72.     }
  73.  
  74.     return 0;
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment