Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>;
  5.  
  6. using namespace std;
  7.  
  8. class Stos
  9. {
  10. public:
  11. int *tab;//tablica przechowujaca elementy
  12. int wierzch, rozmiar;
  13.  
  14. Stos(int rozmiar)
  15. {
  16. cout << "Konstruktor 1-parametrowy z rozmiarem " << rozmiar << " o adresie: " << &tab << endl;
  17. }
  18.  
  19. Stos()
  20. {
  21. cout << "Konstruktor domyslny o adresie: " << &tab << endl;
  22. }
  23.  
  24. void init(Stos& s,int rozm)
  25. {
  26. s.tab = new int[rozm];
  27. s.rozmiar = rozm;
  28. s.wierzch = -1;
  29. }
  30. void destroy(Stos& s)
  31. {
  32. delete[] s.tab;
  33. }
  34. void push(Stos& s, int l)
  35. {
  36. s.tab[++s.wierzch] = l;
  37. }
  38. void pop(Stos& s)
  39. {
  40. --s.wierzch;
  41. }
  42. int top(Stos& s)
  43. {
  44. return s.tab[s.wierzch];
  45. }
  46. int empty(Stos& s)
  47. {
  48. return s.wierzch == -1;
  49. }
  50. int full(Stos& s)
  51. {
  52. return s.wierzch == - 1;
  53. }
  54.  
  55. ~Stos()
  56. {
  57. delete[]tab;
  58. cout << "Destruktor dla adresu: " << &tab << endl;
  59. }
  60. };
  61. int main()
  62. {
  63. Stos s1, s2;
  64. s1.init(s1, 10);
  65. s2.init(s2, 1000);
  66.  
  67. s1.push(s1, 1);
  68. s1.push(s1, 2);
  69. s1.push(s1, 3);
  70. s1.push(s1, 1000);
  71.  
  72. s1.pop(s1);
  73.  
  74. s2.push(s2, 3);
  75. s2.push(s2, 8);
  76. s2.push(s2, 1);
  77.  
  78. cout << "STOS s1:"<<endl;
  79. while (!s1.empty(s1))
  80. {
  81. cout << s1.top(s1) << endl;
  82. s1.pop(s1);
  83. }
  84.  
  85. cout << "STOS s2:" << endl;
  86. while (!s2.empty(s2))
  87. {
  88. cout << s2.top(s2) << endl;
  89. s2.pop(s2);
  90. }
  91.  
  92. s1.destroy(s1);
  93. s1.destroy(s2);
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement