Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Item
  6. {
  7. string name;
  8.  
  9. public:
  10. Item() {}
  11. Item(string name) { this->name = name; }
  12. };
  13.  
  14. class Stack
  15. {
  16. protected:
  17. virtual void push(Item* obj) = 0;
  18. virtual void pop() = 0;
  19. virtual Item* get() = 0;
  20. };
  21.  
  22. class ArrayStack : public Stack
  23. {
  24. int maxsize, size;
  25. Item* tab;
  26.  
  27. public:
  28. ArrayStack(int maxsize)
  29. {
  30. if(maxsize<0) { throw "WrongSizeException"; }
  31. size = 0;
  32. this->maxsize = maxsize; tab = new Item[maxsize];
  33. }
  34.  
  35. ~ArrayStack() { delete[] tab; };
  36.  
  37. virtual void push(Item* obj) // umieszcza el na stosie
  38. {
  39. if(size == maxsize) { throw "StackFullException"; }
  40. else { tab[size] = *obj; size++; }
  41. }
  42.  
  43. virtual void pop() // uswa el ze stosu
  44. {
  45. if(size == 0) { throw "StackEmptyException"; }
  46. else { size--; }
  47. }
  48.  
  49. virtual Item* get() // zwraca el ze stosu
  50. {
  51. if(size == 0) { throw "StackEmptyException"; }
  52. else { return &tab[size]; }
  53. }
  54. };
  55.  
  56. class DynamicArrayStack : public Stack
  57. {
  58. int maxsize, size;
  59. Item* tab;
  60.  
  61. public:
  62. DynamicArrayStack(int starting_size = 0)
  63. {
  64. starting_size = maxsize;
  65.  
  66. if(starting_size<0) { throw "WrongSizeException"; }
  67. size = 0;
  68. tab = new Item[starting_size];
  69. }
  70.  
  71. virtual void push(Item* obj) // umieszcza el na stosie
  72. {
  73. if(size == maxsize)
  74. {
  75. Item *temp = tab;
  76. tab = new Item[maxsize*2];
  77. maxsize = maxsize*2;
  78. for(int i=0; i<maxsize/2; i++)
  79. {
  80. tab[i] = temp[i];
  81. }
  82.  
  83. tab[size] = *obj; size++;
  84. }
  85. else
  86. {
  87. tab[size] = *obj; size++;
  88. }
  89. }
  90.  
  91. virtual void pop() // uswa el ze stosu
  92. {
  93. if(size == 0) { throw "StackEmptyException"; }
  94. else { size--; }
  95. }
  96.  
  97. virtual Item* get() // zwraca el ze stosu
  98. {
  99. if(size == 0) { throw "StackEmptyException"; }
  100. else { return &tab[size]; }
  101. }
  102. };
  103.  
  104. int main()
  105. {
  106. Item sword("Excalibur");
  107. Item axe("Destroyer");
  108.  
  109. try
  110. {
  111. DynamicArrayStack bag(3);
  112. bag.push(&sword);
  113. bag.push(&axe);
  114. bag.push(&axe);
  115. bag.push(&axe);
  116. bag.push(&axe);
  117. }
  118. catch(const char* e)
  119. {
  120. cout << e << endl; // gdy podany rozmiar stosu < 0
  121. }
  122. catch(const char* e)
  123. {
  124. cout << e << endl; // użytkownik usiłuje dodać element na pełen stos
  125. }
  126. catch(const char* e)
  127. {
  128. cout << e << endl; // użytkownik usiłuje pobrać lub usunąć element pustego stosu
  129. }
  130.  
  131.  
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement