Hyluss

Operacje na plikach, ryby

Jul 2nd, 2016
670
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. // polow.cpp : Defines the entry point for the console application.
  2. //
  3. #define _CRT_SECURE_NO_DEPRECATE
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <stdlib.h>
  8. #include <cstdio>
  9. #include <cstring>
  10.  
  11.  
  12. struct struct_ryby {
  13.     char gatunek [20];
  14.     float ilosc;
  15. };
  16.  
  17. int _tmain(int argc, _TCHAR* argv[])
  18. {
  19.  
  20.     float c=0;
  21.     int i=0;
  22.     int z=0;
  23.     int wynik;
  24.  
  25.        
  26.     struct_ryby Ryby[100];
  27.    
  28.     FILE *wczytaj;
  29.     FILE *zapisz;
  30.    
  31.     wczytaj = fopen ("wpolow.txt","r");
  32.     if (NULL==wczytaj) {
  33.         perror ("blad");
  34.         exit (1);
  35.     }
  36.  
  37.     while (!feof (wczytaj))
  38.     {
  39.         fscanf (wczytaj, "%s %f.5", &Ryby[i].gatunek, &Ryby[i].ilosc);
  40.         printf ("%s , %f \n", Ryby[i].gatunek, Ryby[z].ilosc); 
  41.                        
  42.         for (int j=0;j<i;j++)
  43.         {
  44.    
  45.         wynik = strcmp (Ryby[j].gatunek, Ryby[i].gatunek);
  46.            
  47.             printf ("\n j[%d] - %s     i[%d] - %s \n\n",j,Ryby[j].gatunek,i, Ryby[i].gatunek);
  48.            
  49.             if (strcmp (Ryby[j].gatunek, Ryby[i].gatunek)== 0)
  50.             {
  51.                 c=Ryby[i].ilosc;
  52.                 z=i;
  53.             Ryby[z].ilosc = ((Ryby[i].ilosc) + (Ryby[j].ilosc));
  54.             printf ("powtorzylem sie -->  %s , %f , [%d] [%d] -----> %f + %f\n\n", Ryby[i].gatunek, Ryby[z].ilosc, i,z, c, Ryby[j].ilosc);
  55.             (Ryby[j].ilosc) = 0;
  56.             printf ("%d",Ryby[j].ilosc);
  57.            
  58.             }
  59.        
  60.             else
  61.             {
  62.             printf ("nie powtorzylem sie --> %s %f \n", Ryby[i].gatunek, Ryby[i].ilosc);
  63.             }
  64.         }
  65.        
  66.         z++;
  67.         i++; //linczik
  68.     }
  69.  
  70.     zapisz = fopen ("zpolow.txt","w");
  71.     if (NULL==zapisz) {
  72.         perror ("blad");
  73.         fclose (wczytaj);
  74.         exit (1);
  75.     }
  76.    
  77.    
  78.     for (int j=0; j<i; j++) {
  79.        
  80.         printf (" %s %f \n", Ryby[j].gatunek,Ryby[j].ilosc);
  81.        
  82.  
  83.         if ((Ryby[j].ilosc) !=0)
  84.         {
  85.         fprintf (zapisz,"%s - %f.2 \n",Ryby[j].gatunek,Ryby[j].ilosc);
  86.         }
  87.        
  88.        
  89.     }
  90.    
  91.    
  92.     fclose (wczytaj);
  93.     fclose (zapisz);
  94.    
  95.     _getch();
  96.  
  97.     return 0;
  98. }
  99.  
  100. /*
  101. makrela 5
  102. makrela 4
  103. losos 7.5
  104. okon 4.12
  105. tunczyk 6
  106. makrela 8
  107. okon 50
  108. krewetka 30
  109. dorsz 0
  110. */
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment