123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- #include <iostream>
- #include <string>
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- #include <assert.h>
- #include <cstdlib>
- #include "allStack.h"
- using std::cout;
- using std::endl;
- //========================================
- void Stack_constructor (sStack *stack) {
-
- stack->first_point = -100500;
- stack->second_point = 0;
- stack->third_point = 100500;
-
- stack->top = 0;
- stack->counter_change_number = 0;
-
- for (int i = 0; i < MAX_STACK_SIZE; i++) {
- stack->st [i] = -999;
- }
- }
- //========================================
- void Stack_destructoin (sStack *stack) {
- stack->top = 0;
- stack->counter_change_number = 0;
-
- for (int i = 0; i < MAX_STACK_SIZE; i++) {
- stack->st [i] = -100500;
- }
- }
- //========================================
- void sStack_show (sStack *stack) {
- cout << "top = " << stack->top << endl;
- cout << "counter_change_number = " << stack->counter_change_number << endl;
-
- for (int i = 0; i < MAX_STACK_SIZE; i++ ) {
- cout << "st" << i << " " << stack->st [i] << "\n";
- }
- cout << endl;
-
- }
- //========================================
- double sStack_show_last_element (sStack *stack) {
-
- if (sStack_size (stack) < 1 ) {
- processing_error (-444);
- return 0;
- }
-
- double last_element = stack->st [(stack->top) - 1];
-
- if (!sStack_Ok (stack)) {
- sStack_show (stack);
- assert (0);
- }
- return last_element;
- }
- //========================================
- int sStack_size (sStack *stack) {
- //cout << "Stack size: " << stack->top << endl;
-
- if (!sStack_Ok (stack)) {
- sStack_show (stack);
- assert (0);
- }
-
- return stack->top;
- }
- //========================================
- void sStack_Push (sStack *stack, double vell) {
-
- if (!sStack_Ok (stack)) {
- sStack_show (stack);
- assert (0);
- }
-
- if (stack->st [stack->top] == -999 ) {
- stack->counter_change_number++;
- }
-
- stack->st [stack->top++] = vell;
-
- if (!sStack_Ok (stack)) {
- sStack_show (stack);
- assert (("Push", 0));
- }
- }
- //========================================
- double sStack_Pop (sStack *stack) {
-
- double last_element = stack->st [(stack->top) - 1];
- //cout << "last_element = " << last_element << endl;
- stack->st [(stack->top) - 1] = 0;
- stack->top--;
-
- if (!sStack_Ok (stack)) {
- sStack_show (stack);
- assert (("Pop", 0));
- }
-
- return last_element;
- }
- //========================================
- bool sStack_Ok (sStack *stack) {
-
- if ((stack->top < 0) || (stack->top > MAX_STACK_SIZE)) {
- return false;
- }
- if (stack->first_point != -100500 ||
- stack->second_point != 0 || stack->third_point != 100500) {
- cout << "!!! boundary constants are change !!! \n";
- return false;
- }
- int counter = 0;
- for (int i = 0; i < MAX_STACK_SIZE; i++) {
- if (stack->st [i] == -999) {
- counter++;
- }
- }
-
- if ((MAX_STACK_SIZE - stack->counter_change_number) != counter) {
- cout << "!!!WARNING!!! counter_change_number != counter /n";
- }
-
- return true;
- }
- //=======================================
- void cpu_constructor (sCPU *cpu1, sStack *stack) {
-
- cpu1->s1 = stack;
-
- }
- //========================================
- bool sCPU_Ok (sCPU *cpu1) {
-
- if (!sStack_Ok (cpu1->s1)) {
- return false;
- }
-
- return true;
- }
- //========================================
- double sCPU_sinus (sCPU *cpu) {
-
- if (sStack_size (cpu->s1) < 1 ) {
- processing_error (-333);
- return 0;
- }
-
- double rezalt = my_sinus (sStack_Pop (cpu->s1));
- sStack_Push (cpu->s1, rezalt);
-
- if (!sCPU_Ok (cpu)) {
- sStack_show (cpu->s1);
- assert (("sin", 0));
- }
-
- return rezalt;
- }
- //=======================================
- double sCPU_cosine (sCPU *cpu) {
-
- if (sStack_size (cpu->s1) < 1 ) {
- processing_error (-333);
- return 0;
- }
-
-
- double result = my_cosine (sStack_Pop (cpu->s1));
- sStack_Push (cpu->s1, result);
-
- if (!sCPU_Ok (cpu)) {
- sStack_show (cpu->s1);
- assert (("cos", 0));
- }
-
-
- return result;
- }
- //=======================================
- double sCPU_addition (sCPU *cpu) {
-
- if (sStack_size (cpu->s1) < 2 ) {
- processing_error (-333);
- return 0;
- }
-
- double element1 = sStack_Pop (cpu->s1);
- double element2 = sStack_Pop (cpu->s1);
- double result = (element1 + element2);
-
- sStack_Push (cpu->s1, result);
-
- if (!sCPU_Ok (cpu)) {
- sStack_show (cpu->s1);
- assert (("addition", 0));
- }
-
- return result;
- }
- //=======================================
- double sCPU_subtraction (sCPU *cpu) {
-
- if (sStack_size (cpu->s1) < 2 ) {
- processing_error (-333);
- return 0;
- }
-
- double element1 = sStack_Pop (cpu->s1);
- double element2 = sStack_Pop (cpu->s1);
- double result = (element2 - element1);
-
- sStack_Push (cpu->s1, result);
-
- if (!sCPU_Ok (cpu)) {
- sStack_show (cpu->s1);
- assert (("subtraction", 0));
- }
-
- return result;
- }
- //=======================================
- double sCPU_multiplication (sCPU *cpu) {
-
- if (sStack_size (cpu->s1) < 2 ) {
- processing_error (-333);
- return 0;
- }
-
- double element1 = sStack_Pop (cpu->s1);
- double element2 = sStack_Pop (cpu->s1);
- double result = (element2 * element1);
-
- sStack_Push (cpu->s1, result);
-
- if (!sCPU_Ok (cpu)) {
- sStack_show (cpu->s1);
- assert (("multiplication", 0));
- }
-
- return result;
- }
- //=======================================
- double my_sinus (double _number) {
-
- return sin (_number*PI_CONST/180);
- }
- //=======================================
- double my_cosine (double _number) {
- return cos (_number*PI_CONST/180);
- }
- //======================================
- bool processing_error (const double namb_0f_error) {
-
- if (namb_0f_error == -333) {
- cout << "Not enough numbers in the stack \n";
- return true;
- }
-
- if (namb_0f_error == -444) {
- cout << "No element in the stack \n";
- return true;
- }
-
- return false;
- }
|