-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_do_op.c
56 lines (51 loc) · 1.74 KB
/
ft_do_op.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_do_op.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ciglesia <ciglesia@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/08/13 20:39:02 by ciglesia #+# #+# */
/* Updated: 2019/08/13 20:39:03 by ciglesia ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ccmpr(char *s1, char s2)
{
return (*s1 == s2);
}
static int exion(char *n1, char *n2)
{
if (ft_atoi(n1) == -2147483648 || ft_atoi(n2) == -2147483648)
{
if (ft_atoi(n1) == -1 || ft_atoi(n2) == -1)
return (1);
}
return (0);
}
int ft_do_op(int ac, char **av)
{
int nbr;
if (ac == 4)
{
if (ccmpr(av[2], '+'))
nbr = ft_atoi(av[1]) + ft_atoi(av[3]);
else if (ccmpr(av[2], '-'))
nbr = ft_atoi(av[1]) - ft_atoi(av[3]);
else if (ccmpr(av[2], '*'))
{
if (!exion(av[1], av[3]))
nbr = ft_atoi(av[1]) * ft_atoi(av[3]);
else
ft_putstr("2147483648");
}
else if (ccmpr(av[2], '/') && (ft_atoi(av[3]) != 0))
nbr = ft_atoi(av[1]) / ft_atoi(av[3]);
else if (ccmpr(av[2], '%'))
nbr = ft_atoi(av[1]) % ft_atoi(av[3]);
if (!exion(av[1], av[3]) && (ft_atoi(av[3]) != 0))
ft_putnbr(nbr);
}
ft_putstr("\n");
return (0);
}