fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4.  
  5. // Схема массива: [0] - N, число стеков в массиве
  6. // [1] - размер первого стека, [2] - указатель на верхушку первого стека
  7. // ... повторить [1] и [2] N раз
  8. // [1 + 2N] - начало первого стека, [1 + 2N + [1]] - конец первого стека
  9. // ... повторить N раз
  10.  
  11. void init_array_params(int *this_array){
  12. // Ввод числа стеков
  13. int N;
  14. printf("\nType number of stacks: ");
  15. if ((scanf("%d", &N) == 0) || (N < 3)){
  16. printf("\nError.*\n ");
  17. exit(-1);
  18. }
  19. // Создание временного массива, в котором будут храниться начальные размеры будущих стеков
  20. int *sizes;
  21. sizes = (int*)malloc(sizeof(int)*N);
  22. // Ввод начальных размеров стеков
  23. for (int i = 0; i < N; i++) {
  24. int tmp;
  25. printf("Type size of stack number %d: ", i);
  26. if ((scanf("%d", &tmp) == 0) || ( tmp < 2)) {
  27. printf("\nError. Try again.\n ");
  28. i--;
  29. continue;
  30. }
  31. sizes[i] = tmp;
  32. }
  33. // Вычисление длины будущего массива
  34. int size = 1 + N * 2; // +длина "заголовка" массива
  35. for (int i = 0; i < N; i++){
  36. size += sizes[i]; // +длина каждого из стеков
  37. }
  38. *this_array = malloc(sizeof(int)*size);
  39. // Заполнение заголовка
  40. this_array[0] = N;
  41. int last_pointer = 1 + N * 2;
  42. for (int i = 0; i < N; i++){
  43. this_array[1+i*2] = sizes[i];
  44. this_array[2+i*2] = last_pointer;
  45. last_pointer += sizes[i];
  46. }
  47. free(sizes);
  48. }
  49.  
  50. void print_array(int this_array[]){
  51. printf("\n----------------");
  52. printf("\nNumber of stacks: %d", this_array[0]);
  53. printf("\nLength of header: %d", 1+this_array[0]*2);
  54. printf("\n----------------");
  55. for (int i = 1; i/2 < this_array[0]; i += 2){
  56. printf("\nStack #%d\n", i/2);
  57. printf("\nSize: %d", this_array[i]);
  58. printf("\nPointer: %d", this_array[i+1]);
  59. printf("\n----------------");
  60. }
  61. }
  62.  
  63. int push(int stack_number, int value, int *this_array){
  64. if ((stack_number < 0) || (this_array[0] <= stack_number))
  65. return -1;
  66. // Изменение указателя на верхушку стека
  67. this_array[2+stack_number*2]++;
  68. int pointer = this_array[2+stack_number*2];
  69. // Установка значения по указателю
  70. this_array[pointer] = value;
  71. printf("\nValue %d was pushed to stack #%d\n", value, stack_number);
  72. return 1;
  73. }
  74.  
  75. int pop(int stack_number, int *this_array){
  76. if ((stack_number < 0) || (this_array[0] <= stack_number))
  77. return -1;
  78. // Извлечение значения
  79. int pointer = this_array[2+stack_number*2];
  80. int value = this_array[pointer];
  81. // Изменение указателя на верхушку стека
  82. this_array[2+stack_number*2]--;
  83. printf("\nPoped value: %d\n", value);
  84. return value;
  85. }
  86.  
  87. int main() {
  88. int *array = NULL;
  89. init_array_params(&array);
  90. int menu_pointer = -1;
  91. while (menu_pointer != 0){
  92. print_array(&array);
  93. printf("\n\n1. Push to stack...");
  94. printf("\n2. Pop from stack...");
  95. printf("\n\n0. Exit");
  96. printf("\n----------------\n");
  97. scanf("%d", &menu_pointer);
  98. if (menu_pointer == 1){
  99. int stack_number = -1, value;
  100. printf("\nType stack number(0,1,2...): ");
  101. scanf("%d", &stack_number);
  102. printf("\nType value to push: ");
  103. scanf("%d", &value);
  104. push(stack_number, value, &array);
  105. }
  106. if (menu_pointer == 2){
  107. int stack_number, value;
  108. printf("\nType stack number: ");
  109. scanf("%d", &stack_number);
  110. value = pop(stack_number, &array);
  111. printf("\n----------------\n");
  112. printf("\nPoped value: %d\n", value);
  113. char a;
  114. scanf("%c", &a);
  115. }
  116. }
  117. return 0;
  118. }
  119. return 0;
  120. }
Compilation error #stdin compilation error #stdout 0s 1403392KB
stdin
3
compilation info
prog.c: In function 'init_array_params':
prog.c:38:17: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
     *this_array = malloc(sizeof(int)*size);
                 ^
prog.c: In function 'main':
prog.c:89:23: warning: passing argument 1 of 'init_array_params' from incompatible pointer type [-Wincompatible-pointer-types]
     init_array_params(&array);
                       ^
prog.c:11:6: note: expected 'int *' but argument is of type 'int **'
 void init_array_params(int *this_array){
      ^
prog.c:92:21: warning: passing argument 1 of 'print_array' from incompatible pointer type [-Wincompatible-pointer-types]
         print_array(&array);
                     ^
prog.c:50:6: note: expected 'int *' but argument is of type 'int **'
 void print_array(int this_array[]){
      ^
prog.c:104:39: warning: passing argument 3 of 'push' from incompatible pointer type [-Wincompatible-pointer-types]
             push(stack_number, value, &array);
                                       ^
prog.c:63:5: note: expected 'int *' but argument is of type 'int **'
 int push(int stack_number, int value, int *this_array){
     ^
prog.c:110:39: warning: passing argument 2 of 'pop' from incompatible pointer type [-Wincompatible-pointer-types]
             value = pop(stack_number, &array);
                                       ^
prog.c:75:5: note: expected 'int *' but argument is of type 'int **'
 int pop(int stack_number, int *this_array){
     ^
prog.c: At top level:
prog.c:119:1: error: expected identifier or '(' before 'return'
 return 0;
 ^
prog.c:120:1: error: expected identifier or '(' before '}' token
 }
 ^
stdout
Standard output is empty