Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct Student{
  7.     int nr_indexu;
  8.     string imie;
  9.     string nazwisko;
  10.  
  11.     void wypisz()const {
  12.         cout << nr_indexu << " " << imie << " " << nazwisko <<endl;
  13.     }
  14.  
  15. };
  16.  
  17.  
  18. class BazaStudentow{
  19. private:
  20.     vector<Student> bazaStudentow;
  21. public:
  22.  
  23.     Student wczytaj(){
  24.         Student s1;
  25.         cout << "Podaj nr indexu:";
  26.         cin >> s1.nr_indexu;
  27.         cout << "Podaj imie: ";
  28.         cin >> s1.imie;
  29.         cout << "Podaj nazwisko";
  30.         cin >> s1.nazwisko;
  31.         return s1;
  32.     }
  33.  
  34.     void dodaj(){
  35.         Student s1 = wczytaj();
  36.         bazaStudentow.push_back(s1);
  37.     }
  38.  
  39.     void drukuj(){
  40.         for (const Student& s : bazaStudentow) {
  41.             s.wypisz();
  42.         }
  43.     }
  44.  
  45.     const Student* znajdz(int nr_indexu){
  46.         for (const Student& s : bazaStudentow) {
  47.             if( nr_indexu == s.nr_indexu){
  48.                 return &s;
  49.             }
  50.         }
  51.         return nullptr;
  52.     }
  53.  
  54.     const Student* usun(int nr_indexu){
  55.         for (int i=0; i<bazaStudentow.size(); i++) {
  56.             if( nr_indexu == bazaStudentow[i].nr_indexu){
  57.                 const Student s = bazaStudentow[i];
  58.                 bazaStudentow.erase(bazaStudentow.begin() + i);
  59.                 return &s;
  60.             }
  61.         }
  62.         return nullptr;
  63.     }
  64.  
  65.  
  66. };
  67.  
  68.  
  69.  
  70. main()
  71. {
  72.  
  73.     BazaStudentow b;
  74.     b.dodaj();
  75.     b.dodaj();
  76.     b.drukuj();
  77.  
  78.     const Student* wsk = b.znajdz(123123);
  79.  
  80.     if(wsk != nullptr){
  81.         cout<<" Znaleziono " <<endl;
  82.         wsk->wypisz();
  83.     }
  84.  
  85.     const Student* wsk2;
  86.     wsk2 = b.usun(123123);
  87.     if(wsk2 != nullptr){
  88.         cout<<" Usunieto " <<endl;
  89.         wsk2->wypisz();
  90.     }
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement