-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_checker.c
115 lines (108 loc) · 3.06 KB
/
ft_checker.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
105
106
107
108
109
110
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: flfische <flfische@student.42heilbronn. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/29 13:55:16 by flfische #+# #+# */
/* Updated: 2024/04/10 13:45:40 by flfische ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_push_swap.h"
t_ops ft_parse_op(char *op)
{
if (!ft_strcmp(op, "sa") || !ft_strcmp(op, "sa\n"))
return (sa);
if (!ft_strcmp(op, "sb") || !ft_strcmp(op, "sb\n"))
return (sb);
if (!ft_strcmp(op, "ss") || !ft_strcmp(op, "ss\n"))
return (ss);
if (!ft_strcmp(op, "pa") || !ft_strcmp(op, "pa\n"))
return (pa);
if (!ft_strcmp(op, "pb") || !ft_strcmp(op, "pb\n"))
return (pb);
if (!ft_strcmp(op, "ra") || !ft_strcmp(op, "ra\n"))
return (ra);
if (!ft_strcmp(op, "rb") || !ft_strcmp(op, "rb\n"))
return (rb);
if (!ft_strcmp(op, "rr") || !ft_strcmp(op, "rr\n"))
return (rr);
if (!ft_strcmp(op, "rra") || !ft_strcmp(op, "rra\n"))
return (rra);
if (!ft_strcmp(op, "rrb") || !ft_strcmp(op, "rrb\n"))
return (rrb);
if (!ft_strcmp(op, "rrr") || !ft_strcmp(op, "rrr\n"))
return (rrr);
return (0);
}
int ft_execute_ops(t_push_swap *ps, t_ops op)
{
if (op == sa)
ft_swap_a(ps);
else if (op == sb)
ft_swap_b(ps);
else if (op == ss)
ft_swap_both(ps);
else if (op == pa)
ft_push_a(ps);
else if (op == pb)
ft_push_b(ps);
else if (op == ra)
ft_rot_a(ps);
else if (op == rb)
ft_rot_b(ps);
else if (op == rr)
ft_rot_both(ps);
else if (op == rra)
ft_revrot_a(ps);
else if (op == rrb)
ft_revrot_b(ps);
else if (op == rrr)
ft_revrot_both(ps);
else
return (0);
return (1);
}
int ft_checker_loop(t_push_swap *ps)
{
char *line;
t_ops op;
line = get_next_line(0);
while (line)
{
op = ft_parse_op(line);
if (!op)
return (free(line), 1);
if (!ft_execute_ops(ps, op))
return (free(line), 1);
free(line);
line = get_next_line(0);
}
free(line);
return (0);
}
int main(int argc, char **argv)
{
int *array;
int len;
t_push_swap *ps;
if (argc < 2)
return (0);
ft_set_input_error(0);
len = ft_parse_input(argc, argv, &array);
if (*ft_get_input_error() == 1)
return (free(array), ft_putstr_fd("Error\n", 2), 1);
ps = malloc(sizeof(t_push_swap));
if (!ps)
return (free(array), ft_putstr_fd("Error\n", 2), 1);
ft_init_stacks(ps, array, len, 0);
if (ft_checker_loop(ps))
return (ft_free_stacks(ps), ft_putstr_fd("Error\n", 2), 1);
if (ft_is_sorted(ps->a.stack, len))
ft_printf("OK\n");
else
ft_printf("KO\n");
ft_free_stacks(ps);
return (0);
}