Advertisement
Radfler

stack implementation (old, PL)

Mar 17th, 2015
1,599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.26 KB | None | 0 0
  1. /*
  2.  *  Klasa:            stack (z ang. stos)
  3.  *  Definicja klasy:  po angielsku
  4.  *  Dokumantacja:     po polsku
  5.  *
  6.  *  Class:            stack
  7.  *  Class definition: ENG
  8.  *  Documentation:    PL
  9.  *
  10.  *  Wymagany C++11!
  11.  *  C++11 required!
  12.  */
  13.  
  14. // Dla std::exception do dziedziczenia przez klase stack_top_error
  15. #include <exception>
  16. // Klasa wyjatku stack_top_error
  17.  
  18. class stack_top_error
  19. : public std::exception {
  20.  
  21. public:
  22.  
  23.     virtual const char* what() const throw() {
  24.  
  25.         return "Stos jest pusty!";
  26.     }
  27. };
  28.  
  29. template<typename _Type>
  30. class stack {
  31.  
  32.     // Pojedynczy element stosu (tzw. wezel, z ang. node)
  33.     struct _node {
  34.  
  35.         // Wskaznik do nizszego o jeden elementu stosu
  36.         _node *bottom;
  37.  
  38.         // Wartosc elementu stosu
  39.         _Type   value;
  40.  
  41.         // Konstruktor
  42.         _node(_Type var, _node *ptr)
  43.         :  bottom(ptr), value(var) { }
  44.     };
  45.  
  46.     // Funkcja _getElement() pozwala dostac sie do dowolnego elementu
  47.     // Prywatna, uzywana przez konstruktor kopiujacy i operator =
  48.     _Type& _getElement(unsigned n) const {
  49.  
  50.         _node *helper = this->_top;
  51.         for(unsigned i=this->_size-1; i>n; i--)
  52.             helper = helper->bottom;
  53.         return helper->value;
  54.     }
  55.  
  56. public:
  57.  
  58.     // Konstruktor (+ jego definicja)
  59.     stack() : _size(0), _top(nullptr) { }
  60.  
  61.     // Konstruktor kopiujacy
  62.     stack(const stack<_Type>&);
  63.  
  64.     // Operator =
  65.     stack<_Type>& operator=(const stack<_Type>&);
  66.  
  67.     // Destruktor
  68.     ~stack();
  69.  
  70.     // Funkcje dot. stosu
  71.     void      push(const _Type&);
  72.     void       pop();
  73.     bool     empty();
  74.     unsigned  size();
  75.  
  76.     // Kilka dodatkowych funkcji, ktore uwazam za przydatne
  77.  
  78.     /*
  79.      *  Funkcja top() zwraca referencje do szczytu stosu.
  80.      *  Ozncza to ze taki zapis: moj_stos.top() = 50,
  81.      *  przypisze do szczytu stosu wartosc 50.
  82.      *  Inaczej mowiac rezultat funkcji moze byc l-wartoscia.
  83.      *  Gdy stos jest pusty funkcja rzuci wyjatek
  84.      *  stack_top_error (definicja klasy powyzej).
  85.      *  Wyjatek ten mozna zlapac instrukcja catch(std::exception&).
  86.      *  Nie mialem po prostu pomyslu co ta funkcja ma zrobic
  87.      *  gdy stos jest pusty (nie moge zwrocic np 0, gdyz uzytkownik
  88.      *  moze uzyc swojej klasy jako elementu stosu).
  89.      */
  90.     _Type& top() throw(stack_top_error);
  91.  
  92.     /*
  93.      *  Funkcja max_size() zwraca maksymalna ilosc elementow jakie moze pomiescic stos.
  94.      *  Jest to wartosc stala. Funkcja zostala zrealizowana jako funkcja statyczna.
  95.      */
  96.     static constexpr unsigned max_size() { return static_cast<unsigned>(-1); }
  97.  
  98. private:
  99.  
  100.     // Aktualny rozmiar stosu
  101.     unsigned _size;
  102.  
  103.     // Wskaznik do szczytu stosu
  104.     _node *_top;
  105.  
  106. // Przyjaciele
  107.  
  108.     // Funkcja zrobiona na cele prezentacji
  109.     friend void prezentacjaStosu();
  110.  
  111. }; // KONIEC KLASY------------------------------------------------------------------
  112.  
  113. // Definicje funkcji:
  114.  
  115. // Konstruktor kopiujacy
  116. template<typename _Type>
  117. stack<_Type>::stack(const stack<_Type> &value)
  118. : _size(0), _top(nullptr) {
  119.  
  120.     for(unsigned i=0; i<value._size; i++)
  121.         this->push(value._getElement(i));
  122. }
  123.  
  124. // Operator =
  125. template<typename _Type>
  126. stack<_Type>& stack<_Type>::operator=(const stack<_Type> &value) {
  127.  
  128.     if(this == &value) return *this;
  129.     this->~stack();
  130.     for(unsigned i=0; i<value._size; i++)
  131.         this->push(value._getElement(i));
  132.     return *this;
  133. }
  134.  
  135. // Destruktor
  136. template<typename _Type>
  137. stack<_Type>::~stack() {
  138.  
  139.     while(this->_size)
  140.  
  141.         this->pop();
  142. }
  143.  
  144. // push(const _Type&)
  145. template<typename _Type>
  146. void stack<_Type>::push(const _Type &value) {
  147.  
  148.     if(this->_size < this->max_size()) {
  149.  
  150.         this->_top = new _node(value, this->_top);
  151.         this->_size++;
  152.     }
  153. }
  154.  
  155. // pop()
  156. template<typename _Type>
  157. void stack<_Type>::pop() {
  158.  
  159.     if(this->_size) {
  160.  
  161.         _node *helper = this->_top;
  162.         this->_top = this->_top->bottom;
  163.         delete helper;
  164.         this->_size--;
  165.     }
  166. }
  167.  
  168. // empty()
  169. template<typename _Type>
  170. bool stack<_Type>::empty() {
  171.  
  172.     return !this->_size;
  173. }
  174.  
  175. // size()
  176. template<typename _Type>
  177. unsigned stack<_Type>::size() {
  178.  
  179.     return this->_size;
  180. }
  181.  
  182. // top()
  183. template<typename _Type>
  184. _Type& stack<_Type>::top() throw(stack_top_error) {
  185.  
  186.     if(!this->_size)
  187.  
  188.         throw stack_top_error();
  189.  
  190.     return this->_top->value;
  191. }
  192.  
  193. //**********************************************************************************
  194.  
  195. #include <iostream>  // Strumienie cin i cout, manipulator endl
  196. #include <windows.h> // Sleep() i system()
  197. #include <conio.h>   // Dla bardziej dynamicznego menu (funkcja getch())
  198. using namespace std;
  199.  
  200. // Funkcja zaprzyjazniona ze stosem
  201.  
  202. void prezentacjaStosu() {
  203.  
  204.     /*
  205.      *  Tak tworzymy stos: stack<typ_danych> nazwa;
  206.      *  Jak widac stos dziala rowniez z typami
  207.      *  zdefiniowanymi przez uzytkownika (tu std::string);
  208.      */
  209.     stack<string> stack;
  210.     char wybor;
  211.  
  212.     cout.setf(ios::boolalpha);  // Ustawienie flagi boolalpha
  213.     system("title Stos");       // Zmiana tytulu okna
  214.     system("color 0F");         // Zmiana koloru okna
  215.     string x;                   // Dodatkowa zmienna dla push() i top()
  216.  
  217.     while(true) {
  218.  
  219.         cout.flush();
  220.         cout << "Stos stringow: " << endl << endl << endl;
  221.         for(int i=stack.size()-1; i>=0; i--) cout << stack._getElement(i) << endl;
  222.         cout << endl << endl
  223.              << "Lista dzialan: " << endl
  224.              << "1. push()" << endl
  225.              << "2. pop()" << endl
  226.              << "3. empty()" << endl
  227.              << "4. size()" << endl
  228.              << "5. top() -> pokaz szczyt" << endl
  229.              << "6. top() -> edytuj szczyt" << endl
  230.              << "7. max_size()" << endl
  231.              << "8. Wyjscie" << endl << endl
  232.              << "Podaj numer dzialania." << endl;
  233.  
  234.         switch(wybor = getch()) {
  235.  
  236.         case '1':
  237.             cout << "Co umiescic na stosie: ";
  238.             cin >> x;
  239.             stack.push(x);
  240.         break;
  241.  
  242.         case '2':
  243.             if(stack.size()) stack.pop();
  244.             else {
  245.  
  246.                 cout << "Stos jest pusty!";
  247.                 Sleep(1500);
  248.             }
  249.         break;
  250.  
  251.         case '3':
  252.             cout << "Czy stos jest pusty: " << stack.empty();
  253.             Sleep(1500);
  254.         break;
  255.  
  256.         case '4':
  257.             cout << "Rozmiar stosu: " << stack.size();
  258.             Sleep(1500);
  259.         break;
  260.  
  261.         case '5':
  262.             try {
  263.  
  264.                 cout << "Szczyt stosu: " << stack.top();
  265.  
  266.             } catch(exception &e) { // Polimorficzne lapanie wyjatku
  267.  
  268.                 cout << e.what();
  269.             }
  270.             Sleep(1500);
  271.         break;
  272.  
  273.         case '6':
  274.             cout << "Na co zamienic szczyt: ";
  275.             cin >> x;
  276.             try {
  277.  
  278.                 stack.top() = x;
  279.  
  280.             } catch(stack_top_error &e) { // Tradycyjne lapanie wyjatku
  281.  
  282.                 cout << e.what();
  283.                 Sleep(1500);
  284.             }
  285.         break;
  286.  
  287.         case '7':
  288.             cout << "Maksymalny rozmiar stosu: " << stack.max_size();
  289.             Sleep(2000);
  290.         break;
  291.  
  292.         case '8':
  293.             exit(0);
  294.         break;
  295.         }
  296.         system("cls");
  297.         cin.sync();
  298.     }
  299. }
  300.  
  301. int main() { prezentacjaStosu(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement