123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #include <iostream>
- #include <string>
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- #include <cassert>
- #include <cstdlib>
- #include "sStack.h"
- #include "sCPU.h"
- #include "const_gurd.h"
- using std::cout;
- using std::endl;
- //========================================
- /*!
- Constructor of stack struct
- \param stack pointers
- */
- void Stack_constructor (sStack *stack) {
-
- stack->memory_guard_first = guard_first;
- stack->memory_guard_second = guard_second;
- stack->memory_guard_third = guard_third;
- stack->counter_change_number = 0;
-
- stack->top = 0;
-
- for (int i = 0; i < stack->MAX_STACK_SIZE; i++) {
- stack->data [i] = -999;
- }
- }
- //========================================
- /*!
- Destructor of stack struct
- \param stack pointers
- */
- void Stack_destructoin (sStack *stack) {
- stack->top = -100;
- stack->counter_change_number = 0;
-
- for (int i = 0; i < stack->MAX_STACK_SIZE; i++) {
- stack->data [i] = -100500;
- }
- }
- //========================================
- /*!
- The function is showing stack
- \param stack pointers
- */
- void sStack_show (const sStack *stack) {
- cout << "top = " << stack->top << endl;
- cout << "counter_change_number = " << stack->counter_change_number << endl;
- for (int i = 0; i < stack->MAX_STACK_SIZE; i++ ) {
-
- cout << "data " << i << " " << stack->data [i] << "\n";
- }
-
- cout << endl;
-
- }
- //========================================
- /*!
- The function is showing the last element of stack and
- do not remove it
- \param stack pointers
- \return the value of the last element in stack
- */
- double sStack_show_last_element (const sStack *stack) {
-
- if (sStack_size (stack) < 1 ) {
- processing_error (no_element);
- return 0;
- }
-
- double last_element = stack->data [(stack->top) - 1];
- #ifdef USE_SUPER_PUPER_GUARDS
- _ASSERT_OK_(sStack, stack)
- #endif
- return last_element;
- }
- //========================================
- /*!
- THe function is showing the size of stack
- \param stack pointers
- */
- int sStack_size (const sStack *stack) {
- //cout << "Stack size: " << stack->top << endl;
- #ifdef USE_SUPER_PUPER_GUARDS
- _ASSERT_OK_(sStack, stack)
- #endif
-
- return stack->top;
- }
- //========================================
- /*!
- The function add one more number in stack
- \param stack pointers
- \param vell the new number
- */
- void sStack_Push (sStack *stack, double vell) {
- #ifdef USE_SUPER_PUPER_GUARDS
- _ASSERT_OK_(sStack, stack)
- #endif
- if (stack->data [stack->top] == -999 ) {
- stack->counter_change_number++;
- }
-
- stack->data [stack->top++] = vell;
- #ifdef USE_SUPER_PUPER_GUARDS
- _ASSERT_OK_(sStack, stack)
- #endif
- /*
- if (!sStack_Ok (stack)) {
- sStack_show (stack);
- _DEBUG_ASSERT_("Stack overflow!");
- }
- */
- }
- //========================================
- /*!
- The function return the last element in stack
- \param stack pointers
- \return the last element in stack
- */
- double sStack_Pop (sStack *stack) {
-
- double last_element = stack->data [(stack->top) - 1];
- //cout << "last_element = " << last_element << endl;
- stack->data [(stack->top) - 1] = 0;
- stack->top--;
-
- #ifdef USE_SUPER_PUPER_GUARDS
- _ASSERT_OK_(sStack, stack)
- #endif
-
- return last_element;
- }
- //========================================
- /*!
- The function Check stack
- \param stack pointers
- \return bool parameter true io all OK
- */
- bool sStack_Ok (const sStack *stack) {
-
- if ((stack->top < 0) || (stack->top > stack->MAX_STACK_SIZE)) {
- return false;
- }
- if (stack->memory_guard_first != guard_first ||
- stack->memory_guard_second != guard_second || stack->memory_guard_third != guard_third) {
- cout << "!!! boundary constants are change !!! \n";
- return false;
- }
- int counter = 0;
- for (int i = 0; i < stack->MAX_STACK_SIZE; i++) {
- if (stack->data [i] == -999) {
- counter++;
- }
- }
-
- if ((stack->MAX_STACK_SIZE - stack->counter_change_number) != counter) {
- cout << "!!!WARNING!!! counter_change_number != counter /n";
- }
-
- return true;
- }
|