Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Klasa: stack (z ang. stos)
- * Definicja klasy: po angielsku
- * Dokumantacja: po polsku
- *
- * Class: stack
- * Class definition: ENG
- * Documentation: PL
- *
- * Wymagany C++11!
- * C++11 required!
- */
- // Dla std::exception do dziedziczenia przez klase stack_top_error
- #include <exception>
- // Klasa wyjatku stack_top_error
- class stack_top_error
- : public std::exception {
- public:
- virtual const char* what() const throw() {
- return "Stos jest pusty!";
- }
- };
- template<typename _Type>
- class stack {
- // Pojedynczy element stosu (tzw. wezel, z ang. node)
- struct _node {
- // Wskaznik do nizszego o jeden elementu stosu
- _node *bottom;
- // Wartosc elementu stosu
- _Type value;
- // Konstruktor
- _node(_Type var, _node *ptr)
- : bottom(ptr), value(var) { }
- };
- // Funkcja _getElement() pozwala dostac sie do dowolnego elementu
- // Prywatna, uzywana przez konstruktor kopiujacy i operator =
- _Type& _getElement(unsigned n) const {
- _node *helper = this->_top;
- for(unsigned i=this->_size-1; i>n; i--)
- helper = helper->bottom;
- return helper->value;
- }
- public:
- // Konstruktor (+ jego definicja)
- stack() : _size(0), _top(nullptr) { }
- // Konstruktor kopiujacy
- stack(const stack<_Type>&);
- // Operator =
- stack<_Type>& operator=(const stack<_Type>&);
- // Destruktor
- ~stack();
- // Funkcje dot. stosu
- void push(const _Type&);
- void pop();
- bool empty();
- unsigned size();
- // Kilka dodatkowych funkcji, ktore uwazam za przydatne
- /*
- * Funkcja top() zwraca referencje do szczytu stosu.
- * Ozncza to ze taki zapis: moj_stos.top() = 50,
- * przypisze do szczytu stosu wartosc 50.
- * Inaczej mowiac rezultat funkcji moze byc l-wartoscia.
- * Gdy stos jest pusty funkcja rzuci wyjatek
- * stack_top_error (definicja klasy powyzej).
- * Wyjatek ten mozna zlapac instrukcja catch(std::exception&).
- * Nie mialem po prostu pomyslu co ta funkcja ma zrobic
- * gdy stos jest pusty (nie moge zwrocic np 0, gdyz uzytkownik
- * moze uzyc swojej klasy jako elementu stosu).
- */
- _Type& top() throw(stack_top_error);
- /*
- * Funkcja max_size() zwraca maksymalna ilosc elementow jakie moze pomiescic stos.
- * Jest to wartosc stala. Funkcja zostala zrealizowana jako funkcja statyczna.
- */
- static constexpr unsigned max_size() { return static_cast<unsigned>(-1); }
- private:
- // Aktualny rozmiar stosu
- unsigned _size;
- // Wskaznik do szczytu stosu
- _node *_top;
- // Przyjaciele
- // Funkcja zrobiona na cele prezentacji
- friend void prezentacjaStosu();
- }; // KONIEC KLASY------------------------------------------------------------------
- // Definicje funkcji:
- // Konstruktor kopiujacy
- template<typename _Type>
- stack<_Type>::stack(const stack<_Type> &value)
- : _size(0), _top(nullptr) {
- for(unsigned i=0; i<value._size; i++)
- this->push(value._getElement(i));
- }
- // Operator =
- template<typename _Type>
- stack<_Type>& stack<_Type>::operator=(const stack<_Type> &value) {
- if(this == &value) return *this;
- this->~stack();
- for(unsigned i=0; i<value._size; i++)
- this->push(value._getElement(i));
- return *this;
- }
- // Destruktor
- template<typename _Type>
- stack<_Type>::~stack() {
- while(this->_size)
- this->pop();
- }
- // push(const _Type&)
- template<typename _Type>
- void stack<_Type>::push(const _Type &value) {
- if(this->_size < this->max_size()) {
- this->_top = new _node(value, this->_top);
- this->_size++;
- }
- }
- // pop()
- template<typename _Type>
- void stack<_Type>::pop() {
- if(this->_size) {
- _node *helper = this->_top;
- this->_top = this->_top->bottom;
- delete helper;
- this->_size--;
- }
- }
- // empty()
- template<typename _Type>
- bool stack<_Type>::empty() {
- return !this->_size;
- }
- // size()
- template<typename _Type>
- unsigned stack<_Type>::size() {
- return this->_size;
- }
- // top()
- template<typename _Type>
- _Type& stack<_Type>::top() throw(stack_top_error) {
- if(!this->_size)
- throw stack_top_error();
- return this->_top->value;
- }
- //**********************************************************************************
- #include <iostream> // Strumienie cin i cout, manipulator endl
- #include <windows.h> // Sleep() i system()
- #include <conio.h> // Dla bardziej dynamicznego menu (funkcja getch())
- using namespace std;
- // Funkcja zaprzyjazniona ze stosem
- void prezentacjaStosu() {
- /*
- * Tak tworzymy stos: stack<typ_danych> nazwa;
- * Jak widac stos dziala rowniez z typami
- * zdefiniowanymi przez uzytkownika (tu std::string);
- */
- stack<string> stack;
- char wybor;
- cout.setf(ios::boolalpha); // Ustawienie flagi boolalpha
- system("title Stos"); // Zmiana tytulu okna
- system("color 0F"); // Zmiana koloru okna
- string x; // Dodatkowa zmienna dla push() i top()
- while(true) {
- cout.flush();
- cout << "Stos stringow: " << endl << endl << endl;
- for(int i=stack.size()-1; i>=0; i--) cout << stack._getElement(i) << endl;
- cout << endl << endl
- << "Lista dzialan: " << endl
- << "1. push()" << endl
- << "2. pop()" << endl
- << "3. empty()" << endl
- << "4. size()" << endl
- << "5. top() -> pokaz szczyt" << endl
- << "6. top() -> edytuj szczyt" << endl
- << "7. max_size()" << endl
- << "8. Wyjscie" << endl << endl
- << "Podaj numer dzialania." << endl;
- switch(wybor = getch()) {
- case '1':
- cout << "Co umiescic na stosie: ";
- cin >> x;
- stack.push(x);
- break;
- case '2':
- if(stack.size()) stack.pop();
- else {
- cout << "Stos jest pusty!";
- Sleep(1500);
- }
- break;
- case '3':
- cout << "Czy stos jest pusty: " << stack.empty();
- Sleep(1500);
- break;
- case '4':
- cout << "Rozmiar stosu: " << stack.size();
- Sleep(1500);
- break;
- case '5':
- try {
- cout << "Szczyt stosu: " << stack.top();
- } catch(exception &e) { // Polimorficzne lapanie wyjatku
- cout << e.what();
- }
- Sleep(1500);
- break;
- case '6':
- cout << "Na co zamienic szczyt: ";
- cin >> x;
- try {
- stack.top() = x;
- } catch(stack_top_error &e) { // Tradycyjne lapanie wyjatku
- cout << e.what();
- Sleep(1500);
- }
- break;
- case '7':
- cout << "Maksymalny rozmiar stosu: " << stack.max_size();
- Sleep(2000);
- break;
- case '8':
- exit(0);
- break;
- }
- system("cls");
- cin.sync();
- }
- }
- int main() { prezentacjaStosu(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement