Advertisement
gollub

Soc, prva vjezba

Mar 11th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <fstream>
  5. // nisam ukljucio using namespace std, a string se nalazi u std namespace, pa moram ispred string pisati std ::
  6.  
  7. class Student
  8. {
  9.     private:
  10.        std::string name;
  11.        int points, index;
  12.  
  13.     public:
  14.        Student(const std :: string& name = "", const int points = 0, const int index = 0) :
  15.         name(name), points(points), index(index) {} //konstruktor sa podrazumijevanim parametrima, zamjena za prazan i sa parametrima
  16.                                                     //inace bi morali praviti posebno prazan, a posebno sa parametrima
  17.                                                     //ovako jednim udarcem dvije muve
  18.        std::string get_name() const {return name;}
  19.        int get_points() const {return points;}
  20.        int get_index() const {return index;}
  21.  
  22.        void set_name(const std::string& name) {this->name = name;} //ako hocemo ista imena da koristimo, moramo preko this pokazivaca
  23.        void set_points(const int points) {this->points = points;}
  24.        void set_index(const int index) {this->index = index;}
  25.  
  26.        friend bool operator < (const Student& s1, const Student& s2)  //prijateljska funkcija, moze koristiti polja iz private
  27.        {       //prototip funkcije mora biti u publicu, a tijelo moze a ne mora biti unutra, moze i van...
  28.         if(s1.points != s2.points)
  29.         {
  30.           return s1.points < s2.points;   //praksa je da ako su funkcije inline, tijelo bude zajedno sa prototipom, a ako su duz van..
  31.         }
  32.         else
  33.         {
  34.             return s1.index < s2.index;
  35.         }
  36.        }
  37.  
  38.       friend std::ostream& operator << (std::ostream& out, const Student& s)
  39.       {
  40.         out << s.name << ": points=" << s.points << ", index=" << s.index;
  41.         return out;
  42.       }
  43.  
  44. };
  45.  
  46. void print_list(std::vector<Student>& lst)
  47. {
  48.     std::vector<Student>::iterator it;
  49.  
  50.     for(it = lst.begin(); it != lst.end(); ++it)
  51.         std::cout << *it << std::endl;
  52.     std::cout << std::endl;
  53. }
  54.  
  55. void scan_infile(std::vector<Student>& lst)
  56. {
  57.     std::string name;
  58.     int points, index;
  59.     std::ifstream infile;
  60.     infile.open("students.txt");
  61.     if (infile.is_open())
  62.         while(!infile.eof())
  63.         {
  64.             infile >> name >> points >> index;
  65.             lst.push_back(Student(name,points,index));
  66.         }
  67. }
  68.  
  69. int main()
  70. {
  71.     std::vector<Student> st;
  72.  
  73.     scan_infile(st);
  74.  
  75.     print_list(st);
  76.  
  77.     std::sort(st.begin(), st.end());
  78.     print_list(st);
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement