Advertisement
Guest User

6. vjezba

a guest
Nov 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include "biblioteka_vrijeme_i_binarno_pretrazivanje.cc"
  4. using namespace std;
  5.  
  6. struct tstudent
  7. {
  8.     int rb;
  9.     int matBr;
  10.     char prezIme[50];
  11.     int godina;
  12.     float vrijeme; 
  13. };
  14.  
  15. tindeks indeks;
  16. tstudent stud;
  17. fstream dat, ind;
  18. int zbroj = 0;
  19.  
  20. void Unos()
  21. {
  22.     int brZap;
  23.    
  24.     dat.open("student.dat", ios::in|ios::binary);
  25.     if(!dat)
  26.     {
  27.         brZap = 0;
  28.         dat.open("student.dat", ios::out|ios::binary);
  29.         ind.open("student.ind", ios::out|ios::binary);
  30.     }
  31.     else
  32.     {
  33.         dat.seekg(0, ios::end);
  34.         brZap = dat.tellg()/sizeof(tstudent);
  35.         dat.close();dat.clear();
  36.        
  37.         dat.open("student.dat", ios::in|ios::out|ios::ate|ios::binary);
  38.         ind.open("student.ind", ios::in|ios::out|ios::ate|ios::binary);
  39.     }
  40.    
  41.     brZap++;
  42.     vrijeme_pocetak();
  43.     stud.rb = brZap;
  44.     cout << "Maticni br: ";
  45.     cin >> stud.matBr;
  46.     cout << "Ime i prezime: ";
  47.     unos(stud.prezIme);
  48.     cout << "Godina: ";
  49.     cin >> stud.godina;
  50.     vrijeme_kraj();
  51.    
  52.     dat.write((char*)&stud, sizeof(stud));
  53.     indeks.kljuc = stud.matBr;
  54.     indeks.adresa = (int)dat.tellp()-sizeof(tstudent);
  55.     ind.write((char*)&indeks, sizeof(tindeks));
  56.    
  57.     dat.close();dat.clear();
  58.     ind.close();ind.clear();
  59. }
  60.  
  61. void Pretrazivanje(int kljuc)
  62. {
  63.     dat.open("student.dat", ios::in|ios::binary);
  64.     ind.open("student.ind", ios::in|ios::binary);
  65.    
  66.     bool nadjen = false;
  67.     int brojac = 0;
  68.    
  69.     while(true)
  70.     {
  71.         ind.read((char*)&indeks, sizeof(indeks));
  72.         if(ind.eof()) break;
  73.    
  74.         brojac++;  
  75.         if(indeks.kljuc == kljuc)
  76.         {
  77.             dat.seekg(indeks.adresa);
  78.             dat.read((char*)&stud, sizeof(stud));
  79.             cout << stud.rb << " " << stud.matBr << " " << stud.prezIme << " " << stud.godina << endl;
  80.             nadjen = true;
  81.             break;
  82.         }
  83.     }
  84.    
  85.     if(!nadjen)
  86.         cout << "Nije naden..." << endl;
  87.    
  88.     cout << "Broj procitanih=" << brojac;
  89.     dat.close();dat.clear();
  90.     ind.close();ind.clear();
  91. }
  92.  
  93. void Ispis()
  94. {
  95.     dat.open("student.dat", ios::in|ios::binary);
  96.     ind.open("student.ind", ios::in|ios::binary);
  97.    
  98.     while(true)
  99.     {
  100.         ind.read((char*)&indeks, sizeof(indeks));
  101.         if(ind.eof()) break;
  102.        
  103.         dat.seekg(indeks.adresa);
  104.         dat.read((char*)&stud, sizeof(stud));
  105.        
  106.         cout << stud.rb << " " << stud.matBr << " " << stud.prezIme << " " << stud.godina << endl;
  107.     }
  108.    
  109.     dat.close();dat.clear();
  110.     ind.close();ind.clear();
  111. }
  112.  
  113. void Statistika()
  114. {
  115.     dat.open("student.dat", ios::in|ios::binary);
  116.     ind.open("student.ind", ios::in|ios::binary);
  117.    
  118.     while(true)
  119.     {
  120.         ind.read((char*)&indeks, sizeof(tindeks));
  121.         if(ind.eof()) break;
  122.  
  123.         zbroj += indeks.kljuc;
  124.     }
  125.    
  126.     dat.close();dat.clear();
  127.     dat.close();dat.clear();
  128.    
  129.     cout << "Zbroj svih vrijednosti primarnih kljuceva: " << zbroj << endl;
  130. }
  131.  
  132. int main()
  133. {  
  134.     int izbor, kljuc;
  135.     do
  136.     {
  137.         cout << "Izbor: ";
  138.         cin >> izbor;
  139.         cout << endl;
  140.        
  141.         switch(izbor)
  142.         {
  143.             case 1:
  144.                 Unos();
  145.                 break;
  146.             case 2:
  147.                 cout << "Kljuc: ";
  148.                 cin >> kljuc;
  149.                 Pretrazivanje(kljuc); break;
  150.                 break;
  151.             case 3:
  152.                 Ispis();
  153.                 break;
  154.             case 4:
  155.                 Statistika(); break;
  156.         }
  157.        
  158.     }while(izbor != 9);
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement