Advertisement
abs25

SweetC++

Nov 18th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. typedef unsigned int index;
  6.  
  7. template<class T>
  8. class Element
  9. {
  10. public:
  11.     T d;
  12.     Element() {}
  13.     Element(T& d) : d(d) {};
  14.     template<class U> friend ostream& operator<<(ostream& buffer, const Element<U>& x);
  15.     ~Element() {};
  16. };
  17.  
  18. template<class T>
  19. ostream& operator<<(ostream& buffer, const Element<T>& x)
  20. {
  21.     buffer << x.d;
  22.     return buffer;
  23. }
  24.  
  25. template<class T>
  26. class Stack
  27. {
  28. private:
  29.     Element<T>* S;
  30.     index max_size;
  31.     int top;
  32. public:
  33.     Stack(index N);
  34.     bool isEmpty() const;
  35.     bool isFull() const;
  36.     void push(const Element<T>& x);
  37.     Element<T> look_top();
  38.     void pop();
  39.     template<class U> friend ostream& operator<<(ostream& buffer, const Stack<U>& stack);
  40.     ~Stack();
  41. };
  42.  
  43. template<class T>
  44. ostream& operator<<(ostream& buffer, const Stack<T>& stack)
  45. {
  46.     if (stack.isEmpty()) { buffer << "Stog je prazan."; return buffer; }
  47.     else
  48.     {
  49.         for (int i = 0; i <= stack.top; i++) buffer << stack.S[i]<" ";
  50.         return buffer;
  51.     }
  52. }
  53.  
  54. //konstruktori i destruktori
  55.  
  56. template<class T>
  57. Stack<T>::Stack(index N) :max_size(N), top(-1)
  58. {
  59.     S = new Element<T>[N];
  60. }
  61.  
  62. template<class T>
  63. Stack<T>::~Stack()
  64. {
  65.     delete[] S;
  66. }
  67.  
  68. template<class T>
  69. bool Stack<T>::isEmpty() const
  70. {
  71.     return top == -1;
  72. }
  73.  
  74. template<class T>
  75. bool Stack<T>::isFull() const
  76. {
  77.     return top == max_size - 1;
  78. }
  79.  
  80. template<class T>
  81. void Stack<T>::push(const Element<T>& x)
  82. {
  83.     if (isFull()) return;
  84.     else
  85.     {
  86.         top += 1;
  87.         S[top] = x;
  88.         return;
  89.     }
  90. }
  91.  
  92. template<class T>
  93. Element<T> Stack<T>::look_top()
  94. {
  95.     if (isEmpty()) return NULL;
  96.     else
  97.     {
  98.         return S[top];
  99.     }
  100. }
  101.  
  102. template<class T>
  103. void Stack<T>::pop()
  104. {
  105.     if (isEmpty()) return;
  106.     else
  107.     {
  108.         top--;
  109.         return;
  110.  
  111.     }
  112. }
  113.  
  114. int main()
  115. {
  116.     index n;
  117.     cin >> n;
  118.     Stack<int> stog(n);
  119.     int unos;
  120.     string naredba;
  121.     while (1)
  122.     {
  123.         cout << "Stanje stoga: " << stog << endl;
  124.         cin >> naredba;
  125.         if (!naredba.compare("PUSH"))
  126.         {
  127.             cin >> unos;
  128.             stog.push(unos);
  129.             continue;
  130.         }
  131.         if (!naredba.compare("POP"))
  132.         {
  133.             stog.pop();
  134.             continue;
  135.         }
  136.         if (!naredba.compare("exit"))
  137.         {
  138.             cout << "Izlaz" << endl;
  139.             break;
  140.         }
  141.         cout << "Naredba nije poznata";
  142.     }
  143.  
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement