Guest User

Untitled

a guest
Jul 15th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct{
  6.   int top;
  7.   int items[251];
  8. }STACK;
  9.  
  10. void push(int x, STACK *ps){
  11.   if(ps->top == 250){
  12.     printf("Error: stack overflow");
  13.     exit(EXIT_FAILURE);
  14.     }else
  15.       ps->items[++(ps->top)] = x;
  16.  return;
  17. }
  18.  
  19. int pop(STACK *ps){
  20. /*  if(empty(ps)){
  21.     printf("Error: stack underflow");
  22.     exit(EXIT_FAILURE);
  23.     }else */
  24. return(ps->items[(ps->top)--]);
  25. }
  26.  
  27. int main(void){
  28.  
  29. int pole [30];
  30. char string [250];
  31. STACK stack;
  32. stack.top = -1;
  33. int cislo;
  34. int operacia;
  35. int vysledok;
  36.  
  37. while(scanf("%s",string) != EOF){
  38.     if (strcmp("+",string)){
  39.       operacia = pop(&stack) + pop(&stack);
  40.       push(operacia,&stack);}else
  41.     if (strcmp("-",string)){
  42.       operacia = pop(&stack) + pop(&stack);
  43.       push(operacia,&stack);}else
  44.     if (strcmp("*",string)){
  45.       operacia = pop(&stack) + pop(&stack);
  46.       push(operacia,&stack);}else
  47.     if (strcmp("/",string)){
  48.       operacia = pop(&stack) + pop(&stack);
  49.       push(operacia,&stack);}else{
  50.         cislo = atoi(string);
  51.         push(cislo,&stack);
  52.         }
  53. }
  54. vysledok = pop(&stack);
  55.  
  56. printf("%d",vysledok);
  57.  
  58. return 0;
  59. }
Add Comment
Please, Sign In to add comment