bartek27210

struktury

Jan 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  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 srednia;
  11. };
  12.  
  13. void dopisz(char *nazwa);
  14. struct student srednia(struct student s);
  15. void drukuj(struct student s);
  16. void wydruk(char *nazwa);
  17. //double minimium(char*nazwa);
  18. //double maximum(char*nazwa);
  19. //int usun(char*nazwa);
  20. //struct student*najlepsi(char*nazwa); //i
  21. //void popraw(char*nazwa);
  22.  
  23. int main()
  24. {
  25.  
  26.     char nazwa[20];
  27.     printf("Podaj nazwe pliku: ");
  28.     fflush(stdin);
  29.     scanf("%s", nazwa);
  30.     dopisz(nazwa);
  31.     wydruk(nazwa);
  32. }
  33.  
  34. void dopisz(char*nazwa)
  35. {
  36.     FILE*plik = fopen(nazwa, "w");
  37.     int i = 0;
  38.     struct student s;
  39.     fflush(stdin);
  40.     printf("Podaj nazwisko ");
  41.     scanf("%s", s.nazwisko);
  42.     fflush(stdin);
  43.     printf("Podaj imie ");
  44.     scanf("%s", s.imie);
  45.     fflush(stdin);
  46.     printf("Podaj numer ");
  47.     scanf("%d", &s.numer);
  48.     fflush(stdin);
  49.     for (i = 0; i<5; i++)
  50.     {
  51.         printf("podaj %d ocene ", i);
  52.         scanf("%lf", &s.oceny[i]);
  53.     }
  54.     s = srednia(s);
  55.     fwrite(&s, sizeof(struct student), 1, plik);
  56.     fclose(plik);
  57. }
  58.  
  59. struct student srednia(struct student s)
  60. {
  61.     int i = 0;
  62.     double suma = 0;
  63.     for (i = 0; i<5; i++)
  64.     {
  65.         suma += *(s.oceny + i);
  66.     }
  67.     s.srednia=suma/5;
  68.     return s;
  69. };
  70.  
  71. void drukuj(struct student s)
  72. {
  73.     printf("\nLista");
  74.     printf("\nNazwisko: %s", s.nazwisko);
  75.     printf("\nImie: %s", s.imie);
  76.     printf("\nNumer: %d", s.numer);
  77.     printf("\nSrednia: %lf", s.srednia);
  78. }
  79.  
  80. void wydruk(char *nazwa)
  81. {
  82.     FILE*plik;
  83.     struct student s;
  84.     plik = fopen(nazwa,"r");
  85.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  86.     {
  87.         drukuj(s);
  88.     }
  89. }
Add Comment
Please, Sign In to add comment