Advertisement
maverick125a

Trasa funkcje

Jun 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. // ConsoleApplication12.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. struct trasa
  12. {
  13.     float dystans;
  14.     float czas;
  15. };
  16.  
  17.  
  18.  
  19. void wypisz_etapy()
  20. {
  21.    
  22.     trasa pom;
  23.     fstream zrodlo("dane.bin", ios::in | ios::binary);
  24.  
  25.     while (1)
  26.     {
  27.         zrodlo.read((char*)&pom, sizeof(trasa));
  28.         if (zrodlo.good())
  29.         {
  30.            
  31.             cout << "Dystans: " << pom.dystans <<endl;
  32.                 cout << "Czas: " << pom.czas <<endl;
  33.                 cout << endl;
  34.            
  35.  
  36.         }
  37.         else
  38.         {
  39.             if (!zrodlo.eof())
  40.             {
  41.                 cout << "blad odczytu!" << endl;
  42.             }
  43.             break;
  44.  
  45.         }
  46.  
  47.     }
  48.     zrodlo.clear();
  49.     zrodlo.close();
  50.  
  51.  
  52. };
  53.  
  54.  
  55. void trasa_calkowita(float & calkowita1)
  56. {
  57.     trasa pom;
  58.     fstream zrodlo("dane.bin", ios::in | ios::binary);
  59.     calkowita1 = 0;
  60.     while (1)
  61.     {
  62.         zrodlo.read((char*)&pom, sizeof(trasa));
  63.         if (zrodlo.good())
  64.         {
  65.  
  66.             calkowita1 += pom.dystans;
  67.  
  68.         }
  69.         else
  70.         {
  71.             if (!zrodlo.eof())
  72.             {
  73.                 cout << "blad odczytu!" << endl;
  74.             }
  75.             break;
  76.  
  77.         }
  78.  
  79.     }
  80.     zrodlo.clear();
  81.     zrodlo.close();
  82.  
  83.  
  84.  
  85. }
  86.  
  87. void najdluzszy(float & calkowita1)
  88. {
  89.     trasa pom;
  90.     fstream zrodlo("dane.bin", ios::in | ios::binary);
  91.     calkowita1 = 0;
  92.     while (1)
  93.     {
  94.         zrodlo.read((char*)&pom, sizeof(trasa));
  95.         if (zrodlo.good())
  96.         {
  97.  
  98.             calkowita1 += pom.dystans;
  99.  
  100.         }
  101.         else
  102.         {
  103.             if (!zrodlo.eof())
  104.             {
  105.                 cout << "blad odczytu!" << endl;
  106.             }
  107.             break;
  108.  
  109.         }
  110.  
  111.     }
  112.     zrodlo.clear();
  113.     zrodlo.close();
  114.  
  115.  
  116.  
  117. }
  118.  
  119.  
  120.     void srednia_predkosc()
  121.     {
  122.        
  123.         trasa pom;
  124.         fstream zrodlo("dane.bin", ios::in | ios::binary);
  125.         fstream cel("wyniki.txt", ios::out);
  126.         float srednia = 0;
  127.  
  128.         while (1)
  129.         {
  130.             zrodlo.read((char*)&pom, sizeof(trasa));
  131.             if (zrodlo.good())
  132.             {
  133.                 srednia = pom.dystans / pom.czas;
  134.  
  135.                 cel << srednia << endl;
  136.  
  137.             }
  138.             else
  139.             {
  140.                 if (!zrodlo.eof())
  141.                 {
  142.                     cout << "blad odczytu!" << endl;
  143.                 }
  144.                 break;
  145.  
  146.             }
  147.  
  148.         }
  149.         zrodlo.clear();
  150.         zrodlo.close();
  151.         cel.clear();
  152.         cel.close();
  153.     };
  154.  
  155.  
  156. int main()
  157. {
  158.     float dystanssuma;
  159.     //utworzenie_pliku();
  160.     wypisz_etapy();
  161.     trasa_calkowita(dystanssuma);
  162.     cout << endl << "Cały dystans to: " << dystanssuma << endl;
  163.     srednia_predkosc();
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement