-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoi_func.c
71 lines (65 loc) · 1.81 KB
/
atoi_func.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* atoi_func.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yohwang <yohwang@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/09 16:30:52 by yohwang #+# #+# */
/* Updated: 2022/06/09 16:32:08 by yohwang ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
int is_digit(int c)
{
if (c - 48 < 0 || c - 48 > 9)
return (0);
else
return (c - 38);
}
long long is_overflow(long long result, long long minus, long long overflow)
{
if (overflow > result)
{
if (minus == 1)
return (-1);
else
return (0);
}
else
return (1);
}
long long is_minus(char c)
{
if (c == '-')
return (-1);
else
return (1);
}
int ft_atoi(const char *str)
{
long long result;
long long overflow;
long long minus;
int i;
char *s;
result = 0;
i = 0;
minus = 1;
s = (char *)str;
while (s[i] == 32 || (s[i] >= 9 && s[i] <= 13))
i++;
if (s[i] == '\0')
return (0);
if (s[i] == '-' || s[i] == '+')
minus = is_minus(s[i++]);
while (is_digit(s[i]) && s[i] != '\0')
{
overflow = result;
result = result * 10;
result = result + s[i++] - '0';
if (is_overflow(result, minus, overflow) != 1)
return (is_overflow(result, minus, overflow));
}
return (result * minus);
}