Ladies_Man

3_2 Stackmobile

Dec 26th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define max(x, y) ((x)>(y) ? (x):(y))
  6. #define min(x, y) ((x)<(y) ? (x):(y))
  7.  
  8. struct stack {
  9.     int *data;
  10.     int cap;
  11.     int top;
  12. };
  13.  
  14. int n;
  15.  
  16. char m_const[6] = "CONST";//push
  17. char m_add[4] = "ADD";//+
  18. char m_sub[4] = "SUB";//-
  19. char m_mul[4] = "MUL";//*
  20. char m_div[4] = "DIV";///
  21. char m_max[4] = "MAX";
  22. char m_min[4] = "MIN";
  23. char m_neg[4] = "NEG";//sign-swap
  24. char m_dup[4] = "DUP";//copy
  25. char swag[5] = "SWAP";
  26.  
  27. void push (struct stack *s, int x)
  28. {
  29.     if (s->top == s->cap) printf("stack_overflow\n");
  30.     else {
  31.         s->data[s->top] = x;
  32.         s->top++;
  33.     }
  34. }
  35.  
  36. int pop (struct stack *s)
  37. {
  38.     if (0 == s->top) {
  39.         printf("devastation\n");
  40.         return -1;
  41.     }
  42.     else {
  43.         s->top--;
  44.         int ret_val = s->data[s->top];
  45.         return ret_val;
  46.     }
  47. }
  48.  
  49. void s_add (struct stack *s)
  50. {
  51.     int a = pop(s);
  52.     int b = pop(s);
  53.     push (s, a + b);
  54.     //printf("add=%d\n", ret_val);
  55. }
  56.  
  57. void s_sub (struct stack *s)
  58. {
  59.     int a = pop(s);
  60.     int b = pop(s);
  61.     push (s, a - b);
  62.     //printf("sub=%d\n", ret_val);
  63. }
  64.  
  65. void s_mul (struct stack *s)
  66. {
  67.     int a = pop(s);
  68.     int b = pop(s);
  69.     push (s, a * b);
  70.     //printf("mul=%d\n", ret_val);
  71. }
  72.  
  73. void s_div (struct stack *s)
  74. {
  75.     int a = pop(s);
  76.     int b = pop(s);
  77.     push (s, a / b);
  78.     //printf("div=%d\n", ret_val);
  79. }
  80.  
  81. void s_max (struct stack *s)
  82. {
  83.     int a = pop(s);
  84.     int b = pop(s);
  85.     push (s, max(a, b));
  86.     //printf("max=%d\n", ret_val);
  87. }
  88.  
  89. void s_min (struct stack *s)
  90. {
  91.     int a = pop(s);
  92.     int b = pop(s);
  93.     push (s, min(a, b));
  94.     //printf("min=%d\n", ret_val);
  95. }
  96.  
  97. void s_neg (struct stack *s)
  98. {
  99.     int a = pop(s);
  100.     push (s, -a);
  101.     //printf("neg=%d\n", ret_val);
  102. }
  103.  
  104. void s_dup (struct stack *s)
  105. {
  106.     int a = pop(s);
  107.     push (s, a);
  108.     push (s, a);
  109.     //printf("dup=%d\n", ret_val);
  110. }
  111.  
  112. void s_swag (struct stack *s)
  113. {
  114.     int a = pop(s);
  115.     int b = pop(s);
  116.     push (s, a);
  117.     push (s, b);
  118.     //printf("swap\n");
  119. }
  120. void initstack (struct stack *s)
  121. {
  122.     s->data = (int*)malloc(n * sizeof(int));
  123.     s->cap = n;
  124.     s->top = 0;
  125. }
  126.  
  127. int main()
  128. {
  129.     struct stack turn;
  130.     int i, val;
  131.     char s[6];
  132.  
  133.     scanf("%d", &n);
  134.  
  135.     initstack(&turn);
  136.  
  137.     for (i = 0; i < n; i++) {
  138.         scanf("%s", s);
  139.         if (0 == strcmp(s, m_const)) {
  140.             scanf("%d", &val);
  141.             push (&turn, val);
  142.         }
  143.         if (0 == strcmp(s, m_add)) {
  144.             s_add (&turn);
  145.         }
  146.         if (0 == strcmp(s, m_sub)) {
  147.             s_sub (&turn);
  148.         }
  149.         if (0 == strcmp(s, m_mul)) {
  150.             s_mul (&turn);
  151.         }
  152.         if (0 == strcmp(s, m_div)) {
  153.             s_div (&turn);
  154.         }
  155.         if (0 == strcmp(s, m_max)) {
  156.             s_max (&turn);
  157.         }
  158.         if (0 == strcmp(s, m_min)) {
  159.             s_min (&turn);
  160.         }
  161.         if (0 == strcmp(s, m_neg)) {
  162.             s_neg (&turn);
  163.         }
  164.         if (0 == strcmp(s, m_dup)) {
  165.             s_dup (&turn);
  166.         }
  167.         if (0 == strcmp(s, swag)) {
  168.             s_swag (&turn);
  169.         }
  170.     }
  171.  
  172.     printf("%d", pop(&turn));
  173.  
  174.     free(turn.data);
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment