Advertisement
Guest User

Expression Parser

a guest
Mar 21st, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. int eval(char **_s);
  8. int feed_signed_operand(char **_s);
  9.  
  10. int feed_num(char **_s)
  11. {
  12.         char *s = *_s;
  13.         int num = 0;
  14.  
  15.         while (isdigit(*s))
  16.         {
  17.                 num = num * 10 + *s - '0';
  18.                 s++;
  19.         }
  20.         *_s = s;
  21.         return num;
  22. }
  23.  
  24. int feed_single_operand(char **_s)
  25. {
  26.         char *s = *_s;
  27.         int  num = 0;
  28.  
  29.  
  30.         if (*s == '(')
  31.         {
  32.                 s++;
  33.                 num = eval(&s);
  34.                 if (*s == ')')
  35.                         s++;
  36.                 else {
  37.                         printf("missing ) at %s\n", s);
  38.                         exit(0);
  39.                 }
  40.         }
  41.         else if (isdigit(*s))
  42.                 num = feed_num(&s);
  43.         else {
  44.                 printf("error at %s\n", s);
  45.                 exit(0);
  46.         }
  47.  
  48.         *_s = s;
  49.         return num;
  50. }
  51.  
  52. int feed_powered_operand(char **_s)
  53. {
  54.         char *s = *_s;
  55.         int num = 0;
  56.  
  57.         num = feed_single_operand(&s);
  58.         if (*s == '^')
  59.         {
  60.                 int num2;
  61.                 s++;
  62.  
  63.                 num2 = feed_signed_operand(&s);
  64.                 num = (int) pow(num, num2);
  65.         }
  66.  
  67.         *_s = s;
  68.         return num;
  69. }
  70.  
  71. int feed_signed_operand(char **_s)
  72. {
  73.         char *s = *_s;
  74.         int multBy = 1, num = 0;
  75.  
  76.         while (*s == '-' || *s == '+')
  77.         {
  78.                 if (*s == '-')
  79.                         multBy *= -1;
  80.                 s++;
  81.         }
  82.  
  83.         num = feed_powered_operand(&s);
  84.  
  85.         num *= multBy;
  86.         *_s = s;
  87.         return num;
  88. }
  89.  
  90. int feed_mul_dip_expr(char **_s)
  91. {
  92.         char *s = *_s;
  93.         int num;
  94.         int res = 1;
  95.         char c = 0;
  96.  
  97.         while (1)
  98.         {
  99.                 num = feed_signed_operand(&s);
  100.                 if (c == '*' || c == 0)
  101.                         res *= num;
  102.                 else if (c == '/')
  103.                         res /= num;
  104.  
  105.                 if (*s == '*' || *s == '/')
  106.                         c = *s++;
  107.                 else
  108.                         break;
  109.         }
  110.  
  111.         *_s = s;
  112.         return res;
  113. }
  114.  
  115.  
  116. int eval(char **_s)
  117. {
  118.         char *s = *_s;
  119.         int num;
  120.         int res = 0;
  121.         char c = 0;
  122.  
  123.  
  124.         while (1)
  125.         {
  126.                 num = feed_mul_dip_expr(&s);
  127.                 if (c == '+' || res == 0)
  128.                         res += num;
  129.                 else if (c == '-')
  130.                         res -= num;
  131.  
  132.                 if (*s == '+' || *s == '-')
  133.                         c = *s++;
  134.                 else
  135.                         break;
  136.         }
  137.  
  138.         *_s = s;
  139.         return res;
  140. }
  141.  
  142.  
  143. int main()
  144. {
  145. #define E "-22/-11+2^3*2^(1+2^((-1+3)*2))"
  146. //#define E "-(2^(-++-1))"
  147.         char buff[10000],*s;
  148.         int res;
  149.  
  150.         strcpy(buff, E);
  151.         s = buff;
  152.         res = eval(&s);
  153.         printf("%s =  %d\n", buff, res);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement