Hyluss

Miasto opady, Operacje na plikach

Jul 2nd, 2016
601
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. // operacje na plikach, struktura
  2.  
  3. // Miasta.cpp : Defines the entry point for the console application.
  4. //
  5. #define _CRT_SECURE_NO_DEPRECATE
  6. #include "stdafx.h"
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <conio.h>
  10. #include <cstdio>
  11. #include <string.h>
  12.  
  13. struct struct_Miasto
  14. {
  15.     char nazwa[100];
  16.     float opady;
  17.     int numer;
  18. };
  19.  
  20. int _tmain(int argc, _TCHAR* argv[])
  21. {
  22.  
  23. char porownanie;
  24. int i=0;
  25.  
  26. struct_Miasto Miasto [100];
  27.  
  28. FILE *wczytaj;
  29. FILE *zapisz;
  30.  
  31. wczytaj = fopen ("wopady.txt","r");
  32. if (NULL==wczytaj)
  33. {
  34.     perror ("blad");
  35.     exit (1);
  36. }
  37.  
  38. while (!feof (wczytaj)) {
  39.  
  40. fscanf (wczytaj," %s %f", &Miasto[i].nazwa, &Miasto[i].opady);
  41. printf ("%s %.2f \n",Miasto[i].nazwa, Miasto[i].opady);
  42.  
  43.     for (int j=0;j<i;j++)
  44.     {
  45.         porownanie = strcmp (Miasto[i].nazwa, Miasto[j].nazwa);
  46.  
  47.         if (0 == porownanie)
  48.         {
  49.            
  50.             if ((Miasto[i].opady) >= (Miasto[j].opady))
  51.             {
  52.                 Miasto[j].opady = Miasto[i].opady;
  53.            
  54.             }
  55.         i--;   
  56.        
  57.         }  
  58.     } // for
  59.  
  60.     i++;
  61.  
  62.     }
  63.  
  64. zapisz = fopen ("zopady.txt","w");
  65. if (zapisz == NULL )
  66. {
  67.     perror ("blad");
  68.     fclose (wczytaj);
  69.     exit (1);
  70. }
  71.  
  72.     for (int j=0; j<i; j++)
  73.     {
  74.         fprintf (zapisz,"%s %.2f \n", Miasto[j].nazwa,Miasto[j].opady);
  75.         printf ("zapisalem %s %.2f \n", Miasto[j].nazwa,Miasto[j].opady);
  76.  
  77.     }
  78.  
  79. fclose (zapisz);
  80. fclose (wczytaj);
  81.  
  82.     _getch ();
  83.     return 0;
  84. }
  85.  
  86. /* tak wygalda plik "wopady.txt"
  87. Katowice 2.02
  88. Gliwice 1.5
  89. Bytom 5.5
  90. Zabrze 4
  91. Katowice 4.4
  92. Bytom 1.5
  93. Gliwcie 7
  94. Bytom 1.23
  95. Bytom 9.2
  96. */
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment