Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include "../includes/header.h"
  2.  
  3. int check_zero(char op, int zero)
  4. {
  5. if (op == '/' && zero == 0)
  6. {
  7. put_div_error();
  8. return (1);
  9. }
  10. if (op == '%' && zero == 0)
  11. {
  12. put_mod_error();
  13. return (1);
  14. }
  15. return (0);
  16. }
  17.  
  18. int invalid_argument(char *op)
  19. {
  20. if (op[1] != '\0' || (op[0] != '+' && \
  21. op[0] != '-' && op[0] != '*' && \
  22. op[0] != '/' && op[0] != '%'))
  23. {
  24. ft_putstr("0\n");
  25. return (1);
  26. }
  27. return (0);
  28. }
  29.  
  30. int main(int argc, char **argv)
  31. {
  32. int i;
  33. char current_op;
  34. char *operations;
  35. t_operation_fun op_arr[5];
  36.  
  37. op_arr[0] = ft_plus;
  38. op_arr[1] = ft_minus;
  39. op_arr[2] = ft_multiply;
  40. op_arr[3] = ft_div;
  41. op_arr[4] = ft_mod;
  42. current_op = argv[2][0];
  43. operations = "+-*/%\0";
  44. if (argc != 4 || invalid_argument(argv[2]) || \
  45. check_zero(current_op, ft_atoi(argv[3])))
  46. return (0);
  47. i = -1;
  48. while (operations[++i])
  49. if (operations[i] == current_op)
  50. {
  51. ft_putnbr(op_arr[i](ft_atoi(argv[1]), ft_atoi(argv[3])));
  52. ft_putchar('\n');
  53. return (0);
  54. }
  55. return (0);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement