Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Stos{
  6. public:
  7. int *tab, wierzch, rozmiar;
  8.  
  9. Stos(int rozmiar){
  10. cout << "Wywolano konstruktor 1-parametrowy z rozmiarem " << rozmiar << " o adresie: " << &tab << endl;
  11. }
  12.  
  13. Stos(){
  14. cout << "Wywolano konstruktor domyslny o adresie: " << &tab << endl;
  15. }
  16.  
  17. void init(Stos& s, int rozm){
  18. s.tab = new int[rozm];
  19. s.rozmiar = rozm;
  20. s.wierzch = -1;
  21. }
  22.  
  23. void destroy(Stos& s){delete[] s.tab;}
  24.  
  25. void push(Stos& s, int l){s.tab[++s.wierzch] = l;}
  26.  
  27. void pop(Stos& s){--s.wierzch;}
  28.  
  29. int top(Stos& s){return s.tab[s.wierzch];}
  30.  
  31. int empty(Stos& s){return s.wierzch == -1;}
  32.  
  33. int full(Stos& s){return s.wierzch == s.rozmiar -1;}
  34.  
  35. ~Stos(){
  36. delete []tab;
  37. // cout << "Wywolano destruktor" << endl;
  38. cout << "Wywolano destruktor dla adresu: " << &tab << endl;
  39. } //destruktor
  40.  
  41. }; //koniec klasy
  42.  
  43. void dodaj(Stos& s, int a) {
  44. s.push(s,a);
  45. }
  46.  
  47. int main()
  48. {
  49.  
  50. Stos s1(3);
  51. Stos s2;
  52. s1.init(s1,10);
  53. s2.init(s2,1000);
  54.  
  55. s1.push(s1,1);
  56. s1.push(s1,2);
  57. s1.push(s1,3);
  58.  
  59. while(!s1.empty(s1)){
  60. cout << s1.top(s1) << endl;
  61. s1.pop(s1);
  62. }
  63. s1.destroy(s1);
  64. s2.destroy(s2);
  65.  
  66. /*
  67. for(int i = 0; i < 1; i++){
  68. Stos s3(3);
  69. cout << "dziala";
  70. s3.push(s3,1);
  71. cout << s3.top(s3);
  72. }
  73. */
  74.  
  75. /*
  76. Stos s;
  77. s.init(s,10); //wywolana przeze mnie metoda aby kod zadzialal
  78. s.push(s,0);
  79. dodaj(s,1);
  80. dodaj(s,2);
  81. while (!s.empty(s)) {
  82. cout << s.top(s) << endl;
  83. s.pop(s);
  84. } // bledy w dzialaniu powoduje to, ze stos s jest niezainicjowany (dodano przeze mnie metode init dla obiektu s)
  85. */
  86.  
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement