Habsburg

Rijeka

Apr 6th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. struct Student {
  8. string JMBAG;
  9. string ime;
  10. string prezime;
  11. };
  12.  
  13. int bS(vector<Student> polje, string value, int left, int right) {
  14. while (left <= right) {
  15. int middle = (left + right) / 2;
  16. if (polje[middle].JMBAG == value)
  17. return middle;
  18. else if (polje[middle].JMBAG > value)
  19. right = middle - 1;
  20. else
  21. left = middle + 1;
  22. }
  23. return -1;
  24. }
  25.  
  26. void buble(int choice, vector<Student> &polje) {
  27. bool found = true;
  28. while(found) {
  29. found = false;
  30. for(int i = 0; i < polje.size() - 1; ++i) {
  31. if(choice == 1) {
  32. if(polje[i].JMBAG > polje[i+1].JMBAG) {
  33. swap(polje[i], polje[i+1]);
  34. found = true;
  35. }
  36. }
  37. else if(choice == 2) {
  38. if(polje[i].ime > polje[i+1].ime) {
  39. swap(polje[i], polje[i+1]);
  40. found = true;
  41. }
  42. }
  43. else if(choice == 3) {
  44. if(polje[i].prezime > polje[i+1].prezime) {
  45. swap(polje[i], polje[i+1]);
  46. found = true;
  47. }
  48. }
  49. }
  50. };
  51. }
  52.  
  53. int main() {
  54. ifstream dat;
  55. dat.open("student.txt", ios::in);
  56. if(!dat)
  57. return 0;
  58.  
  59. vector<Student> polje;
  60. while(!dat.eof()) {
  61. Student pom;
  62. dat >>pom.JMBAG ;
  63. dat >> pom.ime;
  64. dat >> pom.prezime;
  65. polje.push_back(pom);
  66. }
  67.  
  68. for(int i = 0; i < polje.size(); ++i)
  69. cout << polje[i].JMBAG << " " << polje[i].ime << " " << polje[i].prezime << endl;
  70. cout << endl;
  71.  
  72. int choice;
  73. do {
  74. cout << "Upisite\n1: za sortiranje po JMBAG\n2: za sortiranje po imenu\n3: za sortiranje po prezimenu\n: ";
  75. cin >> choice;
  76. }while(choice < 1 ||choice > 3);
  77. if(choice == 1)
  78. buble(1, polje);
  79. else if(choice == 2)
  80. buble(2, polje);
  81. else
  82. buble(3, polje);
  83.  
  84. for(int i = 0; i < polje.size(); ++i)
  85. cout << polje[i].JMBAG << " " << polje[i].ime << " " << polje[i].prezime << endl;
  86.  
  87. string kljuc;
  88. cout << "Upisite kljuc: " ;
  89. cin >> kljuc;
  90.  
  91. int vri = bS(polje, kljuc, 0, polje.size()-1);
  92. if(vri != -1)
  93. cout << polje[vri].JMBAG << " " << polje[vri].ime << " " << polje[vri].prezime << endl;
  94.  
  95. system("PAUSE");
  96.  
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment