Advertisement
MadCortez

Untitled

Oct 21st, 2021
1,346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <locale.h>
  5. #include <string.h>
  6. #include <string>
  7.  
  8. struct stack {
  9.     int data;
  10.     stack* next;
  11. };
  12.  
  13. stack *head, *now;
  14.  
  15. void callMenu() {
  16.     printf("push(x)\n");
  17.     printf("pop\n");
  18.     printf("do\n");
  19. }
  20.  
  21. void push(int a) {
  22.     now = new(stack);
  23.     now->next = head;
  24.     now->data = a;
  25.     head = now;
  26. }
  27.  
  28. void pop() {
  29.     if (head == NULL) {
  30.         printf("Stack is empty\n");
  31.         return;
  32.     }
  33.     printf("%d\n", head->data);
  34.     now = head->next;
  35.     delete(head);
  36.     head = now;
  37. }
  38.  
  39. void toDo() {
  40.     int a = 0, kol = 0;
  41.     float diff = 0;
  42.     now = head;
  43.     while (now != NULL) {
  44.         kol++;
  45.         a += now->data;
  46.         now = now->next;
  47.     }
  48.     diff = float(a) / kol;
  49.     kol = 0;
  50.     now = head;
  51.     while (now != NULL) {
  52.         if (now->data > diff)
  53.             kol++;
  54.         now = now->next;
  55.     }
  56.     printf("Result = %d\n", kol);
  57. }
  58.  
  59. void view() {
  60.     now = head;
  61.     if (now == NULL) {
  62.         printf("Stack is empty\n");
  63.         return;
  64.     }
  65.     while (now != NULL) {
  66.         printf("%d ", now->data);
  67.         now = now->next;
  68.     }
  69.     printf("\n");
  70. }
  71.  
  72. int main()
  73. {
  74.     char input[255];
  75.     std::string temp;
  76.     int toAdd = 0;
  77.     char zero = ' ';
  78.     setlocale(LC_ALL, "Russian");
  79.     callMenu();
  80.     while (1) {
  81.         scanf("%s", input);
  82.         if (input[0] == 'p' && input[1] == 'u' && input[2] == 's' && input[3] == 'h') {
  83.             temp = "";
  84.             for (int i = 5; i < strlen(input), input[i] != ')'; i++)
  85.                 temp += input[i];
  86.             toAdd = stoi(temp);
  87.             push(toAdd);
  88.         }
  89.         if (input[0] == 'p' && input[1] == 'o' && input[2] == 'p') {
  90.             pop();
  91.         }
  92.         if (input[0] == 'd' && input[1] == 'o') {
  93.             toDo();
  94.         }
  95.         if (input[0] == 'v' && input[1] == 'i' && input[2] == 'e' && input[3] == 'w')
  96.             view();
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement