Advertisement
Guest User

stos szablon

a guest
May 27th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. // stos szablon.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. template<typename T>
  8. class CStos
  9. {
  10. T* m_iWartosci; //Tablica przechowywanych elementów
  11. int m_iIloscElem; //Ilość obecnie przechowywanych elementów
  12. public:
  13. CStos(void);
  14. ~CStos(void);
  15. void Wrzuc(T Element); //Wrzuć nowy element na stos
  16. T Zdejmij(void); //Zdejmij element ze stosu
  17. void Wyswietl(void); //Wyswietl zawartosc stosu
  18. void Wyczysc(void); //Usuń wszystkie dane ze stosu
  19. inline bool JestPusty(void) { return !m_iIloscElem; }
  20. inline int DajDlugosc(void) { return m_iIloscElem; }
  21. };
  22. template<typename T>
  23. CStos<T>::CStos(void) : m_iWartosci(0), m_iIloscElem(0){}
  24. template<typename T>
  25. CStos<T>::~CStos(void)
  26. {
  27. Wyczysc();
  28. }
  29. template<typename T>
  30. void CStos<T>::Wrzuc(T Element)
  31. {
  32. T* tmp = NULL;
  33. tmp = new T [++m_iIloscElem];
  34. tmp[0] = Element;
  35. for (int i = 1; i<m_iIloscElem; i++)
  36. tmp[i] = m_iWartosci[i - 1];
  37. delete[] m_iWartosci;
  38. m_iWartosci = tmp;
  39. }
  40. template<typename T>
  41. T CStos<T>::Zdejmij(void)
  42. {
  43. T Pobrany = 0;
  44. if (m_iWartosci != NULL) {
  45. Pobrany = m_iWartosci[0];
  46. if (--m_iIloscElem) {
  47. T* tmp = NULL;
  48. tmp = new T[m_iIloscElem]; //Pomniejszona tablica
  49. for (int i = 0; i<m_iIloscElem; i++)
  50. tmp[i] = m_iWartosci[i + 1]; //Kopiowanie do mniejszej
  51. delete[] m_iWartosci;
  52. m_iWartosci = tmp; //Podlaczanie nowej-mniejszej tablicy
  53. }
  54. else Wyczysc();
  55. }
  56. else std::cerr << "Brak elementow na stosie!" << std::endl;
  57. return Pobrany;
  58. }
  59. template<typename T>
  60. void CStos<T>::Wyswietl(void)
  61. {
  62. for (int i = 0; i<m_iIloscElem; i++) std::cout << m_iWartosci[i] << ", ";
  63. std::cout << std::endl;
  64. }
  65. template<typename T>
  66. void CStos<T>::Wyczysc(void)
  67. {
  68. if (m_iWartosci != NULL) delete[] m_iWartosci;
  69. m_iWartosci = NULL;
  70. m_iIloscElem = 0;
  71. }
  72.  
  73. int _tmain(int argc, _TCHAR* argv[])
  74. {
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement