Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. using namespace std;
  5.  
  6. //template <typename T>
  7. //void bubble(T *p, int n)
  8. //{
  9. //  T temp;
  10. //  for (int i = 0; i < n; i++)
  11. //  {
  12. //      for (int j = 0; j < n - i - 1; j++)
  13. //      {
  14. //          if (p[j] > p[j+1])
  15. //          {
  16. //              temp = p[j];
  17. //              p[j] = p[j+1];
  18. //              p[j+1] = temp;
  19. //          }
  20. //      }
  21. //  }
  22. //  return;
  23. //}
  24. //
  25. //template <typename T>
  26. //void ispis(T *p, int n)
  27. //{
  28. //  for (int i = 0; i < n; i++)
  29. //  {
  30. //      cout << p[i] << " ";
  31. //  }
  32. //  cout << endl;
  33. //  return;
  34. //}
  35. //
  36. //int main()
  37. //{
  38. //  srand(time(NULL));
  39. //  int *a = new int[3];
  40. //  double *b = new double[5];
  41. //  char *c = new char[4];
  42. //
  43. //  for (int i = 0; i < 3; i++)
  44. //  {
  45. //      a[i] = rand() % 50;
  46. //  }
  47. //  for (int i = 0; i < 5; i++)
  48. //  {
  49. //      b[i] = (double)rand()/RAND_MAX * 10;
  50. //  }
  51. //  for (int i = 0; i < 4; i++)
  52. //  {
  53. //      c[i] = 65 + rand() % 25;
  54. //  }
  55. //
  56. //  bubble(a, 3);
  57. //  bubble(b, 5);
  58. //  bubble(c, 4);
  59. //
  60. //  ispis(a, 3);
  61. //  ispis(b, 5);
  62. //  ispis(c, 4);
  63. //
  64. //  delete[] a;
  65. //  delete[] b;
  66. //  delete[] c;
  67. //  return 0;
  68. //}
  69.  
  70. template <typename T>
  71. class stog
  72. {
  73. private:
  74.     T *p;
  75.     int n, size;
  76. public:
  77.     stog(int x)
  78.     {
  79.         size = x;
  80.         n = -1;
  81.         p = new T[size];
  82.     }
  83.     stog(const stog& ref)
  84.     {
  85.         size = ref.size;
  86.         p = new T[size];
  87.         for (int i = 0; i < size; i++)
  88.         {
  89.             p[i] = ref.p[i];
  90.         }
  91.     }
  92.     ~stog()
  93.     {
  94.         delete[] p;
  95.     }
  96.     stog& operator= (const stog& ref)
  97.     {
  98.         if (this != &ref)
  99.         {
  100.             size = ref.size;
  101.             p = new T[size];
  102.             for (int i = 0; i < size; i++)
  103.             {
  104.                 p[i] = ref.p[i];
  105.             }
  106.         }
  107.         return *this;
  108.     }
  109.     void push(T x)
  110.     {
  111.         if (n == size)
  112.         {
  113.             cout << "Puno." << endl;
  114.         }
  115.         else
  116.         {
  117.             p[++n] = x;
  118.         }
  119.  
  120.         return;
  121.     }
  122.     T pop()
  123.     {
  124.         if (n < 0)
  125.         {
  126.             cout << "Prazno." << endl;
  127.         }
  128.         else
  129.         {
  130.             return p[n--];
  131.         }
  132.     }
  133. };
  134.  
  135. int main()
  136. {
  137.     stog<int> a(10);
  138.     stog<double> b(20);
  139.  
  140.     a.push(5);
  141.     a.push(3);
  142.     cout << a.pop() << endl;
  143.     cout << a.pop() << endl;
  144.  
  145.     b.push(3.4);
  146.     b.push(17.1);
  147.     b.push(-3.1);
  148.     cout << b.pop() << endl;
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement