Advertisement
MSlomiany

przygotowanie_kolokwium_11_06_2018

Jun 10th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. // przygotowanie_kolokwium_11_06_2018.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <math.h>
  8. #include <fstream>
  9. using namespace std;
  10.  
  11. struct oferta {
  12.     char nazwa[20];
  13.     float cena;
  14. };
  15.  
  16. void utworz()
  17. {
  18.     setlocale(LC_ALL, "");
  19.     fstream oferty("dane.bin", ios::out | ios::binary);
  20.     oferta pom;
  21.     char znak;
  22.     do
  23.     {
  24.         cout << "Podaj nazwę:" << endl;
  25.         cin >> pom.nazwa;
  26.         cout << "Podaj cenę:" << endl;
  27.         cin >> pom.cena;
  28.         oferty.write((char*)&pom, sizeof(oferta));
  29.         cout << "Dodać kolejną ofertę?" << endl;
  30.         cin >> znak;
  31.     } while (znak == 't' || znak == 'T');
  32.     oferty.clear();
  33.     oferty.close();
  34. }
  35.  
  36. void policzsr(float *sr)
  37. {
  38.     setlocale(LC_ALL, "");
  39.     oferta pom;
  40.     int i = 0;
  41.     *sr = 0;
  42.     fstream oferty("dane.bin", ios::in | ios::binary);
  43.     while (1)
  44.     {
  45.         oferty.read((char*)&pom, sizeof(oferta));
  46.         if (oferty.good())
  47.         {          
  48.             *sr += pom.cena;
  49.             i++;
  50.         }
  51.         else
  52.         {
  53.             if (!oferty.eof())
  54.             {
  55.                 cout << "Błąd odczytu" << endl;
  56.             }
  57.             break;
  58.         }
  59.     }
  60.     *sr /= i;
  61.     oferty.clear();
  62.     oferty.close();
  63. }
  64.  
  65. void szukajsredniej(float srednia)
  66. {
  67.     setlocale(LC_ALL, "");
  68.     oferta pom;
  69.     fstream oferty("dane.bin", ios::in | ios::binary);
  70.     fstream rezultat("znalezione.txt", ios::out);
  71.     while (1)
  72.     {
  73.         oferty.read((char*)&pom, sizeof(oferta));
  74.         if (oferty.good())
  75.         {
  76.             if (pom.cena < (srednia*1.2))
  77.             {
  78.                 cout << "Nazwa: " << pom.nazwa << " Cena: " << pom.cena << endl;
  79.                 rezultat << "Nazwa: " << pom.nazwa << " Cena: " << pom.cena << endl;
  80.             }
  81.         }
  82.         else
  83.         {
  84.             if (!oferty.eof())
  85.             {
  86.                 cout << "Błąd odczytu" << endl;
  87.             }
  88.             break;
  89.         }
  90.     }
  91.     oferty.clear();
  92.     oferty.close();
  93.     rezultat.clear();
  94.     rezultat.close();
  95. }
  96.  
  97. void szukajmin()
  98. {
  99.     setlocale(LC_ALL, "");
  100.     oferta pom, min;
  101.     int i = 0;
  102.     fstream oferty("dane.bin", ios::in | ios::binary);
  103.     fstream najmniejsza("min.txt", ios::out);
  104.     while (1)
  105.     {
  106.         oferty.read((char*)&pom, sizeof(oferta));
  107.         if (oferty.good())
  108.         {
  109.             if (i == 0)
  110.             {
  111.                 min = pom;
  112.             }
  113.             else
  114.             {
  115.                 if (pom.cena < min.cena)
  116.                 {
  117.                     min = pom;
  118.                 }
  119.             }
  120.         }
  121.         else
  122.         {
  123.             if (!oferty.eof())
  124.             {
  125.                 cout << "Błąd odczytu" << endl;
  126.             }
  127.             break;
  128.         }
  129.     }
  130.     cout << "Nazwa: " << pom.nazwa << " Cena: " << pom.cena << endl;
  131.     najmniejsza << "Nazwa: " << pom.nazwa << " Cena: " << pom.cena << endl;
  132.     oferty.clear();
  133.     oferty.close();
  134.     najmniejsza.clear();
  135.     najmniejsza.close();
  136. }
  137.  
  138. int main()
  139. {
  140.     setlocale(LC_ALL, "");
  141.     float srednia;
  142.  
  143.     utworz();
  144.     policzsr(&srednia);
  145.     cout << "Średnia:" << fixed << setw(10) << setprecision(3) << srednia << endl;
  146.     cout << "Maksimum średnia*1.2" << endl;
  147.     szukajsredniej(srednia);
  148.     cout << "Najmniejsza" << endl;
  149.     szukajmin();
  150.  
  151.     system("PAUSE");
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement