allStack.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <assert.h>
  7. #include <cstdlib>
  8. #include "allStack.h"
  9. using std::cout;
  10. using std::endl;
  11. //========================================
  12. void Stack_constructor (sStack *stack) {
  13. stack->first_point = -100500;
  14. stack->second_point = 0;
  15. stack->third_point = 100500;
  16. stack->top = 0;
  17. stack->counter_change_number = 0;
  18. for (int i = 0; i < MAX_STACK_SIZE; i++) {
  19. stack->st [i] = -999;
  20. }
  21. }
  22. //========================================
  23. void Stack_destructoin (sStack *stack) {
  24. stack->top = 0;
  25. stack->counter_change_number = 0;
  26. for (int i = 0; i < MAX_STACK_SIZE; i++) {
  27. stack->st [i] = -100500;
  28. }
  29. }
  30. //========================================
  31. void sStack_show (sStack *stack) {
  32. cout << "top = " << stack->top << endl;
  33. cout << "counter_change_number = " << stack->counter_change_number << endl;
  34. for (int i = 0; i < MAX_STACK_SIZE; i++ ) {
  35. cout << "st" << i << " " << stack->st [i] << "\n";
  36. }
  37. cout << endl;
  38. }
  39. //========================================
  40. double sStack_show_last_element (sStack *stack) {
  41. if (sStack_size (stack) < 1 ) {
  42. processing_error (-444);
  43. return 0;
  44. }
  45. double last_element = stack->st [(stack->top) - 1];
  46. if (!sStack_Ok (stack)) {
  47. sStack_show (stack);
  48. assert (0);
  49. }
  50. return last_element;
  51. }
  52. //========================================
  53. int sStack_size (sStack *stack) {
  54. //cout << "Stack size: " << stack->top << endl;
  55. if (!sStack_Ok (stack)) {
  56. sStack_show (stack);
  57. assert (0);
  58. }
  59. return stack->top;
  60. }
  61. //========================================
  62. void sStack_Push (sStack *stack, double vell) {
  63. if (!sStack_Ok (stack)) {
  64. sStack_show (stack);
  65. assert (0);
  66. }
  67. if (stack->st [stack->top] == -999 ) {
  68. stack->counter_change_number++;
  69. }
  70. stack->st [stack->top++] = vell;
  71. if (!sStack_Ok (stack)) {
  72. sStack_show (stack);
  73. assert (("Push", 0));
  74. }
  75. }
  76. //========================================
  77. double sStack_Pop (sStack *stack) {
  78. double last_element = stack->st [(stack->top) - 1];
  79. //cout << "last_element = " << last_element << endl;
  80. stack->st [(stack->top) - 1] = 0;
  81. stack->top--;
  82. if (!sStack_Ok (stack)) {
  83. sStack_show (stack);
  84. assert (("Pop", 0));
  85. }
  86. return last_element;
  87. }
  88. //========================================
  89. bool sStack_Ok (sStack *stack) {
  90. if ((stack->top < 0) || (stack->top > MAX_STACK_SIZE)) {
  91. return false;
  92. }
  93. if (stack->first_point != -100500 ||
  94. stack->second_point != 0 || stack->third_point != 100500) {
  95. cout << "!!! boundary constants are change !!! \n";
  96. return false;
  97. }
  98. int counter = 0;
  99. for (int i = 0; i < MAX_STACK_SIZE; i++) {
  100. if (stack->st [i] == -999) {
  101. counter++;
  102. }
  103. }
  104. if ((MAX_STACK_SIZE - stack->counter_change_number) != counter) {
  105. cout << "!!!WARNING!!! counter_change_number != counter /n";
  106. }
  107. return true;
  108. }
  109. //=======================================
  110. void cpu_constructor (sCPU *cpu1, sStack *stack) {
  111. cpu1->s1 = stack;
  112. }
  113. //========================================
  114. bool sCPU_Ok (sCPU *cpu1) {
  115. if (!sStack_Ok (cpu1->s1)) {
  116. return false;
  117. }
  118. return true;
  119. }
  120. //========================================
  121. double sCPU_sinus (sCPU *cpu) {
  122. if (sStack_size (cpu->s1) < 1 ) {
  123. processing_error (-333);
  124. return 0;
  125. }
  126. double rezalt = my_sinus (sStack_Pop (cpu->s1));
  127. sStack_Push (cpu->s1, rezalt);
  128. if (!sCPU_Ok (cpu)) {
  129. sStack_show (cpu->s1);
  130. assert (("sin", 0));
  131. }
  132. return rezalt;
  133. }
  134. //=======================================
  135. double sCPU_cosine (sCPU *cpu) {
  136. if (sStack_size (cpu->s1) < 1 ) {
  137. processing_error (-333);
  138. return 0;
  139. }
  140. double result = my_cosine (sStack_Pop (cpu->s1));
  141. sStack_Push (cpu->s1, result);
  142. if (!sCPU_Ok (cpu)) {
  143. sStack_show (cpu->s1);
  144. assert (("cos", 0));
  145. }
  146. return result;
  147. }
  148. //=======================================
  149. double sCPU_addition (sCPU *cpu) {
  150. if (sStack_size (cpu->s1) < 2 ) {
  151. processing_error (-333);
  152. return 0;
  153. }
  154. double element1 = sStack_Pop (cpu->s1);
  155. double element2 = sStack_Pop (cpu->s1);
  156. double result = (element1 + element2);
  157. sStack_Push (cpu->s1, result);
  158. if (!sCPU_Ok (cpu)) {
  159. sStack_show (cpu->s1);
  160. assert (("addition", 0));
  161. }
  162. return result;
  163. }
  164. //=======================================
  165. double sCPU_subtraction (sCPU *cpu) {
  166. if (sStack_size (cpu->s1) < 2 ) {
  167. processing_error (-333);
  168. return 0;
  169. }
  170. double element1 = sStack_Pop (cpu->s1);
  171. double element2 = sStack_Pop (cpu->s1);
  172. double result = (element2 - element1);
  173. sStack_Push (cpu->s1, result);
  174. if (!sCPU_Ok (cpu)) {
  175. sStack_show (cpu->s1);
  176. assert (("subtraction", 0));
  177. }
  178. return result;
  179. }
  180. //=======================================
  181. double sCPU_multiplication (sCPU *cpu) {
  182. if (sStack_size (cpu->s1) < 2 ) {
  183. processing_error (-333);
  184. return 0;
  185. }
  186. double element1 = sStack_Pop (cpu->s1);
  187. double element2 = sStack_Pop (cpu->s1);
  188. double result = (element2 * element1);
  189. sStack_Push (cpu->s1, result);
  190. if (!sCPU_Ok (cpu)) {
  191. sStack_show (cpu->s1);
  192. assert (("multiplication", 0));
  193. }
  194. return result;
  195. }
  196. //=======================================
  197. double my_sinus (double _number) {
  198. return sin (_number*PI_CONST/180);
  199. }
  200. //=======================================
  201. double my_cosine (double _number) {
  202. return cos (_number*PI_CONST/180);
  203. }
  204. //======================================
  205. bool processing_error (const double namb_0f_error) {
  206. if (namb_0f_error == -333) {
  207. cout << "Not enough numbers in the stack \n";
  208. return true;
  209. }
  210. if (namb_0f_error == -444) {
  211. cout << "No element in the stack \n";
  212. return true;
  213. }
  214. return false;
  215. }