-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
98 lines (90 loc) · 1.87 KB
/
main.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
#include "checker.h"
void init(t_ps *ps, int *stack_a, int *stack_b, char **argv)
{
int j;
int i;
j = 0;
while (j < ps->size)
{
stack_a[j] = 0;
stack_b[j] = 0;
j++;
}
j = 0;
i = 1;
while (i < ps->size + 1)
{
stack_a[j] = ft_atoi(argv[i]);
i++;
j++;
}
j = 0;
while (j < ps->size + 1)
{
if (stack_a[j] == 0)
stack_a[j] = 1;
j++;
}
}
void check_init(char **argv, t_ps *ps, int *stack_a, int *stack_b)
{
if (check_digit(argv) == -1 || check_dup(argv) == -1)
{
free(stack_a);
free(stack_b);
exit(0);
}
ps->bottom_a = ps->size - 1;
ps->top_b = ps->size;
init(ps, stack_a, stack_b, argv);
}
int check_order(int *stack_a, t_ps *ps)
{
int i;
i = 0;
while (i < ps->size - 1)
{
if (stack_a[i] > stack_a[i + 1])
return (0);
i++;
}
return (1);
}
void select_function(char *line, t_ps *ps, int *stack_a, int *stack_b)
{
if (line[0] == 's' && line[1] == 'a')
sa(stack_a, ps);
else if (line[0] == 's' && line[1] == 'b')
sb(stack_b, ps);
else if (line[0] == 'r' && line[1] == 'a')
ra(stack_a, ps);
else if (line[0] == 'r' && line[1] == 'b')
rb(stack_b, ps);
else if (line[0] == 'p' && line[1] == 'a')
pa(stack_b, stack_a, ps);
else if (line[0] == 'p' && line[1] == 'b')
pb(stack_a, stack_b, ps);
else if (line[0] == 'r' && line[1] == 'r' && line[2] == 'a')
rra(stack_a, ps);
else if (line[0] == 'r' && line[1] == 'r' && line[2] == 'b')
rrb(stack_b, ps);
}
int main(int argc, char **argv)
{
t_ps ps;
int *stack_a;
int *stack_b;
char *line;
stack_a = (int *)malloc(sizeof(int) * argc - 1);
stack_b = (int *)malloc(sizeof(int) * argc - 1);
ps.size = argc - 1;
check_init(argv, &ps, stack_a, stack_b);
while (get_next_line(STDIN_FILENO, &line))
select_function(line, &ps, stack_a, stack_b);
free(line);
if (check_order(stack_a, &ps) == 1 && ps.top_b == ps.size)
write(1, "OK", 2);
else
write(1, "KO", 2);
return (0);
}