Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <cstring>
- using namespace std;
- struct Student {
- string JMBAG;
- string ime;
- string prezime;
- };
- int bS(vector<Student> polje, string value, int left, int right) {
- while (left <= right) {
- int middle = (left + right) / 2;
- if (polje[middle].JMBAG == value)
- return middle;
- else if (polje[middle].JMBAG > value)
- right = middle - 1;
- else
- left = middle + 1;
- }
- return -1;
- }
- void buble(int choice, vector<Student> &polje) {
- bool found = true;
- while(found) {
- found = false;
- for(int i = 0; i < polje.size() - 1; ++i) {
- if(choice == 1) {
- if(polje[i].JMBAG > polje[i+1].JMBAG) {
- swap(polje[i], polje[i+1]);
- found = true;
- }
- }
- else if(choice == 2) {
- if(polje[i].ime > polje[i+1].ime) {
- swap(polje[i], polje[i+1]);
- found = true;
- }
- }
- else if(choice == 3) {
- if(polje[i].prezime > polje[i+1].prezime) {
- swap(polje[i], polje[i+1]);
- found = true;
- }
- }
- }
- };
- }
- int main() {
- ifstream dat;
- dat.open("student.txt", ios::in);
- if(!dat)
- return 0;
- vector<Student> polje;
- while(!dat.eof()) {
- Student pom;
- dat >>pom.JMBAG ;
- dat >> pom.ime;
- dat >> pom.prezime;
- polje.push_back(pom);
- }
- for(int i = 0; i < polje.size(); ++i)
- cout << polje[i].JMBAG << " " << polje[i].ime << " " << polje[i].prezime << endl;
- cout << endl;
- int choice;
- do {
- cout << "Upisite\n1: za sortiranje po JMBAG\n2: za sortiranje po imenu\n3: za sortiranje po prezimenu\n: ";
- cin >> choice;
- }while(choice < 1 ||choice > 3);
- if(choice == 1)
- buble(1, polje);
- else if(choice == 2)
- buble(2, polje);
- else
- buble(3, polje);
- for(int i = 0; i < polje.size(); ++i)
- cout << polje[i].JMBAG << " " << polje[i].ime << " " << polje[i].prezime << endl;
- string kljuc;
- cout << "Upisite kljuc: " ;
- cin >> kljuc;
- int vri = bS(polje, kljuc, 0, polje.size()-1);
- if(vri != -1)
- cout << polje[vri].JMBAG << " " << polje[vri].ime << " " << polje[vri].prezime << endl;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment