Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SUCCESS 0
  6. #define MAX_ARRAY 100
  7. #define MAX_DYNAMIC 1000
  8. #define TRUE 1
  9. #define FULL_STACK -1
  10. #define INPUT_ERR -2
  11. #define EMPTY_STACK -3
  12.  
  13. typedef struct
  14. {
  15.     int *high_border;
  16.     int *low_border;
  17.     int *current_ptr;
  18.     int type;
  19. } static_stack_descriptor_t;
  20.  
  21. typedef struct dynamic_stack_descriptor
  22. {
  23.     int value;
  24.     struct dynamic_stack_descriptor* ex_ptr;
  25. } dynamic_stack_descriptor_t;
  26.  
  27.  
  28. int static_push(static_stack_descriptor_t *static_stack)
  29. {
  30.     if (static_stack -> current_ptr == static_stack -> high_border)
  31.         return FULL_STACK;
  32.  
  33.     int elem;
  34.     printf("Please, input element: ");
  35.     if(scanf("%i", &elem) != 1)
  36.         return INPUT_ERR;
  37.  
  38.     static_stack -> current_ptr++;
  39.     *(static_stack -> current_ptr) = elem;
  40.  
  41.     return SUCCESS;
  42. }
  43.  
  44.  
  45. int static_pop(static_stack_descriptor_t *static_stack)
  46. {
  47.     if (static_stack -> current_ptr == static_stack -> low_border)
  48.         return EMPTY_STACK;
  49.  
  50.     printf("Removed element: %i", *(static_stack -> current_ptr));
  51.     (static_stack -> current_ptr)--;
  52.  
  53.     return SUCCESS;
  54. }
  55.  
  56. int dynamic_push(dynamic_stack_descriptor_t **dynamic_stack, dynamic_stack_descriptor_t* free_ptr[MAX_DYNAMIC], int* free_ptr_len)
  57. {
  58.     int elem;
  59.     printf("Please, input element: ");
  60.     if(scanf("%i", &elem) != 1)
  61.         return INPUT_ERR;
  62.  
  63.     dynamic_stack_descriptor_t *ptr = *dynamic_stack;
  64.     (*dynamic_stack) = malloc(sizeof(dynamic_stack_descriptor_t));
  65.     if (*dynamic_stack == NULL)
  66.         return INPUT_ERR;
  67.     for (int i = 0; i < *free_ptr_len; i++)
  68.         if (*dynamic_stack == free_ptr[i])
  69.         {
  70.             for (int j = i; j < *free_ptr_len - 1; j++)
  71.                 free_ptr[j] = free_ptr[j + 1];
  72.             (*free_ptr_len)--;
  73.             break;
  74.         }
  75.     (*dynamic_stack) -> value = elem;
  76.     (*dynamic_stack) -> ex_ptr = ptr;
  77.  
  78.     return SUCCESS;
  79. }
  80.  
  81. void dynamic_remove_elem(dynamic_stack_descriptor_t **dynamic_stack, dynamic_stack_descriptor_t* free_ptr[MAX_DYNAMIC], int* free_ptr_len)
  82. {
  83.     dynamic_stack_descriptor_t *ptr = *dynamic_stack;
  84.     *dynamic_stack = (*dynamic_stack) -> ex_ptr;
  85.     free_ptr[*free_ptr_len] = ptr;
  86.     (*free_ptr_len)++;
  87.     free(ptr);
  88.     return;
  89. }
  90.  
  91. int dynamic_pop(dynamic_stack_descriptor_t **dynamic_stack, dynamic_stack_descriptor_t* free_ptr[MAX_DYNAMIC], int* free_ptr_len)
  92. {
  93.  
  94.     printf("Removed element: %i", (*dynamic_stack) -> value);
  95.  
  96.     dynamic_remove_elem(dynamic_stack, free_ptr, free_ptr_len);
  97.     return SUCCESS;
  98. }
  99.  
  100.  
  101. int main()
  102. {
  103.     //freopen ("in.txt", "r", stdin);
  104.     int choice;
  105.     int array_static_stack[MAX_ARRAY];
  106.     static_stack_descriptor_t static_stack;
  107.     static_stack.low_border = (array_static_stack - 1);
  108.     static_stack.high_border = (&array_static_stack[MAX_ARRAY - 1]);
  109.     static_stack.current_ptr = static_stack.low_border;
  110.     int dynamic_amount = 0;
  111.     dynamic_stack_descriptor_t* dynamic_stack = NULL;
  112.     dynamic_stack_descriptor_t *free_ptr[MAX_DYNAMIC];
  113.     int free_ptr_len;
  114.  
  115.     while (TRUE)
  116.     {
  117.         printf("\n");
  118.         printf(" _ __ ___   ___ _ __  _   _ \n");
  119.         printf("| '_ ` _ \\ / _ \\ '_ \\| | | |\n");
  120.         printf("| | | | | |  __/ | | | |_| |\n");
  121.         printf("|_| |_| |_|\\___|_| |_|\\____|\n");
  122.  
  123.         printf("\n\nATTENTION: Array stack has a limit in 100 elements!");
  124.         printf("\nATTENTION: Dynamic stack has a ARTIFICIAL limit in 1000 elements!\n\n");
  125.         printf("1: ARRAY STACK: Appending new element\n");
  126.         printf("2: ARRAY STACK: Removing element\n");
  127.         printf("3: LIST STACK: Appending new element\n");
  128.         printf("4: LIST STACK: Removing element\n");
  129.         printf("5: ARRAY STACK: Print the descending series sequences of integers in the reverse order\n");
  130.         printf("6: LIST STACK: Print the descending series sequences of integers in the reverse order\n");
  131.         printf("7: ARRAY STACK: Check memory status\n");
  132.         printf("8: LIST STACK: Check memory status\n");
  133.         printf("\n0: EXIT");
  134.         while (TRUE)
  135.         {
  136.             fflush(stdin);
  137.             printf("\n\nYour choice: ");
  138.             if ((scanf("%i", &choice) == 1) && (choice >= 0))
  139.                 break;
  140.             printf("Wrong choice!\n");
  141.             system("pause");
  142.         }
  143.  
  144.         if (!choice)
  145.         {
  146.             while(dynamic_amount != 0)
  147.             {
  148.                 dynamic_remove_elem(&dynamic_stack, free_ptr, &free_ptr_len);
  149.                 dynamic_amount--;
  150.             }
  151.  
  152.  
  153.             break;
  154.         }
  155.  
  156.         if (choice == 1)
  157.         {
  158.             switch(static_push(&static_stack))
  159.             {
  160.                 case FULL_STACK:
  161.                     printf("Stack if full!\n");
  162.                     system("pause");
  163.                     continue;
  164.                 case INPUT_ERR:
  165.                     printf("Input error!\n");
  166.                     system("pause");
  167.                     continue;
  168.                  case SUCCESS:
  169.                     printf("Successfull input!\n");
  170.                     system("pause");
  171.                     continue;
  172.             }
  173.  
  174.         }
  175.         if (choice == 2)
  176.         {
  177.             switch(static_pop(&static_stack))
  178.             {
  179.                 case EMPTY_STACK:
  180.                     printf("Stack is empty!\n");
  181.                     system("pause");
  182.                     continue;
  183.                 case SUCCESS:
  184.                     printf("\n");
  185.                      system("pause");
  186.                      continue;
  187.             }
  188.         }
  189.  
  190.         if (choice == 3)
  191.         {
  192.             if (dynamic_amount == MAX_DYNAMIC)
  193.             {
  194.                 printf("Stack if full!\n");
  195.                 system("pause");
  196.                 continue;
  197.             }
  198.  
  199.             switch(dynamic_push(&dynamic_stack, free_ptr, &free_ptr_len))
  200.             {
  201.                 case INPUT_ERR:
  202.                     printf("Input error!\n");
  203.                     system("pause");
  204.                     continue;
  205.                  case SUCCESS:
  206.                     dynamic_amount++;
  207.                     printf("Successfull input!\n");
  208.                     system("pause");
  209.                     continue;
  210.             }
  211.         }
  212.  
  213.         if (choice == 4)
  214.         {
  215.             if (dynamic_amount == 0)
  216.             {
  217.                 printf("Stack is empty!\n");
  218.                 system("pause");
  219.                 continue;
  220.             }
  221.  
  222.  
  223.             switch(dynamic_pop(&dynamic_stack, free_ptr, &free_ptr_len))
  224.             {
  225.                 case SUCCESS:
  226.                     dynamic_amount--;
  227.                     printf("\n");
  228.                     system("pause");
  229.                     continue;
  230.             }
  231.         }
  232.  
  233.  
  234.         if (choice == 5)
  235.         {
  236.             if (static_stack.current_ptr == static_stack.low_border)
  237.             {
  238.                 printf("Empty stack!\n");
  239.                 system("pause");
  240.                 continue;
  241.             }
  242.  
  243.             int is_printed = 0;
  244.             int* end = static_stack.current_ptr;
  245.             long int mem = (static_stack.current_ptr - static_stack.low_border) * sizeof(static_stack_descriptor_t);
  246.             int in_seq = 0;
  247.             int is_n = 1;
  248.             clock_t time = clock();
  249.             for (; static_stack.current_ptr != static_stack.low_border; static_stack.current_ptr--)
  250.             {
  251.                 in_seq = 0;
  252.                 if (static_stack.current_ptr - 1 != static_stack.low_border &&
  253.                     *static_stack.current_ptr > *(static_stack.current_ptr - 1))
  254.                     in_seq = 1;
  255.  
  256.                 else if (static_stack.current_ptr != end &&
  257.                          *static_stack.current_ptr < *(static_stack.current_ptr + 1))
  258.                         in_seq = 1;
  259.  
  260.                 else if (static_stack.current_ptr - 1 != static_stack.low_border &&
  261.                          static_stack.current_ptr != end &&
  262.                          *static_stack.current_ptr > *(static_stack.current_ptr - 1))
  263.                         in_seq = 1;
  264.  
  265.                 if (static_stack.current_ptr - 1 != static_stack.low_border &&
  266.                     static_stack.current_ptr != end &&
  267.                     *static_stack.current_ptr >= *(static_stack.current_ptr + 1) &&
  268.                     *static_stack.current_ptr > *(static_stack.current_ptr - 1))
  269.                     printf("\n");
  270.  
  271.                 if (in_seq)
  272.                 {
  273.                     is_printed = 1;
  274.                     printf("%i ", *static_stack.current_ptr);
  275.                     is_n = 0;
  276.                 }
  277.  
  278.                 else if (!is_n)
  279.                 {
  280.                     is_n = 1;
  281.                     printf("\n");
  282.                 }
  283.  
  284.             }
  285.  
  286.             if (!is_printed)
  287.                 printf("\nThere are no descending series in stack!");
  288.             printf("\n");
  289.  
  290.             time = clock() - time;
  291.             printf("Time spended in ticks: %li\n", (long int) time);
  292.             printf("Memoty usage in bytes: %li\n", mem);
  293.             system("pause");
  294.  
  295.         }
  296.  
  297.         if (choice == 6)
  298.         {
  299.             if (dynamic_amount == 0)
  300.             {
  301.                 printf("Empty stack!\n");
  302.                 system("pause");
  303.                 continue;
  304.             }
  305.  
  306.             int is_printed = 0;
  307.             //dynamic_stack_descriptor_t *end = dynamic_stack;
  308.             int in_seq = 0;
  309.             int is_n = 0;
  310.  
  311.             long int mem = dynamic_amount * sizeof(dynamic_stack_descriptor_t);
  312.             clock_t time = clock();
  313.             int past = dynamic_stack -> value;
  314.             int current = dynamic_stack -> value;
  315.             dynamic_remove_elem(&dynamic_stack, free_ptr, &free_ptr_len);
  316.             int next = dynamic_stack -> value;
  317.             for (; dynamic_stack != NULL;)
  318.             {
  319.                 next = dynamic_stack -> value;
  320.                 in_seq = 0;
  321.                 if (current > next)
  322.                     in_seq = 1;
  323.  
  324.                 else if (current < past)
  325.                     in_seq = 1;
  326.  
  327.                 if(current >= past && current > next)
  328.                     printf("\n");
  329.  
  330.                 if (in_seq)
  331.                 {
  332.                     is_printed = 1;
  333.                     printf("%i ", current);
  334.                     is_n = 0;
  335.                 }
  336.  
  337.                 else if (!is_n)
  338.                 {
  339.                     is_n = 1;
  340.                     printf("\n");
  341.                 }
  342.  
  343.                 past = current;
  344.                 current = next;
  345.                 dynamic_remove_elem(&dynamic_stack, free_ptr, &free_ptr_len);
  346.  
  347.             }
  348.  
  349.  
  350.  
  351.             if(current < past)
  352.                 printf("%i", current);
  353.  
  354.             if (!is_printed)
  355.                 printf("\nThere are no descending series in stack!");
  356.             printf("\n");
  357.             time = clock() - time;
  358.             printf("Time spended in ticks: %li\n", (long int) time);
  359.             printf("Memoty usage in bytes: %li\n", mem);
  360.             dynamic_amount = 0;
  361.             system("pause");
  362.         }
  363.  
  364.         if (choice == 7)
  365.         {
  366.             printf("Current fullness of array stack: %li / %i", (long int)(static_stack.current_ptr - static_stack.low_border), MAX_ARRAY);
  367.         }
  368.  
  369.         if (choice == 8)
  370.         {
  371.             if (free_ptr_len == 0)
  372.                 printf("There are no free adresses!\n");
  373.  
  374.             else
  375.             {
  376.                 printf("List of free pinters:\n");
  377.                 for(int i = 0; i < free_ptr_len; i++)
  378.                 {
  379.                     printf("%p\n", free_ptr[i]);
  380.                 }
  381.             }
  382.  
  383.             printf("\n");
  384.             system("pause");
  385.         }
  386.  
  387.     }
  388.  
  389.  
  390.     //fclose (stdin);
  391.  
  392.     return SUCCESS;
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement