Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- int is_operator(char c)
- {
- return c == '*' || c == '+' || c == '-' || c == '/' || c == '%';
- }
- int ft_is_digit(char c)
- {
- return ('0' <= c && c <= '9');
- }
- int valid_char(char c)
- {
- return ft_is_digit(c) || c == ' ' || is_operator(c);
- }
- int is_valid_rpn(char *str)
- {
- int optr_count = 0;
- int oprd_count = 0;
- int i = 0;
- while (str[i] == ' ')
- ++i;
- while (str[i])
- {
- if (!valid_char(str[i]))
- return (0);
- if (str[i] == ' ' && ft_is_digit(str[i - 1]))
- ++oprd_count;
- if ((str[i] == '+' || str[i] == '-') && ft_is_digit(str[i + 1]))
- {
- ++i;
- continue;
- }
- else if (is_operator(str[i]) && str[i - 1] != ' ')
- return (0);
- if (is_operator(str[i]) && (str[i + 1] == ' ' || str[i + 1] == '\0'))
- ++optr_count;
- if ((oprd_count < 2 && optr_count > 0) ||
- (oprd_count >= 2 && optr_count >= oprd_count))
- return (0);
- ++i;
- }
- if (optr_count != oprd_count - 1)
- return (0);
- return (1);
- }
- int add(int a, int b)
- {
- return a + b;
- }
- int sub(int a, int b)
- {
- return a - b;
- }
- int mul(int a, int b)
- {
- return a * b;
- }
- int dvs(int a, int b)
- {
- return a / b;
- }
- int mod(int a, int b)
- {
- return a % b;
- }
- int find_a_index(char *str, int end_index)
- {
- int nbrs_to_pass = 1;
- while (end_index >= 0)
- {
- if (nbrs_to_pass == 0)
- return end_index;
- if (is_operator(str[end_index]))
- {
- ++nbrs_to_pass;
- end_index -= 2;
- continue;
- }
- else if (ft_is_digit(str[end_index]))
- {
- --nbrs_to_pass;
- while(end_index >= 1 && str[end_index] != ' ')
- --end_index;
- if (str[end_index] == ' ')
- --end_index;
- }
- else
- return (-20);
- }
- return (-42);
- }
- int rec_rpn_read(char *str, int end_index)
- {
- int (*op)(int, int);
- int a;
- int b;
- switch (str[end_index])
- {
- case '+': op = &add; break;
- case '-': op = ⊂ break;
- case '*': op = &mul; break;
- case '/': op = &dvs; break;
- case '%': op = &mod; break;
- default: printf("Unexpected op error\n"); break;
- }
- end_index -= 2;
- if (is_operator(str[end_index]))
- {
- b = rec_rpn_read(str, end_index);
- end_index = find_a_index(str, end_index);
- if (end_index < 0)
- {
- printf("Unexpected error %d\n", end_index);
- return (0);
- }
- }
- else
- {
- while (end_index >= 1 && str[end_index] != ' ')
- --end_index;
- if (str[end_index] == ' ')
- ++end_index;
- b = atoi(str + end_index);
- if (end_index >= 2)
- end_index -= 2;
- }
- if (is_operator(str[end_index]))
- a = rec_rpn_read(str, end_index);
- else
- {
- while (end_index >= 1 && str[end_index] != ' ')
- --end_index;
- if (str[end_index] == ' ')
- ++end_index;
- a = atoi(str + end_index);
- }
- return (op(a, b));
- }
- int main(int argc, char **argv)
- {
- int result;
- int len = 0;
- if (argc != 2 || !is_valid_rpn(argv[1]))
- {
- printf("Error\n");
- return (1);
- }
- while (argv[1][len])
- ++len;
- result = rec_rpn_read(argv[1], len - 1);
- printf("%d\n", result);
- return (0);
- }
- /*
- echo "Test :"
- ./a.out
- echo "Expected: Error\n"
- echo "Test :"
- ./a.out "5 10 9 / 50 *"
- echo "Expected: Error\n"
- echo "Test :"
- ./a.out ""
- echo "Expected: Error\n"
- echo "Test :"
- ./a.out "5 10 9 / 50 *a"
- echo "Expected: Error\n"
- echo "Test :"
- ./a.out "2 3 * * 3"
- echo "Expected: Error\n"
- echo "Test :"
- ./a.out "2 3 * 4 5 * -"
- echo "Expected: -14\n"
- echo "Test :"
- ./a.out "3 4 +"
- echo "Expected: 7\n"
- echo "Test :"
- ./a.out "3 1 2 * * 4 -"
- echo "Expected: 2\n"
- echo "Test :"
- ./a.out "1 2 * 3 * 4 -"
- echo "Expected: 2\n"
- echo "Test :"
- ./a.out "1 2 * 3 * 4 +"
- echo "Expected: 10\n"
- echo "Test :"
- ./a.out "5 10 9 / - 50 *"
- echo "Expected: 200\n"
- echo "Test :"
- ./a.out "-200 6 +"
- echo "Expected: -194\n"
- echo "Test :"
- ./a.out "5 -10 +9 + * 50 *"
- echo "Expected: -250\n"
- */
Advertisement
Add Comment
Please, Sign In to add comment