Advertisement
Julia_S

2.1_option11

Sep 13th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3.  
  4. void print_stack(int stack[], int count) {
  5. if (count == 0) printf(" Стек пуст\n");
  6. else for (int i = 0;i < count;i++) printf("%i ", stack[i]);
  7. printf("\n");
  8. };
  9.  
  10. void push(int stack[], int *count, int elem) {
  11. if (*count == 50) {
  12. printf("Стек полон. Добавление элемента невозможно.");
  13. }
  14. else {
  15. if (*count == 0) {
  16. stack[0] = elem;
  17. (*count)++;
  18. }
  19. else {
  20. stack[*count] = elem;
  21. (*count)++;
  22. }
  23. }
  24. }
  25.  
  26. int pop(int stack[], int *count) {
  27. stack[(*count) - 1] += stack[(*count) - 2];
  28. int elem = stack[(*count) - 1];
  29. stack[(*count) - 1] = '\0';
  30. (*count)--;
  31. return elem;
  32. }
  33.  
  34.  
  35. int main()
  36. {
  37. setlocale(LC_ALL, "Russian");
  38. int stack[50];
  39. int count = 0;
  40. int key = 0, elem = 0;
  41. while (key != 3) {
  42. printf("\nСодержимое стека: ");
  43. print_stack(stack, count);
  44. printf("1.Добавить элемент в стек.\n");
  45. printf("2.Извлечь элемент из стека. \n");
  46. printf("3.Выход из программы.\n");
  47. printf("Выберите операцию : ");
  48. while (scanf_s("%i", &key) != 1)
  49. {
  50. rewind(stdin);
  51. printf("Выберите операцию повторно: ");
  52. }
  53. if (key == 1) {
  54. printf("Введите значение элемента : ");
  55. scanf_s("%i", &elem);
  56. push(stack, &count, elem);
  57. }
  58. if (key == 2) {
  59. if (count <2) printf("\n Извлечение элемента невозможно.\n");
  60. else printf("\n Извлеченный элемент=%i\n", pop(stack, &count));
  61. }
  62. }
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement