Mandrah

zad7cw14

Jan 25th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. include <iostream>
  2. #include <fstream>
  3. #include <time.h>
  4. #include <cstdlib>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. struct osoba{
  10.     char nazwisko[20];
  11.     char adres[15];
  12.     char stanowisko[15];
  13.     float brutto;
  14. };
  15.  
  16.  
  17. void wpisz(osoba &o)
  18. {
  19.     cout<<"DODAWANIE NOWEJ OSOBY!"<<endl;
  20.     bool wybor = true;
  21.  
  22.     fstream plik;
  23.     plik.open("zadanie7.dat", ios::binary | ios::out | ios::app);
  24.     if(!plik.good())
  25.     {
  26.         cout<<"Nie otworze!"<<endl;
  27.         return;
  28.     }
  29.  
  30.     while(wybor)
  31.     {
  32.         cout<<"Nazwisko: "; cin.get(o.nazwisko, 19); cin.clear(); cin.sync();
  33.         cout<<"Adres: "; cin.get(o.adres, 14); cin.clear(); cin.sync();
  34.         cout<<"Stanowisko: "; cin.get(o.stanowisko, 14); cin.clear(); cin.sync();
  35.         cout<<"Brutto: "; cin>>o.brutto; cin.clear(); cin.sync();
  36.         cout<<endl;
  37.         cout<<"Chcesz wprowadzic kolejne dane? "; cin>>wybor; cin.clear(); cin.sync();
  38.  
  39.         plik.write(reinterpret_cast<char*>(&o), sizeof(o));
  40.  
  41.     }
  42.  
  43.     plik.close(); plik.clear();
  44. }
  45. //--------------------------------
  46. void raport(osoba &o)
  47. {
  48.     fstream zrodlo;
  49.     zrodlo.open("zadanie7.dat", ios::binary | ios::in);
  50.     fstream raport;
  51.     raport.open("raport_z7.txt", ios::out | ios::app);
  52.     if(!zrodlo.good() || !raport.good())
  53.     {
  54.         cout<<"Nie otworze!"<<endl;
  55.         return;
  56.     }
  57.  
  58.     string tytul, kto;
  59.     cout<<"Tytul raportu: "; getline(cin, tytul); cin.clear(); cin.sync();
  60.     cout<<"Imie i nazwisko: "; getline(cin, kto); cin.clear(); cin.sync();
  61.     time_t czas;
  62.     struct tm * ptr;
  63.     time( & czas );
  64.     ptr = localtime( & czas );
  65.     char * data = asctime( ptr );
  66.     char stanowisko[15];
  67.     cout<<"Podaj dla jakiego stanowiska wyliczyc srednia: "; cin.get(stanowisko, 14); cin.clear(); cin.sync();
  68.  
  69.     raport<<"Tytul raportu: "<<tytul<<endl;
  70.     raport<<"Data raportu: "<<data<<endl;
  71.     raport<<"Sporzadzil: "<<kto<<endl;
  72.     raport<<endl;
  73.  
  74.     int ilu_pracownikow = 0;
  75.     float suma_brutto = 0;
  76.     int ilu_w_zawodzie = 0;
  77.     for(int i = 0;;i++)
  78.     {
  79.         zrodlo.read(reinterpret_cast<char*>(&o), sizeof(o));
  80.  
  81.         if(zrodlo.eof()){
  82.             break;
  83.         }
  84.         if(strcmp(o.stanowisko, stanowisko) == 0)
  85.         {
  86.             suma_brutto += o.brutto;
  87.             ilu_w_zawodzie++;
  88.         }
  89.  
  90.         raport<<o.nazwisko<<" "<<o.adres<<" "<<o.stanowisko<<" "<<o.brutto<<endl;
  91.  
  92.         ilu_pracownikow++;
  93.     }
  94.     float srednia = suma_brutto/ilu_w_zawodzie;
  95.     raport<<endl;
  96.     raport<<"-------------PODSUMOWANIE-----------"<<endl;
  97.     raport<<"W bazie jest "<<ilu_pracownikow<<" pracownikow"<<endl;
  98.     raport<<"Pracownikow pracujacych jako "<<stanowisko<<" jest "<<ilu_w_zawodzie<<endl;
  99.     raport<<"Srednia placy wynosi dla "<<stanowisko<<" wynosi "<<srednia<<endl;
  100.  
  101.     zrodlo.close(); raport.close();
  102. }
  103. //--------------------------------
  104. int main()
  105. {
  106.     osoba o;
  107.     wpisz(o);
  108.     raport(o);
  109. }
Add Comment
Please, Sign In to add comment