Advertisement
elektryk798

Kartowka_C_3

Jan 22nd, 2017
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4.  
  5. struct student
  6. {
  7.     char nazwisko[30];
  8.     char imie[20];
  9.     int numer;
  10.     double oceny[5];
  11.     double srednia;
  12. };
  13.  
  14. void dopisz(char *nazwa);
  15. struct student srednia(struct student s);
  16. void drukuj(struct student s);
  17. void wydruk(char *nazwa);
  18. double minimium(char*nazwa);
  19. double maximum(char*nazwa);
  20. int usun(char*nazwa);
  21. struct student*najlepsi(char*nazwa);
  22. void popraw(char*nazwa);
  23. //int ilenajlepszych(char*nazwa);
  24.  
  25. int main()
  26. {
  27.     int i,ile;
  28.     struct student *tab;
  29.     char nazwa[20],co;
  30.     printf("Podaj nazwe pliku: ");
  31.     fflush(stdin);
  32.     scanf("%s", nazwa);
  33.     while(1)
  34.     {
  35.         printf("\n\nCo chcesz zrobic?\nd - dodaj studenta\nw - wypisz liste studentow\nu - usun studentow z podanym numerem\np - popraw dane studenta\nn - wypisz studentow z najlepsza srednia\nb - wypisz najlepsza srednia wsrod studentow\nm - wypisz najgorsza srednia wsrod studentow\nx - zakoncz dzialanie programu\n");
  36.         fflush(stdin);
  37.         co=getchar();
  38.         system("cls");
  39.         switch(co)
  40.         {
  41.             case 'd':
  42.                 dopisz(nazwa);
  43.             break;
  44.             case 'm':
  45.                 printf("\nNajgorsza srednia wsrod studentow: %lf",minimium(nazwa));
  46.             break;
  47.             case 'w':
  48.                 wydruk(nazwa);
  49.             break;
  50.             case 'p':
  51.                 popraw(nazwa);
  52.             break;
  53.             case 'n':
  54.                 tab=najlepsi(nazwa);
  55.                 //ile=ilenajlepszych(nazwa);
  56.             break;
  57.             case 'b':
  58.                 printf("\nNajlepsza srednia wsrod wszystkich studentow: %lf",maximum(nazwa));
  59.             break;
  60.             case 'u':
  61.                 printf("\nUsunieto %d studentow",usun(nazwa));
  62.             break;
  63.         }
  64.         if (co=='x')
  65.             break;
  66.     }
  67.     return 0;
  68. }
  69. void dopisz(char*nazwa)
  70. {
  71.     FILE*plik = fopen("plik.txt", "a");
  72.     int i = 0;
  73.     struct student s;
  74.     fflush(stdin);
  75.     printf("Podaj nazwisko ");
  76.     scanf("%s", s.nazwisko);
  77.     fflush(stdin);
  78.     printf("Podaj imie ");
  79.     scanf("%s", s.imie);
  80.     fflush(stdin);
  81.     printf("Podaj numer ");
  82.     scanf("%d", &s.numer);
  83.     fflush(stdin);
  84.     for (i = 0; i<5; i++)
  85.     {
  86.         printf("podaj %d ocene ", i);
  87.         scanf("%lf", &s.oceny[i]);
  88.     }
  89.     s = srednia(s);
  90.     fwrite(&s, sizeof(struct student), 1, plik);
  91.     fclose(plik);
  92. }
  93. struct student srednia(struct student s)
  94. {
  95.     int i = 0;
  96.     double suma = 0;
  97.     for (i = 0; i<5; i++)
  98.     {
  99.         suma += *(s.oceny + i);
  100.     }
  101.     s.srednia=suma/5;
  102.     return s;
  103. };
  104. void drukuj(struct student s)
  105. {
  106.     printf("\n\nNazwisko: %s", s.nazwisko);
  107.     printf("\nImie: %s", s.imie);
  108.     printf("\nNumer: %d", s.numer);
  109.     printf("\nSrednia: %lf\n", s.srednia);
  110. }
  111. void wydruk(char *nazwa)
  112. {
  113.     FILE*plik;
  114.     struct student s;
  115.     plik = fopen("plik.txt","r");
  116.     printf("Lista");
  117.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  118.     {
  119.         drukuj(s);
  120.     }
  121.     fclose(plik);
  122. }
  123. void wydruk2(char *nazwa)
  124. {
  125.     FILE*plik;
  126.     struct student s;
  127.     plik = fopen("plik2.txt","r");
  128.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  129.     {
  130.         drukuj(s);
  131.     }
  132.     fclose(plik);
  133. }
  134. double minimium(char*nazwa)
  135. {
  136.     int i=0;
  137.     double minsrednia;
  138.     FILE*plik = fopen("plik.txt","r");
  139.     struct student s;
  140.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  141.     {
  142.         if(i==0)
  143.             minsrednia=s.srednia;
  144.         if(s.srednia<minsrednia)
  145.             minsrednia=s.srednia;
  146.         i++;
  147.     }
  148.     fclose(plik);
  149.     return minsrednia;
  150. }
  151. int usun(char*nazwa)
  152. {
  153.     int i=0,ile=0,numer;
  154.     fflush(stdin);
  155.     printf("Podaj numer studenta do usuniecia:\n");
  156.     scanf("%d",&numer);
  157.     FILE*plik = fopen("plik.txt","r");
  158.     FILE*plik2 = fopen("plik2.txt", "a");
  159.     struct student s;
  160.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  161.     {
  162.         if(numer!=s.numer)
  163.             fwrite(&s, sizeof(struct student), 1, plik2);
  164.         else
  165.             ile++;
  166.     }
  167.     fclose(plik2);
  168.     fclose(plik);
  169.     remove("plik.txt");
  170.     if(rename("plik2.txt","plik.txt")==0)
  171.         printf("ZMIENIONO NAZWE");
  172.     else
  173.         printf("\nNIE ZMIENIONO NAZWY, BLAD");
  174.     return ile;
  175. }
  176. double maximum(char*nazwa)
  177. {
  178.     int i=0;
  179.     double maxsrednia;
  180.     FILE*plik = fopen("plik.txt","r");
  181.     struct student s;
  182.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  183.     {
  184.         if(i==0)
  185.             maxsrednia=s.srednia;
  186.         if(s.srednia>maxsrednia)
  187.             maxsrednia=s.srednia;
  188.         i++;
  189.     }
  190.     fclose(plik);
  191.     return maxsrednia;
  192. }
  193. void popraw(char*nazwa)
  194. {
  195.     int i,ile=0;
  196.     double minsrednia;
  197.     FILE*plik = fopen("plik.txt","r");
  198.     FILE*plik2 = fopen("plik2.txt", "a");
  199.     struct student s;
  200.     printf("Podaj numer studenta, ktorego dane chcesz zmienic\n");
  201.     scanf("%d",&ile);
  202.     fseek(plik,0,0);
  203.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  204.     {
  205.         if(s.numer==ile)
  206.         {
  207.             fflush(stdin);
  208.             printf("Podaj nazwisko ");
  209.             scanf("%s", s.nazwisko);
  210.             fflush(stdin);
  211.             printf("Podaj imie ");
  212.             scanf("%s", s.imie);
  213.             fflush(stdin);
  214.             printf("Podaj numer ");
  215.             scanf("%d", &s.numer);
  216.             fflush(stdin);
  217.             for (i=0;i<5;i++)
  218.             {
  219.                 printf("podaj %d ocene ", i);
  220.                 scanf("%lf", &s.oceny[i]);
  221.             }
  222.             s = srednia(s);
  223.             fwrite(&s, sizeof(struct student), 1, plik2);
  224.         }
  225.         else
  226.             fwrite(&s, sizeof(struct student), 1, plik2);
  227.     }
  228.     fclose(plik2);
  229.     fclose(plik);
  230.     remove("plik.txt");
  231.     if(rename("plik2.txt","plik.txt")==0)
  232.         printf("ZMIENIONO NAZWE");
  233.     else
  234.         printf("\nNIE ZMIENIONO NAZWY, BLAD");
  235. }
  236. struct student*najlepsi(char*nazwa)
  237. {
  238.     int i=0,j=0,ile=0,czy=0;;
  239.     double maxsrednia;
  240.     struct student *t=NULL;
  241.     FILE*plik = fopen("plik.txt","r");
  242.     struct student s;
  243.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  244.     {
  245.         if(i==0)
  246.         {
  247.             maxsrednia=s.srednia;
  248.             ile++;
  249.             i++;
  250.             continue;
  251.         }
  252.         if(s.srednia==maxsrednia)
  253.             ile++;
  254.         if(s.srednia>maxsrednia)
  255.         {
  256.             maxsrednia=s.srednia;
  257.             ile=1;
  258.         }
  259.     }
  260.     t=malloc(sizeof(struct student)*ile);
  261.     i=0;
  262.     fseek(plik,0,0);
  263.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  264.     {
  265.         if(!ile)
  266.             break;
  267.         if(s.srednia==maxsrednia)
  268.         {
  269.             strcpy(t[i].nazwisko,s.nazwisko);
  270.             strcpy(t[i].imie,s.imie);
  271.             t[i].numer=s.numer;
  272.             for(;j<5;j++)
  273.                 t[i].oceny[j]=s.oceny[j];
  274.             t[i].srednia=s.srednia;
  275.             i++;
  276.         }
  277.     }
  278.     fclose(plik);
  279.     for(i=0;i<ile;i++)
  280.                 {
  281.                     printf("\n\nStudent %d nazwisko: %s",i+1, t[i].nazwisko);
  282.                     printf("\nStudent %d imie: %s",i+1, t[i].imie);
  283.                     printf("\nStudent %d numer: %d",i+1, t[i].numer);
  284.                     printf("\nStudent %d srednia: %lf\n",i+1, t[i].srednia);
  285.                 }
  286.     return t;
  287. }
  288. /*
  289. int ilenajlepszych(char*nazwa)
  290. {
  291.     int i=0,ile=0;
  292.     double maxsrednia;
  293.     struct student s;
  294.     FILE*plik = fopen("plik.txt","r");
  295.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  296.     {
  297.         if(i==0)
  298.         {
  299.             maxsrednia=s.srednia;
  300.             ile++;
  301.             i++;
  302.             continue;
  303.         }
  304.         if(s.srednia==maxsrednia)
  305.             ile++;
  306.         if(s.srednia>maxsrednia)
  307.         {
  308.             maxsrednia=s.srednia;
  309.             ile=1;
  310.         }
  311.     }
  312.     fclose(plik);
  313.     return ile;
  314. }
  315. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement