-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.c
104 lines (95 loc) · 2.08 KB
/
checker.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvieira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/16 20:19:03 by fvieira #+# #+# */
/* Updated: 2022/12/16 20:19:10 by fvieira ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void checker(t_stack *stack)
{
int i;
int j;
i = 0;
while (i < stack->len)
{
j = i + 1;
while (j < stack->len)
{
if (stack->stack[i] == stack->stack[j]
|| stack->stack[j] > INT_MAX || stack->stack[j] < INT_MIN)
{
ft_printf("Error\n");
free(stack->stack);
exit(1);
}
j++;
}
i++;
}
if (isordered(stack) == 0)
{
free(stack->stack);
exit(1);
}
}
void finalpos(t_stack *stack)
{
int count1;
int count2;
int lower;
count1 = 0;
while (count1 < stack->len)
{
lower = 0;
count2 = 0;
while (count2 < stack->len)
{
if (stack->stack[count2] < stack->stack[count1])
lower++;
count2++;
}
stack->finalpos[count1] = lower;
count1++;
}
}
void zerofirstplace(t_stack *stack)
{
int count;
count = 0;
while (stack->finalpos[count] != 0)
count++;
if (count * 2 >= stack->len)
{
count++;
while (count != stack->len + 1)
{
do_rot(stack, 'a');
count++;
}
}
else
{
while (count > 0)
{
do_revrot(stack, 'a');
count--;
}
}
}
int isordered(t_stack *stack)
{
int count;
count = 0;
while (count < stack->len - 1)
{
if (stack->stack[count] > stack->stack[count + 1])
return (1);
count++;
}
return (0);
}