MeehoweCK

Untitled

Mar 24th, 2020
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. // Lista.h
  2. #ifndef LISTA_H
  3. #define LISTA_H
  4.  
  5. #include "Head.h"
  6.  
  7. class Lista
  8. {
  9.     public:
  10.         Lista();        // konstruktor domyœlny
  11.         ~Lista();
  12.         void wczytaj_z_pliku();
  13.         void dodaj(string, string, string);
  14.         void dodawanie();
  15.         void wypisz_liste();
  16.         void kasuj_ostatni();
  17.     private:
  18.         Student* head;
  19. };
  20.  
  21. #endif // LISTA_H
  22.  
  23. // Lista.cpp
  24. #include <fstream>
  25. #include "Lista.h"
  26.  
  27. using namespace std;
  28.  
  29. /*class Lista
  30. {
  31.     public:
  32.         Lista();        // konstruktor domyœlny
  33.         ~Lista();
  34.         void wczytaj_z_pliku();
  35.         void dodaj(string, string, string);
  36.         void dodawanie();
  37.         void wypisz_liste();
  38.         void kasuj_ostatni();
  39.     private:
  40.         Student* head;
  41. };*/
  42.  
  43. Lista::Lista() : head(nullptr)
  44. {
  45. }
  46.  
  47.  
  48. Lista::~Lista()
  49. {
  50.     while(head)
  51.         kasuj_ostatni();
  52.     cout << "Lista usunieta\n";
  53. }
  54.  
  55. void Lista::kasuj_ostatni()
  56. {
  57.     Student* temp = head;
  58.     if(temp)
  59.     {
  60.         if(temp->next)
  61.         {
  62.             while(temp->next->next )
  63.                 temp = temp->next;
  64.             delete temp->next;
  65.             temp->next = nullptr;
  66.         }
  67.         else
  68.         {
  69.             delete temp;
  70.             head = nullptr;
  71.         }
  72.     }
  73. }
  74.  
  75. void Lista::wczytaj_z_pliku()
  76. {
  77.     ifstream plik;
  78.     string imie, nazwisko, pesel;
  79.     plik.open("lista.txt");
  80.     if(plik.fail())
  81.     {
  82.         cout << "Nie udalo sie wczytac pliku.\n";
  83.         return;
  84.     }
  85.     while(!plik.eof())
  86.     {
  87.         plik >> imie >> nazwisko >> pesel;
  88.         dodaj(imie, nazwisko, pesel);
  89.     }
  90.     cout << "Lista wczytana prawidlowo\n";
  91.     plik.close();
  92. }
  93.  
  94. void Lista::dodawanie()
  95. {
  96.     cout << "Podaj dane studenta (imie, nazwisko, pesel): ";
  97.     string imie, nazwisko, pesel;
  98.     cin >> imie >> nazwisko >> pesel;
  99.     dodaj(imie, nazwisko, pesel);
  100. }
  101.  
  102. void Lista::dodaj(string imie, string nazwisko, string pesel)
  103. {
  104.     Student* nowy = new Student;
  105.     nowy->Name = imie;
  106.     nowy->LastName = nazwisko;
  107.     nowy->dane = pesel;
  108.  
  109.     if(head == nullptr)
  110.         head = nowy;
  111.  
  112.     else
  113.     {
  114.         Student* temp = head;
  115.  
  116.         while(temp->next)
  117.             temp = temp->next;
  118.         temp->next = nowy;
  119.         nowy->next = nullptr;
  120.     }
  121. }
  122.  
  123. void Lista::wypisz_liste()
  124. {
  125.     Student* temp = head;
  126.     while(temp != nullptr)
  127.     {
  128.         cout << temp->Name << " " << temp->LastName << " " << temp->dane << endl;
  129.         temp = temp->next;
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment