fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class DataException
  5. {
  6. public:
  7. virtual void println() const = 0;
  8. };
  9.  
  10. class EmptyException : public DataException
  11. {
  12. public:
  13. EmptyException() {}
  14. void println() const { cout << "Error: Empty" << endl; }
  15. };
  16.  
  17. class FullyException : public DataException
  18. {
  19. char ch;
  20. public:
  21. FullyException(const char& c) { ch = c; }
  22. void println() const { cout << "Error: Fully '" << ch << '\'' << endl; }
  23. };
  24.  
  25. #define QUEUESTACK_SIZE (3)
  26.  
  27. class BaseClass
  28. {
  29. protected:
  30. char data[QUEUESTACK_SIZE];
  31. int lastindex;
  32. int count;
  33. BaseClass() { lastindex = 0; count = 0; }
  34. inline char addLast(const char& value) throw(DataException) {
  35. if (count == QUEUESTACK_SIZE) { throw FullyException(value); }
  36. int index = lastindex;
  37. lastindex = (lastindex + 1) % QUEUESTACK_SIZE;
  38. count++;
  39. return data[index] = value;
  40. }
  41. };
  42.  
  43. class Queue : public virtual BaseClass
  44. {
  45. protected:
  46. int firstindex;
  47. public:
  48. Queue() { firstindex = 0; }
  49. char enqueue(const char& value) throw(DataException) {
  50. return addLast(value);
  51. }
  52. char dequeue() throw(DataException) {
  53. if (count == 0) { throw EmptyException(); }
  54. int index = firstindex;
  55. firstindex = (firstindex + 1) % QUEUESTACK_SIZE;
  56. count--;
  57. return data[index];
  58. }
  59. };
  60.  
  61. class Stack : public virtual BaseClass
  62. {
  63. public:
  64. char pop() throw(DataException) {
  65. if (count == 0) { throw EmptyException(); }
  66. lastindex = (lastindex - 1 + QUEUESTACK_SIZE) % QUEUESTACK_SIZE;
  67. count--;
  68. return data[lastindex];
  69. }
  70. char push(const char& value) throw(DataException) {
  71. return addLast(value);
  72. }
  73. };
  74.  
  75. class QueueStack : public Queue, public Stack
  76. {
  77. public:
  78. QueueStack() {}
  79. } queue, stack;
  80.  
  81. int main() {
  82.  
  83. cout << "Queue Test" << endl;
  84. try {
  85. cout << "enqueue: " << queue.enqueue('a') << endl;
  86. cout << "enqueue: " << queue.enqueue('b') << endl;
  87. cout << "enqueue: " << queue.enqueue('c') << endl;
  88. cout << "enqueue: " << queue.enqueue('d') << endl;
  89. } catch (DataException& ex) {
  90. ex.println();
  91. }
  92. try {
  93. cout << "dequeue: " << queue.dequeue() << endl;
  94. cout << "dequeue: " << queue.dequeue() << endl;
  95. cout << "dequeue: " << queue.dequeue() << endl;
  96. cout << "dequeue: " << queue.dequeue() << endl;
  97. } catch (DataException& ex) {
  98. ex.println();
  99. }
  100.  
  101. cout << "Stack Test" << endl;
  102. try {
  103. cout << "push: " << stack.push('a') << endl;
  104. cout << "push: " << stack.push('b') << endl;
  105. cout << "push: " << stack.push('c') << endl;
  106. cout << "push: " << stack.push('d') << endl;
  107. } catch (DataException& ex) {
  108. ex.println();
  109. }
  110. try {
  111. cout << "pop: " << stack.pop() << endl;
  112. cout << "pop: " << stack.pop() << endl;
  113. cout << "pop: " << stack.pop() << endl;
  114. cout << "pop: " << stack.pop() << endl;
  115. } catch (DataException& ex) {
  116. ex.println();
  117. }
  118.  
  119. return 0;
  120. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Queue Test
enqueue: a
enqueue: b
enqueue: c
Error: Fully 'd'
dequeue: a
dequeue: b
dequeue: c
Error: Empty
Stack Test
push: a
push: b
push: c
Error: Fully 'd'
pop: c
pop: b
pop: a
Error: Empty