#include #include #include #include #include #include #include #include "sCPU.h" #include "const_gurd.h" using std::cout; using std::endl; //======================================= /*! Constructor of CPU stuct \param cpu1, stack pointers to CPU and stack */ void cpu_constructor (sCPU *cpu1, sStack *stack) { cpu1->st = stack; } //======================================== /*! Check CPU \param cpu1 pointers to CPU \return bool parameter true if all OK */ bool sCPU_Ok (sCPU *cpu1) { if (!sStack_Ok (cpu1->st)) { return false; } return true; } //======================================== /*! calculate the sine of last element in stack \param cpu pointers to CPU \return rezalt sine of number */ 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; } //======================================= /*! calculate the cosine of last element in stack \param cpu pointers to CPU \return rezalt cosine of number */ 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; } //======================================= /*! calculate the addition of the two last elements in the stack \param cpu pointers to CPU \return summer of tow nummbers */ 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; } //======================================= /*! calculate the subtraction of the two last elements in the stack \param cpu pointers to CPU \return subtraction of tow nummbers */ 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; } //======================================= /*! calculate the multiplication of the two last elements in the stack \param cpu pointers to CPU \return multiplication of tow nummbers */ 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; } //====================================== /*! Show the CPU and stack \param cpu pointers to CPU */ void sCPU_show (const sCPU *cpu) { sStack_show (cpu->st); } //======================================= /*! calculate sine in degrees \param _number parametet of sine \return rezalt of sine */ double my_sinus (double _number) { return sin (_number*M_PI/180); } //======================================= /*! calculate cosine in degrees \param parametet of cosine \return rezalt of cosine */ double my_cosine (double _number) { return cos (_number*M_PI/180); } //====================================== /*! treats errors \param number of error \return bool parameter */ 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; }