Facebook
From Innocent Tamarin, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 250
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct student
  5. {
  6.         char nazwisko[30];
  7.         char imie[20];
  8.         int numer;
  9.         double oceny[5];
  10.         double sr;
  11. };
  12.  
  13. void dopisz();
  14. void drukuj(struct student);
  15. void drukuj_plik();
  16. struct student srednia(struct student);
  17. void popraw();
  18. void maksimum();
  19. void usun();
  20.  
  21. main()
  22. {
  23.         int znak;
  24.         while(1)
  25.         {
  26.         printf("\t\t\tMenu\n");
  27.         printf("----------------------------------------------------\n");
  28.         printf("d - dopisz element\t\tu - usun studenta\n");
  29.         printf("w - drukuj plik\n");
  30.         printf("p - popraw\n");
  31.         printf("m - maksymalna srednia\t\tn - minimalna srednia\n");
  32.         printf("c - wyczysc ekran\n");
  33.         printf("q - wyjdz\n");
  34.         printf("----------------------------------------------------\n");
  35.  
  36.         znak = getchar();
  37.         switch (znak)
  38.                 {
  39.                 case 'd' :      dopisz();
  40.                                         break;
  41.                 case 'u' :  usun();
  42.                                         break;
  43.                 case 'w' :      drukuj_plik();
  44.                                         break;
  45.                 case 'p' :  popraw();
  46.                                         break;
  47.                 case 'm' :  maksimum();
  48.                                         break;
  49.                 case 'c' :  system("cls");
  50.                                         break;
  51.                 case 'q' :      return 0;
  52.                 default  :      printf("Bledny znak!\n");
  53.                 }
  54.         fflush(stdin);
  55.         }
  56. }
  57.  
  58.  
  59.  
  60. void dopisz()
  61. {
  62.         FILE *plik;
  63.         struct student add;
  64.         int i;
  65.         double o;
  66.  
  67.         plik = fopen("dane.bin","ab");
  68.         if (plik == NULL) printf("Plik nie istnieje\n");
  69.         else
  70.         {
  71.                 printf("\nPodaj nazwisko\n"); fflush(stdin); scanf("%29s",       add.nazwisko);        
  72.                 printf("Podaj imie\n");         fflush(stdin); scanf("%19s",     add.imie);
  73.                 printf("Podaj numer\n");        fflush(stdin); scanf("%d",              &add.numer);
  74.                 printf("Podaj oceny [5]\n");   
  75.                         for(i=0; i<5; i++)
  76.                         {
  77.                                 printf("Ocena [%d]\t\t", (i+1));
  78.                                 fflush(stdin);
  79.                                 scanf("%lg", &o);
  80.                                 if (o<7 && o>0) add.oceny[i] = o;
  81.                                 else
  82.                                 {
  83.                                         printf("Uwaga: Przekroczony zakres!\n");
  84.                                         i--;
  85.                                 }
  86.                         }
  87.                 add=srednia(add);
  88.                 fwrite(&add, sizeof(struct student), 1, plik);
  89.                 fclose(plik);
  90.         }
  91.        
  92. }
  93.  
  94. void drukuj(struct student print)
  95. {
  96.         int j;
  97.  
  98.         printf("Nazwisko:\t%s\t",       print.nazwisko);       
  99.         printf("Numer:\t\t%d\n",        print.numer);
  100.         printf("Imie:\t\t%s\t",         print.imie);
  101.         printf("Oceny:\t\t");
  102.         for (j=0; j<5; j++) printf("%g, ", print.oceny[j]);
  103.         printf("\nSrednia:\t%g\n",print.sr);
  104.         printf("\n");
  105. }
  106.  
  107. void drukuj_plik()
  108. {
  109.         int i=1;
  110.         struct student druk;
  111.         FILE *plik;
  112.  
  113.         plik = fopen("dane.bin", "rb");
  114.         if (plik == NULL) printf("Plik nie istnieje\n");
  115.         else
  116.         {
  117.                        
  118.                         while(fread(&druk,sizeof(struct student),1,plik)!=0)
  119.                         {                                                      
  120.                                 drukuj(druk);
  121.                                 i++;
  122.                         }
  123.                         fclose(plik);
  124.         }
  125. }
  126.  
  127. struct student srednia(struct student sred)
  128. {
  129.         int i;
  130.         double s=0;
  131.  
  132.         for(i=0; i<5; i++) s+=sred.oceny[i];
  133.         sred.sr=s/5;
  134.  
  135.         return sred;
  136. }
  137.  
  138. void popraw()
  139. {
  140.         char im[20], nazw[30];
  141.         int i,num;
  142.         struct student spr;
  143.         FILE *plik;
  144.         FILE *tmp;
  145.  
  146.         printf("\nPodaj nazwisko ucznia:\t");   fflush(stdin);  scanf("%29[^\n]s", nazw);
  147.         printf("Podaj imie ucznia:\t");                 fflush(stdin);  scanf("%19[^\n]s", im);
  148.         printf("Podaj numer ucznia:\t");                fflush(stdin);  scanf("%d", &num);
  149.        
  150.         tmp  = fopen("tmp.bin", "wb");
  151.         plik = fopen("dane.bin", "r+b");
  152.  
  153.         while(fread(&spr, sizeof(struct student), 1, plik)!=0)
  154.         {
  155.                 if (!strcmp(nazw, spr.nazwisko) && !strcmp(im, spr.imie) && num == spr.numer)
  156.                 {
  157.                         printf("\nSzukany student:\n");
  158.                         drukuj(spr);
  159.  
  160.                         printf("Podaj nowe dane:");
  161.                         printf("\nPodaj nazwisko\t");   fflush(stdin); scanf("%29s",     spr.nazwisko);        
  162.                         printf("Podaj imie\t");                 fflush(stdin); scanf("%19s",     spr.imie);
  163.                         printf("Podaj numer\t");                fflush(stdin); scanf("%d",              &spr.numer);
  164.                         printf("Podaj oceny [5]\n");   
  165.                         for(i=0; i<5; i++)
  166.                         {
  167.                                 printf("Ocena %d\t\t", (i+1));
  168.                                 fflush(stdin);
  169.                                 scanf("%lg",    &spr.oceny[i]);
  170.                         }
  171.                         spr=srednia(spr);
  172.  
  173.                         fwrite(&spr, sizeof(struct student), 1, tmp);
  174.                         continue;
  175.                 }
  176.                 else
  177.                 {
  178.                         fwrite(&spr, sizeof(struct student), 1, tmp);
  179.                 }              
  180.         }
  181.  
  182.         fclose(plik);
  183.         remove("dane.bin");
  184.  
  185.         fclose(tmp);
  186.         rename("tmp.bin", "dane.bin");
  187.        
  188. }
  189.  
  190. void usun()
  191. {
  192.         char im[20], nazw[30];
  193.         int num;
  194.         struct student us;
  195.         FILE *plik;
  196.         FILE *tmp;
  197.  
  198.         printf("\nPodaj nazwisko ucznia:\t");   fflush(stdin);  scanf("%29[^\n]s", nazw);
  199.         printf("Podaj imie ucznia:\t");                 fflush(stdin);  scanf("%19[^\n]s", im);
  200.         printf("Podaj numer ucznia:\t");                fflush(stdin);  scanf("%d", &num);
  201.        
  202.         tmp  = fopen("tmp.bin", "wb");
  203.         plik = fopen("dane.bin", "r+b");
  204.  
  205.         while(fread(&us, sizeof(struct student), 1, plik)!=0)
  206.         {
  207.                 if (!strcmp(nazw, us.nazwisko) && !strcmp(im, us.imie) && num == us.numer)
  208.                 {
  209.                         printf("\nUsuniety student:\n");
  210.                         drukuj(us);
  211.                         continue;
  212.                 }
  213.                 else
  214.                 {
  215.                         fwrite(&us, sizeof(struct student), 1, tmp);
  216.                 }              
  217.         }
  218.  
  219.         fclose(plik);
  220.         remove("dane.bin");
  221.  
  222.         fclose(tmp);
  223.         rename("tmp.bin", "dane.bin");
  224.        
  225. }
  226.  
  227. void maksimum()
  228. {
  229.         struct student stmp;
  230.         double max=0;
  231.         FILE *plik;
  232.        
  233.         plik = fopen("dane.bin", "r");
  234.         while(fread(&stmp, sizeof(struct student), 1, plik)!=0)
  235.         {
  236.                 if (stmp.sr > max) max = stmp.sr;
  237.         }
  238.         fclose(plik);
  239.         printf("%lg\n", max);
  240. }
  241.  
  242. void minimum()
  243. {
  244.         struct student stmp;
  245.         double min=10;
  246.         FILE *plik;
  247.        
  248.         plik = fopen("dane.bin", "r");
  249.         while(fread(&stmp, sizeof(struct student), 1, plik)!=0)
  250.         {
  251.                 if (stmp.sr < min) min = stmp.sr;
  252.         }
  253.         fclose(plik);
  254.         printf("%lg\n", min);
  255. }