Hyluss

Operacje na plikach, struktura

Jul 2nd, 2016
461
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. //wczytywanie z pliku, zapis do pliku, struktura
  2.  
  3. // ciezary.cpp : Defines the entry point for the console application.
  4. //
  5.  
  6. #include "stdafx.h"
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #define _CRT_SECURE_NO_DEPRECATE
  12.  
  13.  
  14. struct struct_Ciezarki {
  15. float Waga;
  16. char Imie [20];
  17. int Powt;
  18. float Srednia;
  19. };
  20.  
  21. int _tmain(int argc, _TCHAR* argv[])
  22. {
  23.     FILE *wczytaj;
  24.     FILE *zapisz;
  25.    
  26. int  i=0;
  27. int porownanie=0;
  28. int Powt = 1;
  29.    
  30. struct_Ciezarki Ciezarki [100];
  31.  
  32.     wczytaj = fopen ("wciezary.txt","r");
  33.     if (wczytaj == NULL) {
  34.     perror ("blad");
  35.     exit(1);
  36.     }
  37.  
  38. while (!feof (wczytaj)) {
  39.  
  40. fscanf (wczytaj,"%s %f", &Ciezarki[i].Imie, &Ciezarki[i].Waga);
  41. Ciezarki[i].Powt = 1;
  42. printf ("%s %.2f \n", Ciezarki[i].Imie, Ciezarki[i].Waga);
  43.  
  44. for (int j=0; j<i;j++) {
  45.     porownanie = strcmp (Ciezarki[i].Imie, Ciezarki[j].Imie);
  46.    
  47.     if (0 == porownanie) {
  48.         Ciezarki[j].Waga = Ciezarki[i].Waga + Ciezarki[j].Waga;
  49.         Ciezarki[j].Powt++;
  50.         i--;
  51.         }
  52.  
  53. }
  54.  
  55. i++;
  56. }
  57.  
  58.  
  59. zapisz = fopen ("zciezary.txt","w");
  60. if (zapisz == NULL) {
  61. perror ("blad");
  62. fclose (wczytaj);
  63. exit (1);
  64. }
  65.  
  66.  
  67. for (int j=0; j<i;j++) {
  68. Ciezarki[j].Srednia = Ciezarki[j].Waga / Ciezarki[j].Powt;
  69. printf ("%s podniosl srednio %.2f \n", Ciezarki[j].Imie,Ciezarki[j].Srednia);
  70. fprintf (zapisz,"%s podniosl srednio %.2f \n", Ciezarki[j].Imie,Ciezarki[j].Srednia);
  71. }
  72.  
  73.  
  74.     _getch();
  75.     return 0;
  76. }
  77.  
  78. /* tak wyglada "wciezary.txt"
  79. Adam 10
  80. Adam 5
  81. Lukas 14
  82. Marcin 99
  83. Adam 7
  84. Marcin 53
  85. Wiktor 51
  86. Darek 3
  87. Wiktor 4
  88. Darek 5
  89. Darek 6
  90. */
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment