SHOW:
|
|
- or go back to the newest paste.
1 | //Miletic Marko | |
2 | //41367 | |
3 | - | //Vjezba 5 |
3 | + | //Vjezba 6 |
4 | ||
5 | #include <iostream> | |
6 | #include <fstream> | |
7 | #include <cstdlib> | |
8 | #include <ctime> | |
9 | #include "biblioteka_vrijeme_i_binarno_pretrazivanje.cc" | |
10 | using namespace std; | |
11 | struct tRoba { | |
12 | int sifra; | |
13 | char naziv[50]; | |
14 | float cij, kol; | |
15 | float vrj_unosa; | |
16 | }; | |
17 | tindeks indeks; | |
18 | int si = sizeof(tindeks); | |
19 | tRoba roba; | |
20 | int sr = sizeof(tRoba); | |
21 | void mog1() { | |
22 | fstream dat("roba_Miletic_Marko.dat", ios::in|ios::binary); | |
23 | char dn='n'; | |
24 | int ne=0; | |
25 | if(!dat) dat.open("roba_Miletic_Marko.dat", ios::out|ios::binary); | |
26 | else ne=1; | |
27 | if(ne) cout<<"Rad s postojecom datotekom? (d=rad s postojecom, n=brisanje): "; | |
28 | if(ne) cin>>dn; | |
29 | if(ne && dn=='n') | |
30 | for(dat.close(), dat.clear(), dat.open("roba_Miletic_Marko.dat", ios::out|ios::binary); 0; ) | |
31 | dn='n'; | |
32 | fstream ind("roba_Miletic_Marko.ind", ios::in|ios::binary); | |
33 | if(!ind) ind.open("roba_Miletic_Marko.ind", ios::out|ios::binary); | |
34 | else if(dn=='n') | |
35 | for(ind.close(), ind.clear(), ind.open("roba_Miletic_Marko.ind", ios::out|ios::binary); 0;) | |
36 | dn='n'; | |
37 | dat.close(); | |
38 | dat.clear(); | |
39 | ind.close(); | |
40 | ind.clear(); | |
41 | dat.open("roba_Miletic_Marko.dat", ios::in|ios::out|ios::binary|ios::ate); | |
42 | ind.open("roba_Miletic_Marko.ind", ios::in|ios::out|ios::binary|ios::ate); | |
43 | tRoba nova; | |
44 | int a; | |
45 | int sif=1; | |
46 | while(sif) | |
47 | for(sif=1, a=1, vrijeme_pocetak(), cout << "Sifra: ", cin >> nova.sifra, cout << "Naziv: ", cin.ignore(), cin.getline(nova.naziv, 50), cout << "Cijena: ", cin >> nova.cij, cout << "Kolicina: ", cin >> nova.kol; a; a=0) | |
48 | if(nova.sifra>99 && nova.sifra<1000) sif=0; | |
49 | else cout<<"!! Sifra(100-999). Ponovni unos.."<<endl; | |
50 | vrijeme_kraj(); | |
51 | cout << "Proteklo vrijeme: " << (nova.vrj_unosa = vrijeme_proteklo() ) << endl; | |
52 | tindeks novi_; | |
53 | novi_.kljuc = nova.sifra; | |
54 | novi_.adresa = (int)dat.tellp(); | |
55 | dat.write((char*)&nova, sr); | |
56 | ind.seekg(0, ios::end); | |
57 | int i; | |
58 | int broj_zapisa=(int)ind.tellg()/si; | |
59 | int br=1; | |
60 | for(i=broj_zapisa-1; i>=0 && br; br?i--:i) | |
61 | for(ind.seekg(i*si, ios::beg), ind.read( (char*)&indeks, si), ind.seekp((i+1)*si, ios::beg), ind.write((char*)&indeks, si), a=1; a; a=0) | |
62 | if(indeks.kljuc<novi_.kljuc) | |
63 | br=0; | |
64 | if(i<0) ind.seekp(0, ios::beg); | |
65 | else ind.seekp((i+1)*si, ios::beg); | |
66 | ind.write((char*)&novi_, si); | |
67 | ind.seekg(0, ios::beg); | |
68 | for(int i=0; i<=broj_zapisa; cout<<"sifra: " << indeks.kljuc<<endl<<"adresa: " << indeks.adresa << endl<<endl, i++) | |
69 | ind.read((char*)&indeks, si); | |
70 | dat.close(); | |
71 | dat.clear(); | |
72 | ind.close(); | |
73 | ind.clear(); | |
74 | } | |
75 | void mog2() { | |
76 | fstream dat("roba_Miletic_Marko.dat", ios::in|ios::binary); | |
77 | if(!dat) cout << "Maticna datoteka ne postoji." << endl; | |
78 | if(!dat) return; | |
79 | fstream ind("roba_Miletic_Marko.ind", ios::in|ios::binary); | |
80 | if(!ind) cout<<"ind ne postoji"<<endl; | |
81 | if(!ind) dat.close(); | |
82 | if(!ind) dat.clear(); | |
83 | if(!ind) return; | |
84 | dat.seekg(0, ios::end); | |
85 | int mat_br_zapisa = dat.tellg() / sizeof(tRoba); | |
86 | cout << "Broj zapisa: " << mat_br_zapisa << endl; | |
87 | int sifra; | |
88 | cout << "Sifra robe: "; | |
89 | cin >> sifra; | |
90 | int l=0; | |
91 | int d=mat_br_zapisa-1; | |
92 | bool nadjen = false; | |
93 | int br_proc = 0; | |
94 | while(!nadjen) | |
95 | if(l<=d) { | |
96 | ind.seekg((d+l)/2*si, ios::beg); | |
97 | ind.read((char*)&indeks, si); | |
98 | br_proc++; | |
99 | if(sifra>indeks.kljuc) l=(d+l)/2+1; | |
100 | else if(sifra<indeks.kljuc) d=(d+l)/2-1; | |
101 | else nadjen = true; | |
102 | } else break; | |
103 | if(!nadjen) cout<<"Zapis nije pronadjen."<<endl<<"Broj procitanih zapisa: "<<br_proc<<endl; | |
104 | if(!nadjen) | |
105 | for(dat.close(), dat.clear(), ind.close(), ind.clear(); 1; ) | |
106 | return; | |
107 | cout << "Zapis je pronadjen." << endl << "Broj procitanih zapisa: " << br_proc << endl; | |
108 | cout << endl << "sifra: " << indeks.kljuc << endl << "adresa: " << indeks.adresa << endl << endl; | |
109 | dat.seekg(indeks.adresa, ios::beg); | |
110 | dat.read( (char*)&roba, sr); | |
111 | cout << "sifra: " << roba.sifra << endl | |
112 | << "naziv: " << roba.naziv << endl | |
113 | << "cijena: " << roba.cij << endl | |
114 | << "kolicina: " << roba.kol << endl | |
115 | << "vrijeme unosa: " << roba.vrj_unosa << endl << endl; | |
116 | dat.close(); | |
117 | dat.clear(); | |
118 | ind.close(); | |
119 | ind.clear(); | |
120 | } | |
121 | void mog3() { | |
122 | char mat_naziv[75]; | |
123 | cin.ignore(); | |
124 | cout << "Naziv maticne datoteke: "; | |
125 | cin.getline(mat_naziv, 75); | |
126 | fstream dat(mat_naziv, ios::in|ios::binary); | |
127 | if(!dat) cout<<"Maticna datoteka ne postoji."<<endl; | |
128 | if(!dat) return; | |
129 | fstream ind("roba_Miletic_Marko.ind", ios::in|ios::binary); | |
130 | if(!ind) cout<<"Indeksna datoteka ne postoji."<<endl; | |
131 | if(!ind) dat.close(); | |
132 | if(!ind) dat.clear(); | |
133 | if(!ind) return; | |
134 | double vrijed_uk=0; | |
135 | double vrj_uk=0; | |
136 | while(1) { | |
137 | ind.read((char*)&indeks, si); | |
138 | if(ind.eof() ) break; | |
139 | dat.seekg(indeks.adresa,ios::beg); | |
140 | dat.read((char*)&roba,sr); | |
141 | vrijed_uk+=(roba.cij*roba.kol); | |
142 | vrj_uk+=roba.vrj_unosa; | |
143 | cout << "sifra: " << roba.sifra << endl << "naziv: " << roba.naziv << endl << "cijena: " << roba.cij | |
144 | << endl << "kolicina: " << roba.kol << endl << "vrijeme unosa: " << roba.vrj_unosa << endl << endl; | |
145 | } | |
146 | cout << "Ukupna vrijednost robe: " << vrijed_uk << endl << "Ukupno vrijeme unosa: " << vrj_uk << endl; | |
147 | dat.close(); | |
148 | dat.clear(); | |
149 | ind.close(); | |
150 | ind.clear(); | |
151 | } | |
152 | int main() { | |
153 | srand(time(0) ); | |
154 | rand(); | |
155 | int x; | |
156 | do { | |
157 | cout << endl << " -- < Izbornik > -- " << endl | |
158 | << " 1. Rucni unos podataka o robi u indeksiranu datoteku" << endl | |
159 | << " 2. Pretrazivanje indeksirane datoteke prema sifri robe" << endl | |
160 | << " 3. Ispis sadrzaja maticne datoteke u redoslijedu sifre robe" << endl | |
161 | << " 4. Generiranje zapisa o robi (sa sortiranjem indeksa)" << endl | |
162 | << " 9. Izlaz iz programa" << endl << "Odabir: "; | |
163 | cin >> x; | |
164 | switch(x){ | |
165 | case 1: mog1(); break; | |
166 | case 2: mog2(); break; | |
167 | case 3: mog3(); break; | |
168 | } | |
169 | } while(x!=9); | |
170 | return 0; | |
171 | } |