Advertisement
cunha1

Untitled

Jan 31st, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class tVrstaRobe {
  7.     public:
  8.     static size_t brojac;
  9.     int
  10.         id,
  11.         godinaProizvodnje;
  12.     char roba[25];
  13.     void unos() {
  14.         int identifikator;
  15.         do {
  16.             cout << "Identifikator: "; cin>>identifikator;
  17.         } while(!(identifikator >= 100 && identifikator <= 999));
  18.         id = identifikator;
  19.         cout << "Roba: "; cin>>roba;
  20.         cout << "Godina proizvodnje: "; cin>>godinaProizvodnje;
  21.     }
  22.     void ispis() {
  23.         cout << "Identifikator: " << id << endl <<
  24.                 "Roba: " << roba << endl <<
  25.                 "Godina proizvodnje: " << godinaProizvodnje << endl;
  26.     }
  27. };
  28.  
  29. tVrstaRobe Aroba[50];
  30. size_t tVrstaRobe::brojac=0;
  31.  
  32. class tRoba {
  33.     public:
  34.     int    
  35.         redniBroj,
  36.         id;
  37.     char    
  38.         dobavljac[25],
  39.         primjedba[25];
  40.     bool unos() {
  41.         cout << "Identifikator: "; cin>>id;
  42.         bool pronaden = false;
  43.         for(size_t i=0;i<tVrstaRobe::brojac;i++) {
  44.             if(Aroba[i].id == id) {
  45.                 pronaden=true;
  46.                 break;
  47.             }
  48.         }
  49.         if(!pronaden) {
  50.             cout << "Ne postoji vrsta robe s tim identifikatorom. " << endl;
  51.             return 0;
  52.         }
  53.         cout << "Redni broj robe: "; cin>>redniBroj;
  54.         cout << "Dobavljač: "; cin>>dobavljac;
  55.         cout << "Primjedba: "; cin>>primjedba;
  56.         return 1;
  57.     }
  58.     void ispis() {
  59.         cout << "Identifikator: " << id << endl <<
  60.                 "Redni broj robe: " << redniBroj << endl <<
  61.                 "Dobavljač: " << dobavljac << endl <<
  62.                 "Primjedba: " << primjedba << endl;
  63.     }
  64. };
  65.  
  66.  
  67.  
  68. void unosVrsteRobe() {
  69.     Aroba[tVrstaRobe::brojac++].unos();
  70. }
  71.  
  72. void ispisVrstaRobe() {
  73.     for(size_t i=0;i<tVrstaRobe::brojac;i++)
  74.         Aroba[i].ispis();
  75. }
  76.  
  77. void unosRobe() {
  78.     ofstream dat("datoteka.dat", ios::binary | ios::app);
  79.     tRoba zapis;
  80.     if(zapis.unos())
  81.         dat.write((char*)&zapis, sizeof(zapis));
  82.     dat.close();
  83. }
  84.  
  85. void ispisZadaneRobe() {
  86.     int identifikator;
  87.     tRoba zapis;
  88.     cout << "Identifikator: "; cin>>identifikator;
  89.     ifstream dat("datoteka.dat",ios::binary);
  90.     while(dat.read((char*)&zapis,sizeof(zapis))) {
  91.         if(zapis.id == identifikator) {
  92.             zapis.ispis();
  93.         }
  94.     }
  95.     dat.close();
  96. }
  97.  
  98. void statistika() {
  99.     cout << "Broj vrsta robe: " << tVrstaRobe::brojac << endl;
  100.     ifstream dat("datoteka.dat",ios::binary);
  101.     int brojacRoba = 0;
  102.     int zbrojRoba = 0;
  103.     tRoba zapis;
  104.     while(dat.read((char*)&zapis, sizeof(zapis))) {
  105.         zbrojRoba += zapis.id;
  106.         brojacRoba++;
  107.     }
  108.     dat.close();
  109.     cout << "Broj robe: " << brojacRoba << endl;
  110.     cout << "Aritmeticka sredina id-a robe: " << (float)zbrojRoba / brojacRoba << endl;
  111. }
  112.  
  113. void ispisiPoKljucu() {
  114.     int identifikator;
  115.     cout << "Identifikator: "; cin>>identifikator;
  116.     int pronadenaVrstaRobe=false;
  117.     for(size_t i=0;i<tVrstaRobe::brojac;i++) {
  118.         if(identifikator==Aroba[i].id) {
  119.             pronadenaVrstaRobe=true;
  120.             cout << "Vrsta robe------" << endl;
  121.             Aroba[i].ispis();
  122.             ifstream dat("datoteka.dat",ios::binary);
  123.             tRoba zapis;
  124.             bool pronadenaRoba = false;
  125.             while(dat.read((char*)&zapis,sizeof(zapis))) {
  126.                 if(identifikator == zapis.id) {
  127.                     pronadenaRoba = true;
  128.                     cout << "Roba------" << endl;
  129.                     zapis.ispis();
  130.                     break;
  131.                 }
  132.             }
  133.             dat.close();
  134.             if(!pronadenaRoba)
  135.                 cout << "Nema robe s tim identifikatorom." << endl;
  136.         }
  137.     }
  138.     if(!pronadenaVrstaRobe)
  139.         cout << "Nema vrste robe s tim identifikatorom. " << endl;
  140. }
  141.  
  142. void ispisVrsteRobaSRobom() {
  143.     for(size_t i=0;i<tVrstaRobe::brojac;i++) {
  144.         ifstream dat("datoteka.dat",ios::binary);
  145.         tRoba zapis;
  146.         while(dat.read((char*)&zapis,sizeof(zapis))) {
  147.             if(Aroba[i].id == zapis.id) {
  148.                 Aroba[i].ispis();
  149.                 break;
  150.             }
  151.         }
  152.         dat.close();
  153.     }
  154. }
  155.  
  156. int main() {
  157.     ofstream dat("datoteka.dat");
  158.     dat.close();
  159.     int unos;
  160.     do {
  161.         cout << "1. Upis vrste robe" << endl <<
  162.                 "2. Ispis vrsta roba" << endl <<
  163.                 "3. Upis robe" << endl <<
  164.                 "4. Ispis zadane robe preko id-a" << endl <<
  165.                 "5. Ukupan elemenata obje strukture, arit. sredina identifikatora iz robe" << endl <<
  166.                 "6. Ispisi po identifikatoru" << endl <<
  167.                 "7. Ispis vrste roba koje imaju robu" << endl <<
  168.                 "9. Izlaz" << endl;
  169.         cin>>unos;
  170.         switch(unos) {
  171.             case 1:
  172.                 unosVrsteRobe();
  173.                 break;
  174.             case 2:
  175.                 ispisVrstaRobe();
  176.                 break;
  177.             case 3:
  178.                 unosRobe();
  179.                 break;
  180.             case 4:
  181.                 ispisZadaneRobe();
  182.                 break;
  183.             case 5:
  184.                 statistika();
  185.                 break;
  186.             case 6:
  187.                 ispisiPoKljucu();
  188.                 break;
  189.             case 7:
  190.                 ispisVrsteRobaSRobom();
  191.                 break;
  192.         }
  193.    
  194.     } while(unos!=9);
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement