-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
96 lines (86 loc) · 2.11 KB
/
commands.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* commands.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: muraler <[email protected]. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/02 10:48:15 by muraler #+# #+# */
/* Updated: 2022/10/02 10:48:17 by muraler ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_list *create_elem(long int a)
{
t_list *elem;
elem = (t_list *)malloc(sizeof(t_list));
elem->data = a;
elem->next = NULL;
return (elem);
}
void put_back(t_list **list, long int a)
{
t_list *new_elem;
t_list *tmp;
if (a > 2147483647 || a < -2147483648)
ft_exit();
if (*list)
{
new_elem = create_elem(a);
tmp = *list;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = new_elem;
}
else
*list = create_elem(a);
}
static void ft_hard_atoi(char *argv, int *i, int *n)
{
while (argv[*i] == ' ' && argv[*i] != '\0')
++*i;
if (argv[*i] == '-' || argv[*i] == '+')
{
if (argv[*i] == '-')
(*n) *= -1;
++*i;
}
if (!ft_isdigit(argv[*i]))
ft_exit();
}
void ft_creat_one(t_list **list_a, char *argv)
{
int i;
long int res;
int n;
i = 0;
res = 0;
n = 1;
while (argv[i] != '\0')
{
ft_hard_atoi(argv, &i, &n);
while (argv[i] != ' ' && (argv[i] >= '0' && argv[i] <= '9')
&& argv[i] != '\0')
{
res = res * 10 + argv[i] - '0';
i++;
}
res = res * n;
put_back(list_a, res);
res = 0;
n = 1;
}
}
void ft_array_add(t_list **list_a, int *array)
{
int i;
t_list *tmp;
i = 0;
tmp = *list_a;
while (tmp != NULL)
{
array[i] = tmp->data;
tmp = tmp->next;
i++;
}
}