Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <cstring>
- #include "biblioteka_vrijeme.cc"
- using namespace std;
- struct tpodaci{
- int sifra;
- char naziv[30];
- char smjer[20];
- float vrijeme_unosa;
- int vel_dat;
- };
- fstream dat;
- tpodaci podaci, podaci1;
- int prosti_broj, rbz;
- int prosti(int vel_dat){
- bool prost=false;
- int i,j;
- for(i=vel_dat-1;i>1;i--){
- prost=true;
- for(j=2;j<=sqrt(i);j++){
- if(i%j==0){
- prost=false;
- break;
- }
- }
- if(prost) return i;
- }
- return 0;
- }
- void kreiraj(){
- cout<<"Velicina datoteke(broj zapisa): ";cin>>podaci.vel_dat;
- podaci.sifra=0;
- podaci.naziv[0]='\0';
- podaci.smjer[0]='\0';
- prosti_broj=prosti(podaci.vel_dat);
- cout<<"Prosti broj: "<<prosti_broj<<endl;
- dat.open("datoteka.dat", ios::out | ios::binary);
- int i;
- for(i=0;i<podaci.vel_dat;i++){
- dat.write((char*)&podaci, sizeof(tpodaci));
- }
- dat.close();
- cout<<endl;
- }
- void unos(){
- vrijeme_pocetak();
- cout<<"Sifra predmeta: ";cin>>podaci.sifra;
- cin.ignore();
- cout<<"Naziv predmeta: ";cin.getline(podaci.naziv,30);
- cout<<"Smjer: ";cin.getline(podaci.smjer,20);
- vrijeme_kraj();
- podaci.vrijeme_unosa=int(vrijeme_proteklo()/1000);
- rbz=podaci.sifra%prosti_broj;
- dat.open("datoteka.dat", ios::in | ios::out | ios::binary);
- dat.seekg(rbz*sizeof(tpodaci));
- do{
- dat.read((char*)&podaci1, sizeof(tpodaci));
- }while(podaci1.sifra>0);
- rbz=(dat.tellg()/sizeof(tpodaci))-1;
- dat.seekp(rbz*sizeof(tpodaci));
- dat.write((char*)&podaci, sizeof(tpodaci));
- dat.close();
- cout<<endl;
- }
- void trazi(){
- int brojac=0;
- bool postoji=false;
- int sifra;
- cout<<"Unesite trazenu sifru: ";cin>>sifra;
- dat.open("datoteka.dat", ios::in | ios::binary);
- rbz=sifra%prosti_broj;
- dat.seekg(rbz*sizeof(tpodaci));
- do{
- dat.read((char*)&podaci, sizeof(tpodaci));
- if ((podaci.sifra%prosti_broj) == rbz) brojac++;
- if(dat.eof()) break;
- if(podaci.sifra==sifra){
- postoji=true;
- cout<<endl;
- cout<<"Naziv predmeta: "<<podaci.naziv<<endl;
- cout<<"Smjer: "<<podaci.smjer<<endl;
- cout<<"Vrijeme unosa: "<<podaci.vrijeme_unosa<<" sek"<<endl<<endl;
- if(brojac>1) cout<<"Postoji duplikat"<<endl<<endl;
- }
- }while(podaci.sifra>0);
- dat.close();
- cout<<endl;
- if(!postoji) cout<<"Zapis ne postoji!"<<endl;
- }
- void statistika(){
- int suma=0;
- dat.open("datoteka.dat", ios::in | ios::binary);
- while(true){
- dat.read((char*)&podaci, sizeof(tpodaci));
- if(dat.eof()) break;
- suma+=podaci.sifra;
- }
- cout<<"Zbroj svih primarnih kljuceva u datoteci: "<<suma<<endl<<endl;
- dat.close();
- }
- int main(){
- int odabir;
- do{
- cout<<"Unesite 1 za kreiranje datoteke"<<endl;
- cout<<"Unesite 2 za unos u datoteku"<<endl;
- cout<<"Unesite 3 za pretrazivanje datoteke"<<endl;
- cout<<"Unesite 4 za statistiku datoteke"<<endl;
- cout<<"-------------------------------------"<<endl;
- cout<<"Vas odabir? ";cin>>odabir;
- cout<<endl;
- switch(odabir){
- case 1:kreiraj();break;
- case 2:unos();break;
- case 3:trazi();break;
- case 4:statistika();break;
- default:break;
- }
- }while(odabir!=9);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement