-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_bonus.c
More file actions
122 lines (112 loc) · 2.99 KB
/
main_bonus.c
File metadata and controls
122 lines (112 loc) · 2.99 KB
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
116
117
118
119
120
121
122
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: uyilmaz <uyilmaz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/06 20:14:38 by uyilmaz #+# #+# */
/* Updated: 2023/03/07 05:47:13 by uyilmaz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap_bonus.h"
int is_list_sorted(t_list *list)
{
t_list *next;
t_list *prev;
prev = list;
next = list->next;
while (next)
{
if (*(prev->content) > *(next->content))
return (0);
prev = prev->next;
next = next->next;
}
return (1);
}
void executer(char *instruction, t_list **stack_a, t_list **stack_b)
{
if (ft_strncmp(instruction, "pb", 2))
pb(stack_a, stack_b);
else if (ft_strncmp(instruction, "pa", 2))
pa(stack_a, stack_b);
else if (ft_strncmp(instruction, "ra", 2))
ra(stack_a);
else if (ft_strncmp(instruction, "rb", 2))
rb(stack_b);
else if (ft_strncmp(instruction, "rra", 3))
rra(stack_a);
else if (ft_strncmp(instruction, "rrb", 3))
rrb(stack_b);
else if (ft_strncmp(instruction, "rrr", 3))
rrr(stack_a, stack_b);
else if (ft_strncmp(instruction, "rr", 2))
rr(stack_a, stack_b);
else if (ft_strncmp(instruction, "sa", 2))
sa(stack_a);
}
int check_it(t_list **stack_a, char **instructions)
{
t_list *stack_b;
int i;
stack_b = NULL;
if (!instructions)
{
print_error("Error\n");
return (0);
}
i = 0;
while (instructions[i])
{
executer(instructions[i], stack_a, &stack_b);
i++;
}
free_double_char(instructions);
if (is_list_sorted(*stack_a) && !stack_b)
return (1);
ft_lstclear(&stack_b);
ft_putstr("KO\n");
return (0);
}
char **get_instructions(void)
{
char *instructions;
char *line;
char **result;
instructions = get_next_line(0);
line = get_next_line(0);
while (line)
{
instructions = ft_strjoin_v2(instructions, line);
free (line);
line = get_next_line(0);
}
result = ft_split(instructions, '\n');
free(instructions);
return (result);
}
int main(int ac, char **av)
{
char **instructions;
char *data_str;
int **data_arr;
int size;
t_list *stack_a;
if (ac == 1)
return (0);
instructions = get_instructions();
data_str = all_in_one(ac - 1, av + 1);
if (char_check(data_str))
return (1);
data_arr = to_arr(data_str, &size);
if (!data_arr)
return (2);
if (check_doubles(data_arr, size))
return (1);
stack_a = list_initializer(size, data_arr);
if (!stack_a)
return (2);
if (check_it(&stack_a, instructions))
ft_putstr("OK\n");
}