-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
104 lines (93 loc) · 2.01 KB
/
utils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: daxferna <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 01:59:23 by daxferna #+# #+# */
/* Updated: 2024/12/23 06:03:32 by daxferna ### ########.fr */
/* */
/* ************************************************************************** */
#include "../push_swap.h"
int isnum(char *str)
{
int i;
i = 0;
while (str[i])
{
if (!ft_isdigit(str[i]))
return (0);
i++;
}
return (1);
}
bool imprvatoi(char *str, int *num)
{
int sign;
int i;
long res;
sign = 1;
i = 0;
res = 0;
*num = 0;
if (str[i] == '-' || str[i] == '+')
{
if (str[i++] == '-')
sign = -1;
if (!str[i])
return (0);
}
while (str[i])
{
if (!ft_isdigit(str[i]))
return (0);
res = res * 10 + (str[i++] - '0');
if ((sign == 1 && res > INT_MAX) || (sign == -1 && -1 * res < INT_MIN))
return (0);
}
*num = (int)(sign * res);
return (1);
}
void freesplit(char **split)
{
int i;
i = 0;
while (split[i])
free(split[i++]);
free(split);
}
bool sorted(t_num *stack_a)
{
t_num *current;
current = stack_a;
while (current->next)
{
if (current->content > current->next->content)
return (0);
current = current->next;
}
return (1);
}
int first_num(t_num *stack)
{
t_num *tmp;
int min;
int i;
int res;
tmp = stack;
min = tmp->index;
i = 0;
res = 0;
while (tmp)
{
if (tmp->index < min)
{
min = tmp->index;
res = i;
}
i++;
tmp = tmp->next;
}
return (res);
}