G2A Many GEOs
SHARE
TWEET

rewyrdghdrsgfgfsd

a guest Apr 5th, 2020 158 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. typedef struct stack
  5. {
  6.     int* elements;
  7.     int top;
  8.     int maxsize;
  9. }stack;
  10.  
  11. void push(stack* s, int n)
  12. {
  13.     s -> top++;
  14.     s -> elements[s->top] = n;
  15. }
  16.  
  17. int pop(stack* s)
  18. {
  19.     s->top--;
  20.     return s->elements[s->top+1];
  21. }
  22.  
  23. bool empty(stack*s)
  24. {
  25.     if(s->top==-1) return true;
  26.     else return false;
  27. }
  28.  
  29. stack* new_stack(int size)
  30. {
  31.     stack* stos=(int*)malloc(sizeof(int*)+2*sizeof(int));
  32.     stos->maxsize=size;
  33.     stos->top=-1;
  34.     stos->elements=malloc(size*sizeof(int));
  35.     return stos;
  36. }
  37.  
  38. void delstack(stack* s)
  39. {
  40.     free(s->elements);
  41.     free(s);
  42. }
  43.  
  44. void plus(stack* s)
  45. {
  46.     int a = pop(s);
  47.     int b = pop(s);
  48.     push(s, b+a);
  49. }
  50.  
  51. void minus(stack *s)
  52. {
  53.     int a = pop(s);
  54.     int b = pop(s);
  55.     push(s, b-a);
  56. }
  57.  
  58. void mult(stack *s)
  59. {
  60.     int a = pop(s);
  61.     int b = pop(s);
  62.     push(s, b*a);
  63. }
  64.  
  65. void divide(stack *s)
  66. {
  67.     int a = pop(s);
  68.     int b = pop(s);
  69.     push(s, b/a);
  70. }
  71.  
  72. int readnum(char *input, int numsize)
  73. {
  74.     int t=0;
  75.     int result=0;
  76.     int multiplier=1;
  77.  
  78.     for(t=0; t<numsize && input[t]!='\0'; t++)
  79.     {
  80.         multiplier*=10;
  81.     }
  82.     multiplier/=10;
  83.  
  84.     for(t=0; t<numsize && input[t]!='\0'; t++)
  85.     {
  86.         result+=(input[t]-48)*multiplier;
  87.         multiplier/=10;
  88.     }
  89.     return result;
  90. }
  91.  
  92. typedef enum input_types {num, plu, min, multi, divid} input_types;
  93.  
  94. input_types check(char* ch)
  95. {
  96.     if (ch[0] == '-') return min;
  97.     else if (ch[0] == '+') return plu;
  98.     else if (ch[0] == '/') return divid;
  99.     else if (ch[0] == '*') return multi;
  100.     else return num;
  101. }
  102.  
  103. int dowork(int n, int k)
  104. {
  105.     stack* ops=new_stack(n);
  106.     char* input=malloc((k+1)*sizeof(char));
  107.  
  108.     while(n>0 || ops->top>0)
  109.     {
  110.         for(int i=0; i<n; i++) input[i]="\0";
  111.         scanf("%s", input);
  112.  
  113.         if(check(input)==plu) plus(ops);
  114.         else if(check(input)==min) minus(ops);
  115.         else if(check(input)==multi) mult(ops);
  116.         else if(check(input)==divid) divide(ops);
  117.         else
  118.         {
  119.             push(ops, readnum(input,k));
  120.             n--;
  121.         }
  122.     }
  123.     int result = pop(ops);
  124.     delstack(ops);
  125.     free(input);
  126.     return result;
  127. }
  128.  
  129. int main()
  130. {
  131.     int n,k;
  132.     scanf("%d",&n);
  133.     scanf("%d",&k);
  134.     int res = dowork(n,k);
  135.     printf("%d\n", res);
  136.     return 0;
  137. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top