-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbonus.c
95 lines (86 loc) · 2.7 KB
/
bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bnidia <bnidia@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 08:42:42 by bnidia #+# #+# */
/* Updated: 2022/04/11 17:23:43 by bnidia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void parse_operation(t_listc **a, t_listc **b, char *str);
static void parse_operation2(t_listc **a, t_listc **b, char *str);
static void print_result(t_listc **a, t_listc **b);
/* This is bonus function - checker */
int main(int argc, char *argv[])
{
t_listc *a;
t_listc *b;
t_mmm minmaxmed;
char *str;
a = NULL;
b = NULL;
if (argc == 1)
return (0);
a = init_stack(argc, argv);
check_if_duplicated(&a, &minmaxmed);
str = get_next_line(0);
while (str)
{
parse_operation(&a, &b, str);
free(str);
str = get_next_line(0);
}
free(str);
print_result(&a, &b);
if (a != NULL)
ft_lstc_clear(&a);
if (b != NULL)
ft_lstc_clear(&b);
return (0);
}
static void print_result(t_listc **a, t_listc **b)
{
if (check_if_sorted(a) && *b == NULL)
write(1, "OK\n", 3);
else
write(1, "KO\n", 3);
}
static void parse_operation(t_listc **a, t_listc **b, char *str)
{
size_t strlen;
strlen = ft_strlen((const char *)str);
if (!ft_strncmp(str, "sa\n", strlen))
sa(*a, false);
else if (!ft_strncmp(str, "sb\n", strlen) && b != NULL)
sb(*b, false);
else if (!ft_strncmp(str, "ss\n", strlen))
ss(*a, *b, false);
else if (!ft_strncmp(str, "pa\n", strlen))
pa(a, b, false);
else if (!ft_strncmp(str, "pb\n", strlen))
pb(a, b, false);
else
parse_operation2(a, b, str);
}
static void parse_operation2(t_listc **a, t_listc **b, char *str)
{
size_t strlen;
strlen = ft_strlen((const char *)str);
if (!ft_strncmp(str, "ra\n", strlen))
ra(a, false);
else if (!ft_strncmp(str, "rb\n", strlen) && b != NULL)
rb(b, false);
else if (!ft_strncmp(str, "rr\n", strlen))
rr(a, b, false);
else if (!ft_strncmp(str, "rra\n", strlen))
rra(a, false);
else if (!ft_strncmp(str, "rrb\n", strlen) && b != NULL)
rrb(b, false);
else if (!ft_strncmp(str, "rrr\n", strlen))
rrr(a, b, false);
else
ft_error();
}