sCPU.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <assert.h>
  7. #include <cstdlib>
  8. #include "sCPU.h"
  9. #include "const_gurd.h"
  10. using std::cout;
  11. using std::endl;
  12. //=======================================
  13. /*!
  14. Constructor of CPU stuct
  15. \param cpu1, stack pointers to CPU and stack
  16. */
  17. void cpu_constructor (sCPU *cpu1, sStack *stack) {
  18. cpu1->st = stack;
  19. }
  20. //========================================
  21. /*!
  22. Check CPU
  23. \param cpu1 pointers to CPU
  24. \return bool parameter true if all OK
  25. */
  26. bool sCPU_Ok (sCPU *cpu1) {
  27. if (!sStack_Ok (cpu1->st)) {
  28. return false;
  29. }
  30. return true;
  31. }
  32. //========================================
  33. /*!
  34. calculate the sine of last element in stack
  35. \param cpu pointers to CPU
  36. \return rezalt sine of number
  37. */
  38. double sCPU_sinus (sCPU *cpu) {
  39. #ifdef USE_SUPER_PUPER_GUARDS
  40. _ASSERT_OK_(sCPU, cpu)
  41. #endif
  42. if (sStack_size (cpu->st) < 1 ) {
  43. processing_error (no_element);
  44. return 0;
  45. }
  46. double rezalt = my_sinus (sStack_Pop (cpu->st));
  47. sStack_Push (cpu->st, rezalt);
  48. #ifdef USE_SUPER_PUPER_GUARDS
  49. _ASSERT_OK_(sCPU, cpu)
  50. #endif
  51. return rezalt;
  52. }
  53. //=======================================
  54. /*!
  55. calculate the cosine of last element in stack
  56. \param cpu pointers to CPU
  57. \return rezalt cosine of number
  58. */
  59. double sCPU_cosine (sCPU *cpu) {
  60. #ifdef USE_SUPER_PUPER_GUARDS
  61. _ASSERT_OK_(sCPU, cpu)
  62. #endif
  63. if (sStack_size (cpu->st) < 1 ) {
  64. processing_error (no_element);
  65. return 0;
  66. }
  67. double result = my_cosine (sStack_Pop (cpu->st));
  68. sStack_Push (cpu->st, result);
  69. #ifdef USE_SUPER_PUPER_GUARDS
  70. _ASSERT_OK_(sCPU, cpu)
  71. #endif
  72. return result;
  73. }
  74. //=======================================
  75. /*!
  76. calculate the addition of the two last elements in the stack
  77. \param cpu pointers to CPU
  78. \return summer of tow nummbers
  79. */
  80. double sCPU_addition (sCPU *cpu) {
  81. #ifdef USE_SUPER_PUPER_GUARDS
  82. _ASSERT_OK_(sCPU, cpu)
  83. #endif
  84. if (sStack_size (cpu->st) < 2 ) {
  85. processing_error (not_enouth_number);
  86. return 0;
  87. }
  88. double element1 = sStack_Pop (cpu->st);
  89. double element2 = sStack_Pop (cpu->st);
  90. double result = (element1 + element2);
  91. sStack_Push (cpu->st, result);
  92. #ifdef USE_SUPER_PUPER_GUARDS
  93. _ASSERT_OK_(sCPU, cpu)
  94. #endif
  95. return result;
  96. }
  97. //=======================================
  98. /*!
  99. calculate the subtraction of the two last elements in the stack
  100. \param cpu pointers to CPU
  101. \return subtraction of tow nummbers
  102. */
  103. double sCPU_subtraction (sCPU *cpu) {
  104. #ifdef USE_SUPER_PUPER_GUARDS
  105. _ASSERT_OK_(sCPU, cpu)
  106. #endif
  107. if (sStack_size (cpu->st) < 2 ) {
  108. processing_error (not_enouth_number);
  109. return 0;
  110. }
  111. double element1 = sStack_Pop (cpu->st);
  112. double element2 = sStack_Pop (cpu->st);
  113. double result = (element2 - element1);
  114. sStack_Push (cpu->st, result);
  115. #ifdef USE_SUPER_PUPER_GUARDS
  116. _ASSERT_OK_(sCPU, cpu)
  117. #endif
  118. return result;
  119. }
  120. //=======================================
  121. /*!
  122. calculate the multiplication of the two last elements in the stack
  123. \param cpu pointers to CPU
  124. \return multiplication of tow nummbers
  125. */
  126. double sCPU_multiplication (sCPU *cpu) {
  127. #ifdef USE_SUPER_PUPER_GUARDS
  128. _ASSERT_OK_(sCPU, cpu)
  129. #endif
  130. if (sStack_size (cpu->st) < 2 ) {
  131. processing_error (not_enouth_number);
  132. return 0;
  133. }
  134. double element1 = sStack_Pop (cpu->st);
  135. double element2 = sStack_Pop (cpu->st);
  136. double result = (element2 * element1);
  137. sStack_Push (cpu->st, result);
  138. #ifdef USE_SUPER_PUPER_GUARDS
  139. _ASSERT_OK_(sCPU, cpu)
  140. #endif
  141. return result;
  142. }
  143. //======================================
  144. /*!
  145. Show the CPU and stack
  146. \param cpu pointers to CPU
  147. */
  148. void sCPU_show (const sCPU *cpu) {
  149. sStack_show (cpu->st);
  150. }
  151. //=======================================
  152. /*!
  153. calculate sine in degrees
  154. \param _number parametet of sine
  155. \return rezalt of sine
  156. */
  157. double my_sinus (double _number) {
  158. return sin (_number*M_PI/180);
  159. }
  160. //=======================================
  161. /*!
  162. calculate cosine in degrees
  163. \param parametet of cosine
  164. \return rezalt of cosine
  165. */
  166. double my_cosine (double _number) {
  167. return cos (_number*M_PI/180);
  168. }
  169. //======================================
  170. /*!
  171. treats errors
  172. \param number of error
  173. \return bool parameter
  174. */
  175. bool processing_error (const double namb_0f_error) {
  176. if (namb_0f_error == not_enouth_number) {
  177. cout << "Not enough numbers in the stack \n";
  178. return true;
  179. }
  180. if (namb_0f_error == no_element) {
  181. cout << "No element in the stack \n";
  182. return true;
  183. }
  184. return false;
  185. }