Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct student
  5. {
  6.     char nume[40];
  7.     char sex;
  8.     float nota;
  9. }STUDENT;
  10. void citire(FILE *f,STUDENT s[50],int n) //subpunctul a
  11. {
  12.     int i;
  13.     if((f=fopen("311AC.bin","wb"))==NULL)
  14.     {
  15.         printf("Fisierul nu poate fi deschis.");
  16.         exit(1);
  17.     }
  18.     for(i=0;i<n;i++)
  19.     {
  20.         printf("Numele elevului nr %d este:",i+1);
  21.         fflush(stdin);
  22.         scanf("%s",s[i].nume);
  23.  
  24.         printf("Sexul elevului este (M/F):");
  25.         fflush(stdin);
  26.         scanf("%c",&s[i].sex);
  27.  
  28.         printf("Nota elevului este:");
  29.         fflush(stdin);
  30.         scanf("%f",&s[i].nota);
  31.     }
  32.  
  33.     fwrite(s,sizeof(STUDENT),n,f);
  34.     fclose(f);
  35. }
  36. int cautare(char pers[20],STUDENT s[50],int n) //subpunctul b
  37. {
  38.     int i,ok=0;
  39.     for(i=0;i<n;i++)
  40.         if(strcmp(s[i].nume,pers)==0)
  41.             ok++;
  42.     return ok;
  43.  
  44. }
  45. void nota(STUDENT s[50],int n,int nr_std[2])
  46. {
  47.     int i;
  48.     float max=0.0;
  49.     for(i=0;i<n;i++)
  50.         if(s[i].nota>max)
  51.             max=s[i].nota;
  52.     int f=0,b=0;
  53.     for(i=0;i<n;i++)
  54.         if(s[i].nota==max)
  55.             if(s[i].sex=='F')
  56.                 f++;
  57.             else
  58.                 b++;
  59.     nr_std[0]=f;
  60.     nr_std[1]=b;
  61.  
  62. }
  63. int main()
  64. {
  65.     FILE *f;
  66.     STUDENT s[100];
  67.     int n,i,ok=0,nr_std[2];
  68.     char pers[50];
  69.  
  70.     printf("Tastati numarul de studenti:");
  71.     scanf("%d",&n);
  72.  
  73.     citire(f,s,n);
  74.  
  75.     if((f=fopen("311AC.bin","rb"))==NULL)
  76.     {
  77.         printf("Fisierul nu poate fi deschis.");
  78.         exit(1);
  79.     }
  80.     fread(s,sizeof(STUDENT),n,f);
  81.  
  82.     printf("Tastati numele elevui cautat:");
  83.     fflush(stdin);
  84.     scanf("%s",pers);
  85.  
  86.     ok=cautare(pers,s,n);
  87.  
  88.     if(ok==0)
  89.         printf("Nu exista.\n");
  90.     else
  91.         printf("Exista.\n");
  92.  
  93.     fclose(f);
  94.     if((f=fopen("311AC.bin","rb"))==NULL)
  95.     {
  96.         printf("Fisierul nu poate fi deschis.");
  97.         exit(1);
  98.     }
  99.     fread(s,sizeof(STUDENT),n,f);
  100.  
  101.     nota(s,n,nr_std);
  102.  
  103.     printf("Numarul de fete care au obtinut nota maxima este %d, iar nr de baieti care au obtinut nota maxima este %d.",nr_std[0],nr_std[1]);
  104.    
  105.    return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement