|
@@ -0,0 +1,202 @@
|
|
|
+#include <iostream>
|
|
|
+#include <string>
|
|
|
+#include <cstdio>
|
|
|
+#include <cmath>
|
|
|
+#include <cstring>
|
|
|
+#include <assert.h>
|
|
|
+#include <cstdlib>
|
|
|
+
|
|
|
+#include "sCPU.h"
|
|
|
+#include "const_gurd.h"
|
|
|
+
|
|
|
+
|
|
|
+using std::cout;
|
|
|
+using std::endl;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+//=======================================
|
|
|
+void cpu_constructor (sCPU *cpu1, sStack *stack) {
|
|
|
+
|
|
|
+ cpu1->st = stack;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//========================================
|
|
|
+bool sCPU_Ok (sCPU *cpu1) {
|
|
|
+
|
|
|
+ if (!sStack_Ok (cpu1->st)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+//========================================
|
|
|
+double sCPU_sinus (sCPU *cpu) {
|
|
|
+
|
|
|
+#ifdef USE_SUPER_PUPER_GUARDS
|
|
|
+ _ASSERT_OK_(sCPU, cpu)
|
|
|
+#endif
|
|
|
+
|
|
|
+ if (sStack_size (cpu->st) < 1 ) {
|
|
|
+ processing_error (no_element);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ double rezalt = my_sinus (sStack_Pop (cpu->st));
|
|
|
+ sStack_Push (cpu->st, rezalt);
|
|
|
+
|
|
|
+#ifdef USE_SUPER_PUPER_GUARDS
|
|
|
+ _ASSERT_OK_(sCPU, cpu)
|
|
|
+#endif
|
|
|
+
|
|
|
+ return rezalt;
|
|
|
+}
|
|
|
+
|
|
|
+//=======================================
|
|
|
+double sCPU_cosine (sCPU *cpu) {
|
|
|
+
|
|
|
+#ifdef USE_SUPER_PUPER_GUARDS
|
|
|
+ _ASSERT_OK_(sCPU, cpu)
|
|
|
+#endif
|
|
|
+
|
|
|
+ if (sStack_size (cpu->st) < 1 ) {
|
|
|
+ processing_error (no_element);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ double result = my_cosine (sStack_Pop (cpu->st));
|
|
|
+ sStack_Push (cpu->st, result);
|
|
|
+
|
|
|
+#ifdef USE_SUPER_PUPER_GUARDS
|
|
|
+ _ASSERT_OK_(sCPU, cpu)
|
|
|
+#endif
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+//=======================================
|
|
|
+double sCPU_addition (sCPU *cpu) {
|
|
|
+
|
|
|
+#ifdef USE_SUPER_PUPER_GUARDS
|
|
|
+ _ASSERT_OK_(sCPU, cpu)
|
|
|
+#endif
|
|
|
+
|
|
|
+ if (sStack_size (cpu->st) < 2 ) {
|
|
|
+ processing_error (not_enouth_number);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ double element1 = sStack_Pop (cpu->st);
|
|
|
+ double element2 = sStack_Pop (cpu->st);
|
|
|
+ double result = (element1 + element2);
|
|
|
+
|
|
|
+ sStack_Push (cpu->st, result);
|
|
|
+
|
|
|
+#ifdef USE_SUPER_PUPER_GUARDS
|
|
|
+ _ASSERT_OK_(sCPU, cpu)
|
|
|
+#endif
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+//=======================================
|
|
|
+double sCPU_subtraction (sCPU *cpu) {
|
|
|
+
|
|
|
+#ifdef USE_SUPER_PUPER_GUARDS
|
|
|
+ _ASSERT_OK_(sCPU, cpu)
|
|
|
+#endif
|
|
|
+
|
|
|
+ if (sStack_size (cpu->st) < 2 ) {
|
|
|
+ processing_error (not_enouth_number);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ double element1 = sStack_Pop (cpu->st);
|
|
|
+ double element2 = sStack_Pop (cpu->st);
|
|
|
+ double result = (element2 - element1);
|
|
|
+
|
|
|
+ sStack_Push (cpu->st, result);
|
|
|
+
|
|
|
+#ifdef USE_SUPER_PUPER_GUARDS
|
|
|
+ _ASSERT_OK_(sCPU, cpu)
|
|
|
+#endif
|
|
|
+
|
|
|
+ return result;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//=======================================
|
|
|
+double sCPU_multiplication (sCPU *cpu) {
|
|
|
+
|
|
|
+#ifdef USE_SUPER_PUPER_GUARDS
|
|
|
+ _ASSERT_OK_(sCPU, cpu)
|
|
|
+#endif
|
|
|
+
|
|
|
+ if (sStack_size (cpu->st) < 2 ) {
|
|
|
+ processing_error (not_enouth_number);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ double element1 = sStack_Pop (cpu->st);
|
|
|
+ double element2 = sStack_Pop (cpu->st);
|
|
|
+ double result = (element2 * element1);
|
|
|
+
|
|
|
+ sStack_Push (cpu->st, result);
|
|
|
+
|
|
|
+#ifdef USE_SUPER_PUPER_GUARDS
|
|
|
+ _ASSERT_OK_(sCPU, cpu)
|
|
|
+#endif
|
|
|
+
|
|
|
+ return result;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//======================================
|
|
|
+void sCPU_show (const sCPU *cpu) {
|
|
|
+
|
|
|
+ sStack_show (cpu->st);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//=====================================
|
|
|
+bool sCPU_Ok (const sCPU *cpu) {
|
|
|
+
|
|
|
+ if (!sStack_Ok (cpu->st)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+//=======================================
|
|
|
+double my_sinus (double _number) {
|
|
|
+
|
|
|
+ return sin (_number*M_PI/180);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//=======================================
|
|
|
+double my_cosine (double _number) {
|
|
|
+
|
|
|
+ return cos (_number*M_PI/180);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//======================================
|
|
|
+bool processing_error (const double namb_0f_error) {
|
|
|
+
|
|
|
+ if (namb_0f_error == not_enouth_number) {
|
|
|
+ cout << "Not enough numbers in the stack \n";
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (namb_0f_error == no_element) {
|
|
|
+ cout << "No element in the stack \n";
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|