Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // przygotowanie_kolokwium_11_06_2018.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <iomanip>
- #include <math.h>
- #include <fstream>
- using namespace std;
- struct oferta {
- char nazwa[20];
- float cena;
- };
- void utworz()
- {
- setlocale(LC_ALL, "");
- fstream oferty("dane.bin", ios::out | ios::binary);
- oferta pom;
- char znak;
- do
- {
- cout << "Podaj nazwę:" << endl;
- cin >> pom.nazwa;
- cout << "Podaj cenę:" << endl;
- cin >> pom.cena;
- oferty.write((char*)&pom, sizeof(oferta));
- cout << "Dodać kolejną ofertę?" << endl;
- cin >> znak;
- } while (znak == 't' || znak == 'T');
- oferty.clear();
- oferty.close();
- }
- void policzsr(float *sr)
- {
- setlocale(LC_ALL, "");
- oferta pom;
- int i = 0;
- *sr = 0;
- fstream oferty("dane.bin", ios::in | ios::binary);
- while (1)
- {
- oferty.read((char*)&pom, sizeof(oferta));
- if (oferty.good())
- {
- *sr += pom.cena;
- i++;
- }
- else
- {
- if (!oferty.eof())
- {
- cout << "Błąd odczytu" << endl;
- }
- break;
- }
- }
- *sr /= i;
- oferty.clear();
- oferty.close();
- }
- void szukajsredniej(float srednia)
- {
- setlocale(LC_ALL, "");
- oferta pom;
- fstream oferty("dane.bin", ios::in | ios::binary);
- fstream rezultat("znalezione.txt", ios::out);
- while (1)
- {
- oferty.read((char*)&pom, sizeof(oferta));
- if (oferty.good())
- {
- if (pom.cena < (srednia*1.2))
- {
- cout << "Nazwa: " << pom.nazwa << " Cena: " << pom.cena << endl;
- rezultat << "Nazwa: " << pom.nazwa << " Cena: " << pom.cena << endl;
- }
- }
- else
- {
- if (!oferty.eof())
- {
- cout << "Błąd odczytu" << endl;
- }
- break;
- }
- }
- oferty.clear();
- oferty.close();
- rezultat.clear();
- rezultat.close();
- }
- void szukajmin()
- {
- setlocale(LC_ALL, "");
- oferta pom, min;
- int i = 0;
- fstream oferty("dane.bin", ios::in | ios::binary);
- fstream najmniejsza("min.txt", ios::out);
- while (1)
- {
- oferty.read((char*)&pom, sizeof(oferta));
- if (oferty.good())
- {
- if (i == 0)
- {
- min = pom;
- }
- else
- {
- if (pom.cena < min.cena)
- {
- min = pom;
- }
- }
- }
- else
- {
- if (!oferty.eof())
- {
- cout << "Błąd odczytu" << endl;
- }
- break;
- }
- }
- cout << "Nazwa: " << pom.nazwa << " Cena: " << pom.cena << endl;
- najmniejsza << "Nazwa: " << pom.nazwa << " Cena: " << pom.cena << endl;
- oferty.clear();
- oferty.close();
- najmniejsza.clear();
- najmniejsza.close();
- }
- int main()
- {
- setlocale(LC_ALL, "");
- float srednia;
- utworz();
- policzsr(&srednia);
- cout << "Średnia:" << fixed << setw(10) << setprecision(3) << srednia << endl;
- cout << "Maksimum średnia*1.2" << endl;
- szukajsredniej(srednia);
- cout << "Najmniejsza" << endl;
- szukajmin();
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement