Advertisement
Guest User

PO-Lab1_Pliki_28.02.17

a guest
Feb 28th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <malloc.h>
  5.  
  6. struct Student {
  7.     char imie[30];
  8.     char nazwisko[50];
  9.     int ocena[15];
  10.     int suma;
  11.     float procent;
  12. };
  13.  
  14.  
  15. int main()
  16. {
  17.     FILE* oceny_lab = fopen("oceny_lab.txt", "r");
  18.     int l_studentow, l_ocen, count = 0;
  19.  
  20.     if (oceny_lab) {
  21.         fscanf(oceny_lab, "%d %d", &l_studentow, &l_ocen);
  22.  
  23.         Student *s = (Student*)malloc(l_studentow * sizeof(Student));       // Tablica struktur
  24.  
  25.         FILE* oceny_stud = fopen("oceny_stud.txt", "r");
  26.  
  27.         if (oceny_stud) {
  28.             while (!feof(oceny_stud)) {
  29.                 fscanf(oceny_stud, "%s %s", &s[count].imie, &s[count].nazwisko);    // Wczytanie imion i nazwisk
  30.                 s[count].suma = 0;                                                  // Zainicjowanie sumy dla kazdego studenta
  31.                 ++count;
  32.             }
  33.  
  34.                     count = 0;                                                      // Wyzerowanie licznika!
  35.  
  36.             while (!feof(oceny_lab)) {
  37.                 for (int i = 0; i < l_ocen; ++i) {
  38.                     fscanf(oceny_lab, "%d", &s[count].ocena[i]);                    // Wczytanie ocen dla studenta
  39.                     s[count].suma += s[count].ocena[i];                             // Liczenie sumy dla studenta
  40.                 }
  41.                 ++count;
  42.             }
  43.  
  44.             FILE* oceny_out = fopen("oceny_out.txt", "w+");
  45.  
  46.             if (oceny_out) {
  47.                 for (int i = 0; i < l_studentow; ++i) {
  48.                     s[i].procent = (s[i].suma * 100) / (l_ocen * 10);               // Obliczenie oceny procentowej studenta
  49.  
  50.                     fprintf(oceny_out, "%s %s %d %.2f \n", s[i].imie, s[i].nazwisko, s[i].suma, s[i].procent);  // Wypisanie do pliku
  51.                 }
  52.             }
  53.             else printf("Blad podczas zapisywania danych do pliku! \n");
  54.  
  55.             fclose(oceny_out);
  56.         }
  57.         else printf("Blad podczas wczytywania danych z pliku! \n");
  58.  
  59.         fclose(oceny_stud);
  60.     }
  61.     else printf("Blad podczas wczytywania danych z pliku! \n");
  62.  
  63.     fclose(oceny_lab);
  64.  
  65.     printf("Dane zapisano w pliku oceny_out.txt! \n");
  66.     _getch();
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement