123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- #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;
- //=======================================
- /*!
- 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;
- }
|