Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. size_t TAB_SIZE_MAX = 10;
  4. size_t TAB_SIZE_MIN = 1;
  5.  
  6. class BaseArraySizeException {
  7. protected:
  8. std::size_t size_;
  9. public:
  10. BaseArraySizeException(std::size_t size) : size_(size) {}
  11. ~BaseArraySizeException() {}
  12. virtual void printError() const = 0;
  13. };
  14. class TooSmallArrayException : public BaseArraySizeException {
  15. public:
  16. TooSmallArrayException(std::size_t size) : BaseArraySizeException(size) {}
  17. void printError() const {
  18. std::cerr << "Rozmiar tablicy: " << size_ << " jest mniejszy niz "
  19. << TAB_SIZE_MIN << "!" << std::endl;
  20. }
  21. };
  22. class TooBigArrayException : public BaseArraySizeException {
  23. public:
  24. TooBigArrayException(std::size_t size) : BaseArraySizeException(size) {}
  25. void printError() const {
  26. std::cerr << "Rozmiar tablicy: " << size_ << " jest wiekszy niz " << TAB_SIZE_MAX << "!" << std::endl;
  27. }
  28. };
  29.  
  30. template <typename T>
  31. class Queue {
  32. private:
  33. int front, rear, max;
  34. T *q;
  35. public:
  36. Queue(int size = 0) {
  37. front = rear = 0;
  38. max = size;
  39. try {
  40. q = new T[size];
  41. } catch (const std::bad_alloc &e) {
  42. std::cerr << "Nie udalo sie zaalokowac pamieci: " << e.what() << std::endl;
  43. }
  44. }
  45. int push(T);
  46. T pop();
  47. ~Queue() {
  48. delete[]q;
  49. }
  50. };
  51.  
  52.  
  53. template <typename T>
  54. int Queue<T>::push(T elem) {
  55. if (front >= max)
  56. throw TooBigArrayException;
  57. q[front] = elem; front++;
  58. }
  59.  
  60. template <typename T>
  61. T Queue<T>::pop() {
  62. if (front <= 0) {
  63. return -1;
  64. }
  65. T result = q[rear];
  66. ++rear;
  67. return result;
  68.  
  69. }
  70. int main() {
  71.  
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement