-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker_bonus.c
83 lines (76 loc) · 1.76 KB
/
checker_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
#include "checker_bonus.h"
int issorted(t_stack *stack)
{
while (stack)
{
if (stack->prev && stack->el < stack->prev->el)
return (0);
stack = stack->next;
}
return (1);
}
void ft_execorerror(char *instruction, t_stacks *stacks, int a)
{
if (!ft_strcmp(instruction, "sa"))
ft_swapa(stacks);
else if (!ft_strcmp(instruction, "sb"))
ft_swapb(stacks);
else if (!ft_strcmp(instruction, "ss"))
ft_swapa(stacks) && ft_swapb(stacks);
else if (!ft_strcmp(instruction, "ra"))
ft_rotatea(stacks);
else if (!ft_strcmp(instruction, "rb"))
ft_rotateb(stacks);
else if (!ft_strcmp(instruction, "rr"))
ft_rotatea(stacks) && ft_rotateb(stacks);
else if (!ft_strcmp(instruction, "pa"))
ft_btoa(stacks);
else if (!ft_strcmp(instruction, "pb"))
ft_atob(stacks);
else if (!ft_strcmp(instruction, "rrb"))
ft_revrotateb(stacks);
else if (!ft_strcmp(instruction, "rra"))
ft_revrotatea(stacks);
else if (!ft_strcmp(instruction, "rrr"))
ft_revrotatea(stacks) && ft_revrotateb(stacks);
else if (a == 0)
ft_error();
}
void ft_readline(t_stacks *stacks)
{
char *line;
int size;
size = (int)stacks->size;
while (get_next_line(0, &line) > 0)
{
ft_execorerror(line, stacks, 0);
free(line);
}
ft_execorerror(line, stacks, 1);
free(line);
if (issorted(stacks->mya) && size == ft_count(stacks->mya))
write(1, "OK\n", 3);
else
write(1, "KO\n", 3);
}
void ft_startwork(char **av)
{
t_stacks *stacks;
stacks = malloc(sizeof(t_stacks));
stacks->sorted = NULL;
stacks->mya = ft_filla(av);
stacks->size = ft_count(stacks->mya);
while (stacks->mya->prev)
stacks->mya = stacks->mya->prev;
ft_readline(stacks);
freethestack(stacks);
}
int main(int ac, char **av)
{
if (ft_checkint(av))
ft_error();
if (ac < 2)
return (0);
ft_startwork(av);
return (0);
}