Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <cstring>
  5. #include "biblioteka_vrijeme.cc"
  6.  
  7. using namespace std;
  8.  
  9. struct tpodaci{
  10. int sifra;
  11. char naziv[30];
  12. char smjer[20];
  13. float vrijeme_unosa;
  14. int vel_dat;
  15. };
  16.  
  17. fstream dat;
  18. tpodaci podaci, podaci1;
  19. int prosti_broj, rbz;
  20.  
  21. int prosti(int vel_dat){
  22. bool prost=false;
  23. int i,j;
  24. for(i=vel_dat-1;i>1;i--){
  25. prost=true;
  26. for(j=2;j<=sqrt(i);j++){
  27. if(i%j==0){
  28. prost=false;
  29. break;
  30. }
  31. }
  32. if(prost) return i;
  33. }
  34. return 0;
  35. }
  36.  
  37. void kreiraj(){
  38. cout<<"Velicina datoteke(broj zapisa): ";cin>>podaci.vel_dat;
  39. podaci.sifra=0;
  40. podaci.naziv[0]='\0';
  41. podaci.smjer[0]='\0';
  42. prosti_broj=prosti(podaci.vel_dat);
  43. cout<<"Prosti broj: "<<prosti_broj<<endl;
  44. dat.open("datoteka.dat", ios::out | ios::binary);
  45. int i;
  46. for(i=0;i<podaci.vel_dat;i++){
  47. dat.write((char*)&podaci, sizeof(tpodaci));
  48. }
  49. dat.close();
  50. cout<<endl;
  51. }
  52.  
  53. void unos(){
  54. vrijeme_pocetak();
  55. cout<<"Sifra predmeta: ";cin>>podaci.sifra;
  56. cin.ignore();
  57. cout<<"Naziv predmeta: ";cin.getline(podaci.naziv,30);
  58. cout<<"Smjer: ";cin.getline(podaci.smjer,20);
  59. vrijeme_kraj();
  60. podaci.vrijeme_unosa=int(vrijeme_proteklo()/1000);
  61. rbz=podaci.sifra%prosti_broj;
  62. dat.open("datoteka.dat", ios::in | ios::out | ios::binary);
  63. dat.seekg(rbz*sizeof(tpodaci));
  64. do{
  65. dat.read((char*)&podaci1, sizeof(tpodaci));
  66. }while(podaci1.sifra>0);
  67. rbz=(dat.tellg()/sizeof(tpodaci))-1;
  68. dat.seekp(rbz*sizeof(tpodaci));
  69. dat.write((char*)&podaci, sizeof(tpodaci));
  70. dat.close();
  71. cout<<endl;
  72. }
  73.  
  74. void trazi(){
  75. int brojac=0;
  76. bool postoji=false;
  77. int sifra;
  78. cout<<"Unesite trazenu sifru: ";cin>>sifra;
  79. dat.open("datoteka.dat", ios::in | ios::binary);
  80. rbz=sifra%prosti_broj;
  81. dat.seekg(rbz*sizeof(tpodaci));
  82. do{
  83. dat.read((char*)&podaci, sizeof(tpodaci));
  84. if ((podaci.sifra%prosti_broj) == rbz) brojac++;
  85. if(dat.eof()) break;
  86. if(podaci.sifra==sifra){
  87. postoji=true;
  88. cout<<endl;
  89. cout<<"Naziv predmeta: "<<podaci.naziv<<endl;
  90. cout<<"Smjer: "<<podaci.smjer<<endl;
  91. cout<<"Vrijeme unosa: "<<podaci.vrijeme_unosa<<" sek"<<endl<<endl;
  92. if(brojac>1) cout<<"Postoji duplikat"<<endl<<endl;
  93. }
  94. }while(podaci.sifra>0);
  95. dat.close();
  96. cout<<endl;
  97. if(!postoji) cout<<"Zapis ne postoji!"<<endl;
  98. }
  99.  
  100. void statistika(){
  101. int suma=0;
  102. dat.open("datoteka.dat", ios::in | ios::binary);
  103. while(true){
  104. dat.read((char*)&podaci, sizeof(tpodaci));
  105. if(dat.eof()) break;
  106. suma+=podaci.sifra;
  107. }
  108. cout<<"Zbroj svih primarnih kljuceva u datoteci: "<<suma<<endl<<endl;
  109. dat.close();
  110. }
  111.  
  112. int main(){
  113. int odabir;
  114. do{
  115. cout<<"Unesite 1 za kreiranje datoteke"<<endl;
  116. cout<<"Unesite 2 za unos u datoteku"<<endl;
  117. cout<<"Unesite 3 za pretrazivanje datoteke"<<endl;
  118. cout<<"Unesite 4 za statistiku datoteke"<<endl;
  119. cout<<"-------------------------------------"<<endl;
  120. cout<<"Vas odabir? ";cin>>odabir;
  121. cout<<endl;
  122. switch(odabir){
  123. case 1:kreiraj();break;
  124. case 2:unos();break;
  125. case 3:trazi();break;
  126. case 4:statistika();break;
  127. default:break;
  128. }
  129. }while(odabir!=9);
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement