sStack.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <cassert>
  7. #include <cstdlib>
  8. #include "sStack.h"
  9. #include "sCPU.h"
  10. #include "const_gurd.h"
  11. using std::cout;
  12. using std::endl;
  13. //========================================
  14. /*!
  15. Constructor of stack struct
  16. \param stack pointers
  17. */
  18. void Stack_constructor (sStack *stack) {
  19. stack->memory_guard_first = guard_first;
  20. stack->memory_guard_second = guard_second;
  21. stack->memory_guard_third = guard_third;
  22. stack->counter_change_number = 0;
  23. stack->top = 0;
  24. for (int i = 0; i < stack->MAX_STACK_SIZE; i++) {
  25. stack->data [i] = -999;
  26. }
  27. }
  28. //========================================
  29. /*!
  30. Destructor of stack struct
  31. \param stack pointers
  32. */
  33. void Stack_destructoin (sStack *stack) {
  34. stack->top = -100;
  35. stack->counter_change_number = 0;
  36. for (int i = 0; i < stack->MAX_STACK_SIZE; i++) {
  37. stack->data [i] = -100500;
  38. }
  39. }
  40. //========================================
  41. /*!
  42. The function is showing stack
  43. \param stack pointers
  44. */
  45. void sStack_show (const sStack *stack) {
  46. cout << "top = " << stack->top << endl;
  47. cout << "counter_change_number = " << stack->counter_change_number << endl;
  48. for (int i = 0; i < stack->MAX_STACK_SIZE; i++ ) {
  49. cout << "data " << i << " " << stack->data [i] << "\n";
  50. }
  51. cout << endl;
  52. }
  53. //========================================
  54. /*!
  55. The function is showing the last element of stack and
  56. do not remove it
  57. \param stack pointers
  58. \return the value of the last element in stack
  59. */
  60. double sStack_show_last_element (const sStack *stack) {
  61. if (sStack_size (stack) < 1 ) {
  62. processing_error (no_element);
  63. return 0;
  64. }
  65. double last_element = stack->data [(stack->top) - 1];
  66. #ifdef USE_SUPER_PUPER_GUARDS
  67. _ASSERT_OK_(sStack, stack)
  68. #endif
  69. return last_element;
  70. }
  71. //========================================
  72. /*!
  73. THe function is showing the size of stack
  74. \param stack pointers
  75. */
  76. int sStack_size (const sStack *stack) {
  77. //cout << "Stack size: " << stack->top << endl;
  78. #ifdef USE_SUPER_PUPER_GUARDS
  79. _ASSERT_OK_(sStack, stack)
  80. #endif
  81. return stack->top;
  82. }
  83. //========================================
  84. /*!
  85. The function add one more number in stack
  86. \param stack pointers
  87. \param vell the new number
  88. */
  89. void sStack_Push (sStack *stack, double vell) {
  90. #ifdef USE_SUPER_PUPER_GUARDS
  91. _ASSERT_OK_(sStack, stack)
  92. #endif
  93. if (stack->data [stack->top] == -999 ) {
  94. stack->counter_change_number++;
  95. }
  96. stack->data [stack->top++] = vell;
  97. #ifdef USE_SUPER_PUPER_GUARDS
  98. _ASSERT_OK_(sStack, stack)
  99. #endif
  100. /*
  101. if (!sStack_Ok (stack)) {
  102. sStack_show (stack);
  103. _DEBUG_ASSERT_("Stack overflow!");
  104. }
  105. */
  106. }
  107. //========================================
  108. /*!
  109. The function return the last element in stack
  110. \param stack pointers
  111. \return the last element in stack
  112. */
  113. double sStack_Pop (sStack *stack) {
  114. double last_element = stack->data [(stack->top) - 1];
  115. //cout << "last_element = " << last_element << endl;
  116. stack->data [(stack->top) - 1] = 0;
  117. stack->top--;
  118. #ifdef USE_SUPER_PUPER_GUARDS
  119. _ASSERT_OK_(sStack, stack)
  120. #endif
  121. return last_element;
  122. }
  123. //========================================
  124. /*!
  125. The function Check stack
  126. \param stack pointers
  127. \return bool parameter true io all OK
  128. */
  129. bool sStack_Ok (const sStack *stack) {
  130. if ((stack->top < 0) || (stack->top > stack->MAX_STACK_SIZE)) {
  131. return false;
  132. }
  133. if (stack->memory_guard_first != guard_first ||
  134. stack->memory_guard_second != guard_second || stack->memory_guard_third != guard_third) {
  135. cout << "!!! boundary constants are change !!! \n";
  136. return false;
  137. }
  138. int counter = 0;
  139. for (int i = 0; i < stack->MAX_STACK_SIZE; i++) {
  140. if (stack->data [i] == -999) {
  141. counter++;
  142. }
  143. }
  144. if ((stack->MAX_STACK_SIZE - stack->counter_change_number) != counter) {
  145. cout << "!!!WARNING!!! counter_change_number != counter /n";
  146. }
  147. return true;
  148. }