Guest User

Untitled

a guest
Jan 9th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.80 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int     is_operator(char c)
  5. {
  6.     return c == '*' || c == '+' || c == '-' || c == '/' || c == '%';
  7. }
  8.  
  9. int     ft_is_digit(char c)
  10. {
  11.     return ('0' <= c && c <= '9');
  12. }
  13.  
  14. int     valid_char(char c)
  15. {
  16.     return ft_is_digit(c) || c == ' ' || is_operator(c);
  17. }
  18.  
  19. int     is_valid_rpn(char *str)
  20. {
  21.     int     optr_count = 0;
  22.     int     oprd_count = 0;
  23.     int     i = 0;
  24.  
  25.     while (str[i] == ' ')
  26.         ++i;
  27.     while (str[i])
  28.     {
  29.         if (!valid_char(str[i]))
  30.             return (0);
  31.         if (str[i] == ' ' && ft_is_digit(str[i - 1]))
  32.             ++oprd_count;
  33.         if ((str[i] == '+' || str[i] == '-') && ft_is_digit(str[i + 1]))
  34.         {
  35.             ++i;
  36.             continue;
  37.         }
  38.         else if (is_operator(str[i]) && str[i - 1] != ' ')
  39.             return (0);
  40.         if (is_operator(str[i]) && (str[i + 1] == ' ' || str[i + 1] == '\0'))
  41.             ++optr_count;
  42.         if ((oprd_count < 2 && optr_count > 0) ||
  43.             (oprd_count >= 2 && optr_count >= oprd_count))
  44.             return (0);
  45.         ++i;
  46.     }
  47.     if (optr_count != oprd_count - 1)
  48.         return (0);
  49.     return (1);
  50. }
  51.  
  52. int     add(int a, int b)
  53. {
  54.     return a + b;
  55. }
  56.  
  57. int     sub(int a, int b)
  58. {
  59.     return a - b;
  60. }
  61.  
  62. int     mul(int a, int b)
  63. {
  64.     return a * b;
  65. }
  66.  
  67. int     dvs(int a, int b)
  68. {
  69.     return a / b;
  70. }
  71.  
  72. int     mod(int a, int b)
  73. {
  74.     return a % b;
  75. }
  76.  
  77. int     find_a_index(char *str, int end_index)
  78. {
  79.     int     nbrs_to_pass = 1;
  80.  
  81.     while (end_index >= 0)
  82.     {
  83.         if (nbrs_to_pass == 0)
  84.             return end_index;
  85.         if (is_operator(str[end_index]))
  86.         {
  87.             ++nbrs_to_pass;
  88.             end_index -= 2;
  89.             continue;
  90.         }
  91.         else if (ft_is_digit(str[end_index]))
  92.         {
  93.             --nbrs_to_pass;
  94.             while(end_index >= 1 && str[end_index] != ' ')
  95.                 --end_index;
  96.             if (str[end_index] == ' ')
  97.                 --end_index;
  98.         }
  99.         else
  100.             return (-20);
  101.     }
  102.     return (-42);
  103. }
  104.  
  105. int     rec_rpn_read(char *str, int end_index)
  106. {
  107.     int     (*op)(int, int);
  108.     int     a;
  109.     int     b;
  110.  
  111.     switch (str[end_index])
  112.     {
  113.         case '+': op = &add; break;
  114.         case '-': op = &sub; break;
  115.         case '*': op = &mul; break;
  116.         case '/': op = &dvs; break;
  117.         case '%': op = &mod; break;
  118.         default: printf("Unexpected op error\n"); break;
  119.     }
  120.     end_index -= 2;
  121.     if (is_operator(str[end_index]))
  122.     {
  123.         b = rec_rpn_read(str, end_index);
  124.         end_index = find_a_index(str, end_index);
  125.         if (end_index < 0)
  126.         {
  127.             printf("Unexpected error %d\n", end_index);
  128.             return (0);
  129.         }
  130.     }
  131.     else
  132.     {
  133.         while (end_index >= 1 && str[end_index] != ' ')
  134.             --end_index;
  135.         if (str[end_index] == ' ')
  136.             ++end_index;
  137.         b = atoi(str + end_index);
  138.         if (end_index >= 2)
  139.             end_index -= 2;
  140.     }
  141.     if (is_operator(str[end_index]))
  142.         a = rec_rpn_read(str, end_index);
  143.     else
  144.     {
  145.         while (end_index >= 1 && str[end_index] != ' ')
  146.             --end_index;
  147.         if (str[end_index] == ' ')
  148.             ++end_index;
  149.         a = atoi(str + end_index);
  150.     }
  151.     return (op(a, b));
  152. }
  153.  
  154. int     main(int argc, char **argv)
  155. {
  156.     int     result;
  157.     int     len = 0;
  158.  
  159.     if (argc != 2 || !is_valid_rpn(argv[1]))
  160.     {
  161.         printf("Error\n");
  162.         return (1);
  163.     }
  164.     while (argv[1][len])
  165.         ++len;
  166.     result = rec_rpn_read(argv[1], len - 1);
  167.     printf("%d\n", result);
  168.     return (0);
  169. }
  170. /*
  171. echo "Test :"
  172. ./a.out
  173. echo "Expected: Error\n"
  174.  
  175. echo "Test :"
  176. ./a.out "5 10 9 / 50 *"
  177. echo "Expected: Error\n"
  178.  
  179. echo "Test :"
  180. ./a.out ""
  181. echo "Expected: Error\n"
  182.  
  183. echo "Test :"
  184. ./a.out "5 10 9 / 50 *a"
  185. echo "Expected: Error\n"
  186.  
  187. echo "Test :"
  188. ./a.out "2 3 * * 3"
  189. echo "Expected: Error\n"
  190.  
  191. echo "Test :"
  192. ./a.out "2 3 * 4 5 * -"
  193. echo "Expected: -14\n"
  194.  
  195. echo "Test :"
  196. ./a.out "3 4 +"
  197. echo "Expected: 7\n"
  198.  
  199. echo "Test :"
  200. ./a.out "3 1 2 * * 4 -"
  201. echo "Expected: 2\n"
  202.  
  203. echo "Test :"
  204. ./a.out "1 2 * 3 * 4 -"
  205. echo "Expected: 2\n"
  206.  
  207. echo "Test :"
  208. ./a.out "1 2 * 3 * 4 +"
  209. echo "Expected: 10\n"
  210.  
  211. echo "Test :"
  212. ./a.out "5 10 9 / - 50 *"
  213. echo "Expected: 200\n"
  214.  
  215. echo "Test :"
  216. ./a.out "-200 6 +"
  217. echo "Expected: -194\n"
  218.  
  219. echo "Test :"
  220. ./a.out "5 -10 +9 + * 50 *"
  221. echo "Expected: -250\n"
  222. */
Advertisement
Add Comment
Please, Sign In to add comment