Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include "biblioteka_vrijeme_i_binarno_pretrazivanje.cc"
- using namespace std;
- struct tstudent
- {
- int rb;
- int matBr;
- char prezIme[50];
- int godina;
- float vrijeme;
- };
- tindeks indeks;
- tstudent stud;
- fstream dat, ind;
- int zbroj = 0;
- void Unos()
- {
- int brZap;
- dat.open("student.dat", ios::in|ios::binary);
- if(!dat)
- {
- brZap = 0;
- dat.open("student.dat", ios::out|ios::binary);
- ind.open("student.ind", ios::out|ios::binary);
- }
- else
- {
- dat.seekg(0, ios::end);
- brZap = dat.tellg()/sizeof(tstudent);
- dat.close();dat.clear();
- dat.open("student.dat", ios::in|ios::out|ios::ate|ios::binary);
- ind.open("student.ind", ios::in|ios::out|ios::ate|ios::binary);
- }
- brZap++;
- vrijeme_pocetak();
- stud.rb = brZap;
- cout << "Maticni br: ";
- cin >> stud.matBr;
- cout << "Ime i prezime: ";
- unos(stud.prezIme);
- cout << "Godina: ";
- cin >> stud.godina;
- vrijeme_kraj();
- dat.write((char*)&stud, sizeof(stud));
- indeks.kljuc = stud.matBr;
- indeks.adresa = (int)dat.tellp()-sizeof(tstudent);
- ind.write((char*)&indeks, sizeof(tindeks));
- dat.close();dat.clear();
- ind.close();ind.clear();
- }
- void Pretrazivanje(int kljuc)
- {
- dat.open("student.dat", ios::in|ios::binary);
- ind.open("student.ind", ios::in|ios::binary);
- bool nadjen = false;
- int brojac = 0;
- while(true)
- {
- ind.read((char*)&indeks, sizeof(indeks));
- if(ind.eof()) break;
- brojac++;
- if(indeks.kljuc == kljuc)
- {
- dat.seekg(indeks.adresa);
- dat.read((char*)&stud, sizeof(stud));
- cout << stud.rb << " " << stud.matBr << " " << stud.prezIme << " " << stud.godina << endl;
- nadjen = true;
- break;
- }
- }
- if(!nadjen)
- cout << "Nije naden..." << endl;
- cout << "Broj procitanih=" << brojac;
- dat.close();dat.clear();
- ind.close();ind.clear();
- }
- void Ispis()
- {
- dat.open("student.dat", ios::in|ios::binary);
- ind.open("student.ind", ios::in|ios::binary);
- while(true)
- {
- ind.read((char*)&indeks, sizeof(indeks));
- if(ind.eof()) break;
- dat.seekg(indeks.adresa);
- dat.read((char*)&stud, sizeof(stud));
- cout << stud.rb << " " << stud.matBr << " " << stud.prezIme << " " << stud.godina << endl;
- }
- dat.close();dat.clear();
- ind.close();ind.clear();
- }
- void Statistika()
- {
- dat.open("student.dat", ios::in|ios::binary);
- ind.open("student.ind", ios::in|ios::binary);
- while(true)
- {
- ind.read((char*)&indeks, sizeof(tindeks));
- if(ind.eof()) break;
- zbroj += indeks.kljuc;
- }
- dat.close();dat.clear();
- dat.close();dat.clear();
- cout << "Zbroj svih vrijednosti primarnih kljuceva: " << zbroj << endl;
- }
- int main()
- {
- int izbor, kljuc;
- do
- {
- cout << "Izbor: ";
- cin >> izbor;
- cout << endl;
- switch(izbor)
- {
- case 1:
- Unos();
- break;
- case 2:
- cout << "Kljuc: ";
- cin >> kljuc;
- Pretrazivanje(kljuc); break;
- break;
- case 3:
- Ispis();
- break;
- case 4:
- Statistika(); break;
- }
- }while(izbor != 9);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement