-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_swap.c
82 lines (73 loc) · 1.33 KB
/
push_swap.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
#include "push_swap.h"
int main(int argc, char **argv)
{
t_two_stacks *stacks;
int32_t i;
i = argc - 1;
if (argc == 1)
return (0);
stacks = stacks_init(NULL, NULL);
if (!stacks)
error_handle(&stacks);
if (argc == 2)
parse_line(argv[1], &stacks);
else
{
while (i > 0)
{
try_parse(argv[i], &stacks);
--i;
}
}
if (is_no_repited(stacks->a))
error_handle(&stacks);
sort_distributor(&stacks);
return (0);
}
void try_parse(char *str, t_two_stacks **stacks)
{
int32_t i;
int64_t res;
i = 0;
if (str[0] == '-' && str[1] > '9' && str[1] < '0')
error_handle(stacks);
if (str[0] == '-' && !str[1])
error_handle(stacks);
++i;
while (str[i])
{
if (str[i] > '9' || str[i] < '0')
error_handle(stacks);
++i;
}
res = (int64_t)atol(&str[0]);
if (res > I_32_MAX || res < I_32_MIN)
error_handle(stacks);
(*stacks)->a = push((*stacks)->a, (int32_t)res, 0, 0);
}
void error_handle(t_two_stacks **stacks)
{
write(2, "Error\n", 6);
free_stacks(stacks);
exit(1);
}
void free_stacks(t_two_stacks **stacks)
{
int32_t for_free;
while ((*stacks)->a)
for_free = pop(&((*stacks)->a));
while ((*stacks)->b)
for_free = pop(&((*stacks)->a));
}
int32_t is_sorted(t_stack *a)
{
t_stack *tmp;
tmp = a;
while (tmp->next)
{
if (tmp->value > tmp->next->value)
return (1);
tmp = tmp->next;
}
return (0);
}