Pp9

zz3

Pp9
Mar 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. // Z3.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10. class Stos
  11. {
  12. int *tab;
  13. int rozmiar, wierzcholek;
  14.  
  15. public:
  16.  
  17. //inicjacja stosu, który jest tablica int
  18. void init(int x)
  19. {
  20. tab = new int[x];
  21. wierzcholek = -1;
  22. rozmiar = x;
  23. }
  24.  
  25. //metoda usuwajaca tablice z pamieci
  26. void destroy()
  27. {
  28. delete tab;
  29. }
  30.  
  31. //metoda umieszczajaca liczbe na tablica
  32. void push(int y)
  33. {
  34. if (!full()) tab[++wierzcholek] = y;
  35. else cout << "Stos jest pelny, nic nie moge dopisac!" << endl;
  36. }
  37.  
  38. //metoda zdejmujaca liczbe z wierzcholka
  39. void pop()
  40. {
  41. --wierzcholek;
  42. }
  43.  
  44. //metoda zwracajaca liczbe z wierzcholka stosu
  45. int top()
  46. {
  47. if (empty())
  48. {
  49. cout << "Nie moge przeczytac liczby, bo stos jest pusty!" << endl;
  50. }
  51. return tab[wierzcholek];
  52. }
  53.  
  54. //metoda zwracajaca wartosc 1 dla pustego stosu lub 0 dla nie pustego
  55. int empty()
  56. {
  57. if (wierzcholek == -1) return 1;
  58. else return 0;
  59. }
  60.  
  61. //metoda zwracajaca wartosc 1 dla stosu pelnego lub 0 dla niepelnego
  62. int full()
  63. {
  64. if (wierzcholek == (rozmiar - 1)) return 1;
  65. else return 0;
  66. }
  67.  
  68. };
  69.  
  70. int _tmain(int argc, _TCHAR* argv[])
  71. {
  72. Stos s1, s2;
  73. int maksymalny_rozmiar = 10;
  74. s1.init(maksymalny_rozmiar);
  75. s2.init(maksymalny_rozmiar);
  76.  
  77. for (int i = 1; i <= maksymalny_rozmiar; i++)
  78. {
  79. char bufor[50];
  80. int liczba;
  81. cout << "Podaj " << i << " liczbe: ";
  82. cin >> bufor;
  83. s1.push(liczba = atoi(bufor));
  84. }
  85.  
  86. int licznik = maksymalny_rozmiar;
  87. while (!s1.empty())
  88. {
  89.  
  90. s2.push(s1.top());
  91. s1.pop();
  92. cout << licznik-- << " liczba to: " << s2.top() << endl;
  93. }
  94. while (!s1.full())
  95. {
  96. s1.push(s2.top());
  97. s2.pop();
  98. }
  99. s1.destroy();
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment