-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.c
More file actions
86 lines (79 loc) · 2.63 KB
/
operations.c
File metadata and controls
86 lines (79 loc) · 2.63 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* operations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: scosta-j <scosta-j@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/21 15:13:24 by scosta-j #+# #+# */
/* Updated: 2023/08/22 07:04:18 by scosta-j ### ########.fr */
/* */
/* ************************************************************************** */
#include "stack.h"
/**
* @brief performs the reverse rotate operation on both stacks
*/
int rev_rotate_both(t_list **stack_a, t_list **stack_b)
{
return (rev_rotate(stack_a) && rev_rotate(stack_b));
}
/**
* @brief performs the rotate operation on both stacks
*/
int rotate_both(t_list **stack_a, t_list **stack_b)
{
return (rotate(stack_a) && rotate(stack_b));
}
/**
* @brief performs the swap operation on both stacks
*/
int swap_both(t_list **stack_a, t_list **stack_b)
{
return (swap(stack_a) && swap(stack_b));
}
/**
* @brief executes the selected rotate/rev rotate operation
*/
int exec_rotate(char *ptr, t_list **stack_a, t_list **stack_b, int size)
{
if (!ft_strncmp("ra", ptr, size))
return (rotate(stack_a));
if (!ft_strncmp("rb", ptr, size))
return (rotate(stack_b));
if (!ft_strncmp("rr", ptr, size))
return (rotate_both(stack_a, stack_b));
if (!ft_strncmp("rra", ptr, size))
return (rev_rotate(stack_a));
if (!ft_strncmp("rrb", ptr, size))
return (rev_rotate(stack_b));
if (!ft_strncmp("rrr", ptr, size))
return (rev_rotate_both(stack_a, stack_b));
return (0);
}
/**
* @brief executes the move indicated by the string ptr
* if sucessful prints the move
*/
void execute_move(char *ptr, t_list **stack_a, t_list **stack_b)
{
int size;
int sucess;
size = ft_strlen(ptr);
if (ptr[0] == 'r')
sucess = exec_rotate(ptr, stack_a, stack_b, size);
if (!ft_strncmp("sa", ptr, size))
sucess = swap(stack_a);
if (!ft_strncmp("sb", ptr, size))
sucess = swap(stack_b);
if (!ft_strncmp("ss", ptr, size))
sucess = swap_both(stack_a, stack_b);
if (!ft_strncmp("pa", ptr, size))
sucess = push(stack_b, stack_a);
if (!ft_strncmp("pb", ptr, size))
sucess = push(stack_a, stack_b);
if (sucess)
{
write (1, ptr, size);
write (1, "\n", 1);
}
}