Advertisement
iandimare

Untitled

Jan 30th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. #include"List.h"
  2. #include"Zad2.h"
  3. #define x 3
  4.  
  5. template<class T>
  6. void print_list(List<T> *p)
  7. {
  8.     unsigned int i = 1;
  9.     cout << endl;
  10.     while (p)
  11.     {
  12.         cout << i << ". " << p->data << endl;
  13.         p = p->next;
  14.         ++i;
  15.     }
  16.  
  17. }
  18.  
  19. template<class T>
  20. unsigned int count(List<T> *p)
  21. {
  22.     unsigned int counter = 0;
  23.     while (p)
  24.     {
  25.         ++counter;
  26.         p = p->next;
  27.     }
  28.     return counter;
  29. }
  30.  
  31. template<class T>
  32. void add_to_list(List<T> *&head, List<T> *&tail, T element)
  33. {
  34.     List<T> *p = new List<T>;
  35.     p->data = element;
  36.  
  37.     if (head)
  38.     {
  39.         tail->next = p;
  40.         tail = tail->next;
  41.     }
  42.     else head = tail = p;
  43. }
  44.  
  45. template<class T>
  46. void delete_from_list(List<T> *&head, List<T> *&tail)
  47. {
  48.     cout << "\nKtory element chcesz usunac?\n";
  49.     print_list(head);
  50.  
  51.     List<T> *p = head;
  52.     unsigned int n, ilosc = count(head);
  53.  
  54.     cin >> n;
  55.     if (n > ilosc)
  56.         return;
  57.     else
  58.     {
  59.         if (n == 1)
  60.         {
  61.             head = head->next;
  62.             delete p;
  63.         }
  64.         else
  65.         {
  66.             while (n > 2)
  67.             {
  68.                 p = p->next;
  69.                 --n;
  70.             }
  71.             if (p->next == tail)
  72.             {
  73.                 delete tail;
  74.                 tail = p;
  75.                 p->next = NULL;
  76.             }
  77.             else
  78.             {
  79.                 List<T> *cleaner = p->next;
  80.                 p->next = p->next->next;
  81.                 delete cleaner;
  82.             }
  83.         }
  84.     }
  85.     cout << "\nPo usunieciu:\n";
  86.     print_list(head);
  87. }
  88.  
  89. void printmenu()
  90. {
  91.     cout << "\n(1)Wyswietl liste";
  92.     cout << "\n(2)Dodaj do listy";
  93.     cout << "\n(3)Usun z listy";
  94.     cout << "\n(4)Zapisz liste do pliku";
  95.     cout << "\n(5)Odczytaj liste z pliku";
  96.     cout << "\n(6)Wyszukaj studenta";
  97.     cout << "\n(0)Powrot\n";
  98. }
  99.  
  100. template<class T>
  101. void delete_list(List<T> *&head)
  102. {
  103.     List<T> *p;
  104.     while (head)
  105.     {
  106.         p = head;
  107.         head = head->next;
  108.         delete p;
  109.     }
  110. }
  111.  
  112. template<class T>
  113. void add_from_file(List<T> *&head, List<T> *&tail)
  114. {
  115.  
  116. }
  117.  
  118.  
  119. void save(List<Student> *p)
  120. {
  121.     const char *txt = "St.bin";
  122.     ofstream file1(txt, ios::out | ios::binary);
  123.     int ilosc = count(p);
  124.     Student *tab = new Student[ilosc];
  125.  
  126.     int i = 0;
  127.     while (p)
  128.     {
  129.         tab[i] = p->data;
  130.         p = p->next;
  131.         ++i;
  132.     }
  133.  
  134.     file1.write((char*)tab, ilosc*sizeof(Student));
  135.     file1.close();
  136.  
  137.     delete[] tab;
  138. }
  139.  
  140. template<class T>
  141. void load(List<T> *&head, List<T> *&tail)
  142. {
  143.     const char *txt = "St.bin";
  144.     ifstream file1(txt, ios::in | ios::binary);
  145.     Student *tab = new Student[sizeof(Student)];
  146.  
  147.     file1.read((char*)tab, sizeof(Student));
  148.     while (!(file1.eof()))
  149.     {
  150.         add_to_list(head, tail, *tab);
  151.         file1.read((char*)tab, sizeof(Student));
  152.     }
  153.  
  154.     delete[] tab;
  155. }
  156.  
  157. void find_id(List<Student> *p)
  158. {
  159.     int id, counter = 0;
  160.     cout << "\nWprowadz indeks: ";
  161.     cin >> id;
  162.  
  163.     while (p)
  164.     {
  165.         counter++;
  166.         if (p->data.nr_indeksu == id)
  167.         {
  168.             cout << "\nOdnaleziono element. Jest on na pozycji nr " << counter;
  169.             cout << ".\nDane studenta:\n" << p->data;
  170.         }
  171.         p = p->next;
  172.     }
  173. }
  174.  
  175. void zad3()
  176. {
  177.     List<Student> *head = NULL, *tail = NULL;
  178.     Student s1, s2, s3;
  179.     s1 = { "Sebastian", "Jena", 23041995, 122045 };
  180.     s2 = { "Jan", "Nowak", 01012000, 123456 };
  181.     s3 = { "Ktos", "Ktosik", 30042000, 334234 };
  182.     add_to_list(head, tail, s1);
  183.     add_to_list(head, tail, s2);
  184.     add_to_list(head, tail, s3);
  185.  
  186.     char podmenu;
  187.     do
  188.     {
  189.         printmenu();
  190.         podmenu = _getch();
  191.         switch (podmenu)
  192.         {
  193.  
  194.         case '1':
  195.             print_list(head);
  196.             break;
  197.         case '2':
  198.         {
  199.             cout << "Wpisz odpowiednio imie, nazwisko, date urodzenia i indeks,\n i zatwierdzajac wszystko Enterem:\n";
  200.             Student dodaj;
  201.             cin >> dodaj;
  202.             add_to_list(head, tail, dodaj);
  203.         }break;
  204.         case '3':
  205.             delete_from_list(head, tail);
  206.             break;
  207.         case '4':
  208.             save(head);
  209.             break;
  210.         case '5':
  211.             load(head, tail);
  212.             break;
  213.         case '6':
  214.             find_id(head);
  215.             break;
  216.         default:
  217.             break;
  218.         }
  219.     } while (podmenu != '0');
  220.     delete_list(head);
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement