The_Law

Untitled

Dec 6th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. enum{ MAXN = (int)2e6 };
  6.  
  7. typedef struct list
  8. {
  9.     char type;
  10.     long long key;
  11.     struct list *next;
  12. } list;
  13.  
  14. char s[MAXN];
  15. list* brackets[MAXN];
  16.  
  17.  
  18. long long getnum(int it)
  19. {
  20.     while ((s[it] != '\0') && (s[it] == '0'))
  21.         ++it;
  22.  
  23.     if (s[it] < '0' || '9' < s[it])
  24.         return 0;
  25.  
  26.     int curr = it;
  27.     while ((s[curr] != '\0') && (('0' <= s[curr]) && (s[curr] <= '9')))
  28.         ++curr;
  29.  
  30.     --curr;
  31.     long long res = 0;
  32.     int p = 0;
  33.     while (it <= curr)
  34.     {
  35.         if (!p)
  36.             p = 1;
  37.         res += (s[curr] - '0') * p;
  38.         --curr;
  39.         p *= 10;
  40.     }
  41.  
  42.     return res;
  43. }
  44.  
  45. long long compute(list *tmp)
  46. {
  47.     int sum = 0;
  48.     int current = 0;
  49.     int sign = 1;
  50.  
  51.     tmp = tmp->next;
  52.  
  53.     if (tmp->type == '-')
  54.         sign = -1, tmp = tmp->next;
  55.  
  56.     while (tmp->type != ')')
  57.     {
  58.         current = tmp->key;
  59.         tmp = tmp->next;
  60.  
  61.         char operation = tmp->type;
  62.  
  63.         while ((operation == '*' || operation == '/') && (tmp->next->type != ')'))
  64.         {
  65.             if (operation == '*')
  66.                 current *= tmp->next->key;
  67.             else
  68.                 current /= tmp->next->key;
  69.  
  70.             tmp = tmp->next->next;
  71.             operation = tmp->type;
  72.         }
  73.  
  74.         current *= sign;
  75.         sum += current;
  76.         char oper = tmp->type;
  77.  
  78.         if (oper == '-' || oper == '+')
  79.         {
  80.             tmp = tmp->next;
  81.  
  82.             if (oper == '-')
  83.                 sign = -1;
  84.             else
  85.                 sign = 1;
  86.         }
  87.     }
  88.  
  89.     return sum;
  90. }
  91.  
  92. int main(void)
  93. {
  94.     FILE* in = fopen("input.txt", "r");
  95.     FILE* out = fopen("output.txt", "w");
  96.  
  97.     fgets(s, MAXN, in);
  98.  
  99.     int it = 0;
  100.     list *buff = malloc(sizeof(list));
  101.     buff->type = '(';
  102.     list *stak = buff;
  103.  
  104.  
  105.     while (s[it] != '\0')
  106.     {
  107.         if (('0' <= s[it]) && (s[it] <= '9'))
  108.         {
  109.             int curr = getnum(it);
  110.             buff->next = malloc(sizeof(list));
  111.             buff = buff->next;
  112.             buff->type = 'i';
  113.             buff->key = curr;
  114.  
  115.             while ((s[it] != '\0') && (('0' <= s[it]) && (s[it] <= '9')))
  116.                 ++it;
  117.             --it;
  118.         }
  119.         else if (s[it] != ' ')
  120.         {
  121.             buff->next = malloc(sizeof(list));
  122.             buff = buff->next;
  123.             buff->type = s[it];
  124.         }
  125.  
  126.         ++it;
  127.     }
  128.  
  129.     buff->next = malloc(sizeof(list));
  130.     buff = buff->next;
  131.     buff->type = ')';
  132.     buff->next = NULL;
  133.  
  134.     int currbr = 0;
  135.  
  136.     while (stak)
  137.     {
  138.         if (stak->type == '(')
  139.         {
  140.             brackets[currbr] = stak;
  141.             ++currbr;
  142.         }
  143.  
  144.         if (stak->type == ')')
  145.         {
  146.             --currbr;
  147.             long long res = compute(brackets[currbr]);
  148.             brackets[currbr]->type = 'i';
  149.             brackets[currbr]->key = res;
  150.             brackets[currbr]->next = stak->next;
  151.         }
  152.  
  153.         stak = stak->next;
  154.     }
  155.  
  156.     fprintf(out, "%d", brackets[currbr]->key);
  157.  
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment