Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int MAX = 100;
  4. /////////////////////////////////////////////////////////////
  5. template <class Type>
  6. class Stack
  7. {
  8. private:
  9. Type * st = new Type[MAX];
  10. int top; // индекс вершины стека
  11. public:
  12. Stack(); // конструктор
  13. void push(Type var) // занести число в стек
  14. {
  15. st[++top] = var;
  16. }
  17. Type pop(); // взять число из стека
  18. Stack(const Stack &n);
  19. Stack(int n);
  20. ~Stack();
  21. operator bool()const;
  22. void operator +(Type x);
  23.  
  24.  
  25.  
  26. Type operator --(int)
  27. {
  28. return st[top--];
  29.  
  30. }
  31.  
  32.  
  33. void operator = (Stack &n);
  34. };
  35. /////////////////////////////////////////////////////////////
  36. template<class Type>
  37. Stack<Type>::Stack() // конструктор по умолчанию
  38. {
  39. top = -1;
  40. cout << "конструктор по умолчанию";
  41. }
  42. //---------------------------------------------------------—
  43. template<class Type>
  44. Type Stack<Type>::pop() // взять число из стека
  45. {
  46. return st[top--];
  47. }
  48. template<class Type>
  49. Stack<Type>::Stack(const Stack &n) // конструктор копирования
  50. {
  51. top = n.top;
  52. Type *s = new Type[top];
  53. for (int i = top; i >= 0; i--) {
  54. s[top - i] = n.st[i];
  55. }
  56. for (int i = 0; i <= top; i++) {
  57. st[top - i] = s[i];
  58. }
  59. }
  60. template<class Type>
  61. Stack<Type>::Stack(int n) // конструктор инициализации
  62. {
  63. top = -1;
  64. Type var;
  65. cout << "Вам необходимо ввести с клавиатуры значения, в количестве = " << n << "раз" << endl;
  66. for (int i = 0; i < n; i++) {
  67. cin >> var;
  68. push(var);
  69. }
  70. cout << "конструктор инициализации";
  71. }
  72. /*template<class Type>
  73. Stack<Type>::Stack(const Stack &n) // конструктор копирования
  74. {
  75. top = n.top;
  76.  
  77. for (i = 0; i < top; i++) {
  78. st[i] = n.st[i]);
  79. }
  80. }*/
  81. template<class Type>
  82. Stack<Type>::~Stack() // деструктор
  83. {
  84. delete[] st;
  85.  
  86. cout << "деструктор";
  87. }
  88. template<class Type>
  89. Stack<Type>::operator bool()const
  90. {
  91. cout << "оператор проверки на пустоту пустой/непустой - 1/0\n";
  92. if (top == -1) {
  93. f = false;
  94. return f;
  95. }
  96. else
  97. {
  98. f = true;
  99. return f;
  100. }
  101. return 0;
  102. }
  103. template<class Type>
  104. void Stack<Type>::operator +(Type x)
  105. {
  106. st[++top] = x;
  107. }
  108.  
  109. template<class Type>
  110. void Stack<Type>::operator =(Stack &n)
  111. {
  112. top = n.top;
  113. Type *s = new Type[top];
  114. for (int i = top; i >= 0; i--) {
  115. s[top -i] = n.st[i];
  116. }
  117. for (int i = 0; i <= top; i++) {
  118. st[top - i] = s[i];
  119. }
  120. }
  121. //---------------------------------------------------------—
  122. int main()
  123. {
  124. setlocale(LC_ALL, "Russian");
  125. setlocale(LC_NUMERIC, "C");
  126.  
  127. Stack<float> s1; // s1 - объект класса Stack<float>
  128. s1.push(1111.1F); // занести 3 float, вытолкнуть 3 float
  129. s1.push(2222.2F);
  130. s1.push(3333.3F);
  131. cout << bool(s1);
  132. cout << "1: " << s1.pop() << endl;
  133. cout << "2: " << s1.pop() << endl;
  134. cout << "3: " << s1.pop() << endl;
  135. cout << bool(s1);
  136. Stack<long> s2; // s2 - объект класса Stack<long>
  137. s2.push(123123123L); // занести 3 long, вытолкнуть 3 long
  138. s2.push(234234234L);
  139. s2.push(345345345L);
  140. cout << "1: " << s2.pop() << endl;
  141. cout << "2: " << s2.pop() << endl;
  142. cout << "3: " << s2.pop() << endl;
  143. Stack<int> s3(4); // s2 - объект класса Stack<long>
  144. cout << endl << s3-- << endl;
  145. s3 + 8;
  146. Stack<int> p = s3;
  147. cout << "1: " << s3.pop() << endl;
  148. cout << "2: " << s3.pop() << endl;
  149. cout << "3: " << s3.pop() << endl;
  150. cout << "4: " << s3.pop() << endl;
  151. cout << "1: " << p.pop() << endl;
  152. cout << "2: " << p.pop() << endl;
  153. cout << "3: " << p.pop() << endl;
  154. cout << "4: " << p.pop() << endl;
  155.  
  156. system("pause");
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement