Version de base fini, gérer petit nombre et répartir les fonctions
This commit is contained in:
parent
25e6827055
commit
8f7727c38e
135
push_swap.c
135
push_swap.c
|
@ -6,7 +6,7 @@
|
||||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/23 16:48:12 by erey-bet #+# #+# */
|
/* Created: 2022/11/23 16:48:12 by erey-bet #+# #+# */
|
||||||
/* Updated: 2022/12/04 19:48:15 by erey-bet ### ########.fr */
|
/* Updated: 2022/12/05 22:58:30 by erey-bet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -15,6 +15,32 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void ft_putchar(char c)
|
||||||
|
{
|
||||||
|
write(1, &c, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_stack(t_stack sa, t_stack sb)
|
||||||
|
{
|
||||||
|
write(1, "A: ", 3);
|
||||||
|
for (int i = 0; i < sa.len; i++)
|
||||||
|
{
|
||||||
|
ft_putchar(sa.list[i] + '0');
|
||||||
|
write(1, ", ", 2);
|
||||||
|
}
|
||||||
|
ft_putchar('\n');
|
||||||
|
if (sb.len > 0)
|
||||||
|
{
|
||||||
|
write(1, "B: ", 3);
|
||||||
|
for (int i = 0; i < sb.len; i++)
|
||||||
|
{
|
||||||
|
ft_putchar(sb.list[i] + '0');
|
||||||
|
write(1, ", ", 2);
|
||||||
|
}
|
||||||
|
ft_putchar('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int get_min(long *tab, int len)
|
int get_min(long *tab, int len)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -46,46 +72,89 @@ unsigned int *get_index(long *tab, int len)
|
||||||
return (new);
|
return (new);
|
||||||
}
|
}
|
||||||
|
|
||||||
void swap(t_stack *s)
|
void swap(t_stack *s, char *message)
|
||||||
{
|
{
|
||||||
unsigned int c;
|
unsigned int c;
|
||||||
|
|
||||||
c = *s.list[0];
|
c = (*s).list[0];
|
||||||
*s.list[0] = *s.list[1];
|
(*s).list[0] = (*s).list[1];
|
||||||
*s.list[1] = c;
|
(*s).list[1] = c;
|
||||||
write(1, "sa", 2);
|
write(1, "s", 1);
|
||||||
|
write(1, message, 1);
|
||||||
|
write(1, "\n", 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void push(t_stack *s_push, t_stack *s_receive, char *message)
|
void push(t_stack *s_push, t_stack *s_receive, char *message)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int push;
|
unsigned int push;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
push = *s_push.list[0];
|
push = (*s_push).list[0];
|
||||||
while (++i < *s_push.len)
|
while (++i < (*s_push).len)
|
||||||
*s_push.list[i - 1] = *s_push.list[i];
|
(*s_push).list[i - 1] = (*s_push).list[i];
|
||||||
|
i = (*s_receive).len;
|
||||||
|
while (i-- > 0)
|
||||||
|
(*s_receive).list[i + 1] = (*s_receive).list[i];
|
||||||
|
(*s_receive).list[0] = push;
|
||||||
|
(*s_receive).len += 1;
|
||||||
|
(*s_push).len -= 1;
|
||||||
|
write(1, "p", 1);
|
||||||
|
write(1, message, 1);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rotate(t_stack *s, char *message)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
unsigned int push;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (++i < *s_receive.len)
|
push = (*s).list[0];
|
||||||
|
while (++i < (*s).len)
|
||||||
|
(*s).list[i - 1] = (*s).list[i];
|
||||||
|
(*s).list[(*s).len - 1] = push;
|
||||||
|
write(1, "r", 1);
|
||||||
|
write(1, message, 1);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*void push_number(t_stack *sa, int i)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}*/
|
||||||
}
|
|
||||||
|
|
||||||
void rotate()
|
int max_len_binary(t_stack s)
|
||||||
{
|
{
|
||||||
|
unsigned int i;
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int max;
|
||||||
|
|
||||||
}
|
y = -1;
|
||||||
|
max = 1;
|
||||||
void get_number_in_top(t_stack **s, int y, int len)
|
while (++y < s.len)
|
||||||
{
|
{
|
||||||
if (len - y + 1 < len / 2)
|
i = 1;
|
||||||
|
x = 1;
|
||||||
|
while (i < s.list[y])
|
||||||
|
{
|
||||||
|
i *= 2;
|
||||||
|
x++;
|
||||||
}
|
}
|
||||||
|
if (x > max)
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
return (max);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void push_swap(long *tab, int len)
|
void push_swap(long *tab, int len)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int y;
|
int y;
|
||||||
|
int x;
|
||||||
|
int len_sa;
|
||||||
t_stack sa;
|
t_stack sa;
|
||||||
t_stack sb;
|
t_stack sb;
|
||||||
|
|
||||||
|
@ -93,15 +162,33 @@ void push_swap(long *tab, int len)
|
||||||
sa.len = len;
|
sa.len = len;
|
||||||
sb.list = malloc(sizeof(unsigned int) * (len + 1));
|
sb.list = malloc(sizeof(unsigned int) * (len + 1));
|
||||||
sb.len = 0;
|
sb.len = 0;
|
||||||
|
print_stack(sa, sb);
|
||||||
|
x = max_len_binary(sa);
|
||||||
|
y = 0;
|
||||||
|
while (x-- > 0)
|
||||||
|
{
|
||||||
i = 0;
|
i = 0;
|
||||||
y = 1;
|
len_sa = sa.len;
|
||||||
while (i < len)
|
while (i < len_sa)
|
||||||
{
|
{
|
||||||
if ((sa.list[i] >> y) & 1 == 1)
|
if (((sa.list[0] >> y) & 1) == 0)
|
||||||
{
|
{
|
||||||
if (i > 0)
|
push(&sa, &sb, "b");
|
||||||
// get number in top
|
print_stack(sa, sb);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rotate(&sa, "a");
|
||||||
|
print_stack(sa, sb);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (sb.len > 0)
|
||||||
|
{
|
||||||
|
push(&sb, &sa, "a");
|
||||||
|
print_stack(sa, sb);
|
||||||
|
}
|
||||||
|
y++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,7 +213,7 @@ int main(int argc, char *argv[])
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
get_index(tab, y);
|
push_swap(tab, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
|
|
Loading…
Reference in a new issue