-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.c
116 lines (106 loc) · 2.74 KB
/
rule.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
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rule.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alvutina <alvutina@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 10:15:18 by alvutina #+# #+# */
/* Updated: 2024/06/12 10:27:02 by alvutina ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void ft_for_swap(t_stack_elem *name_stack, char *message)
{
int first;
int temp;
first = search_first_elem_stack(name_stack);
temp = name_stack[first].value;
name_stack[first].value = name_stack[first +1].value;
name_stack[first + 1].value = temp;
ft_putstr_fd(message, 1);
}
void ft_for_push(t_stack_elem *a, t_stack_elem *b, char *message)
{
int first;
int end;
first = 0;
while (a[first].is_sorted != 1)
first++;
end = 0;
while (b[end].index != -1 && b[end].is_sorted != 1)
end++;
end--;
b[end].value = a[first].value;
a[first].value = 0;
b[end].is_sorted = 1;
a[first].is_sorted = 0;
ft_putstr_fd(message, 1);
}
void ft_for_rotate(t_stack_elem *stack, char *message)
{
int start;
int end;
int tempnum;
int tempnum2;
int i;
start = 0;
while (stack[start].index != -1 && stack[start].is_sorted != 1)
start++;
end = 0;
while (stack[end].index != -1)
end++;
end--;
i = end;
tempnum = stack[i].value;
while (i > start)
{
tempnum2 = stack[i - 1].value;
stack[i - 1].value = tempnum;
tempnum = tempnum2;
i--;
}
stack[end].value = tempnum;
ft_putstr_fd(message, 1);
}
void ft_for_reverse_rotate(t_stack_elem *stack, char *message)
{
int start;
int end;
int tempnum;
int tempnum2;
int i;
start = 0;
while (stack[start].index != -1 && stack[start].is_sorted != 1)
start++;
end = 0;
while (stack[end].index != -1)
end++;
end--;
i = start;
tempnum = stack[i].value;
while (i < end)
{
tempnum2 = stack[i + 1].value;
stack[i + 1].value = tempnum;
tempnum = tempnum2;
i++;
}
stack[start].value = tempnum;
ft_putstr_fd(message, 1);
}
void rr_rrr(t_stack_elem *a, t_stack_elem *b, int direction)
{
if (direction == 1)
{
ft_for_reverse_rotate(a, NULL);
ft_for_reverse_rotate(b, NULL);
ft_putstr_fd("rrr\n", 1);
}
else
{
ft_for_rotate(a, NULL);
ft_for_rotate(b, NULL);
ft_putstr_fd("rr\n", 1);
}
}