Commit cd786e3e authored by W.Dobson's avatar W.Dobson
Browse files

Multiples modification pour fixer erreur au niveau de la fonction de tri +...

Multiples modification pour fixer erreur au niveau de la fonction de tri + modif fonction peek + modif file main.c + fixe d'autre problèmes
parent fb7dfdd5
CC=gcc -Wall -Wextra -g -fsanitize=address -fsanitize=leak
run: stack.o main.o
$(CC) stack.o main.o -o run -lm
$(CC) stack.o main.o -o test -lm
stack.o: stack.c stack.h
$(CC) -c stack.c -lm
......@@ -10,6 +10,6 @@ main.o: main.c
$(CC) -c main.c -lm
clean:
mv -f *.o run
mv -f *.o test
debuild: clean run
\ No newline at end of file
debuild: clean test
\ No newline at end of file
......@@ -7,21 +7,31 @@
int main(int argc, char **argv){
int tab_size = atoi(argv[1]);
if(tab_size != argc - 2)
printf("La taille du tableau ne correspond pas au nombre de chiffres donner en paramètre !!");
if(argc < 3)
printf("Vous devez rentrer la taille du tableau et les différentes valeurs à y rentrer\n");
else if(atoi(argv[1]) != argc - 2)
printf("La taille du tableau ne correspond pas au nombre de chiffres donner en paramètre !!\n");
else{
int tab_size = atoi(argv[1]);
int32_t *tab_to_sort = malloc(tab_size * sizeof(int));
for(int i = 0; i < tab_size; i++){
tab_to_sort[i] = atoi(argv[i+2]);
}
printf("Tableau à trier: ");
for(int i = 0; i < tab_size; i++){
printf("%d ", tab_to_sort[i]);
}
printf("\n\n");
stack *g = malloc(sizeof(stack));
stack *d = malloc(sizeof(stack));
stack_init(g, tab_size);
......@@ -29,6 +39,7 @@ int main(int argc, char **argv){
stack_sort(g, d, tab_to_sort, tab_size);
printf("Pile triée: ");
stack_print(g);
......
......@@ -20,7 +20,7 @@ void stack_print(stack *s){
printf("%d ", s->values[i]);
}
printf("\n\n");
printf("\n");
}
......@@ -60,12 +60,17 @@ void stack_pop(stack *s, int32_t *val){
void stack_peek(stack s, int32_t *val){
if(s.top == -1)
void stack_peek(stack s, int32_t *val, int32_t *top){
if(s.top == -1){
*val = 0;
else
*top = -1;
}else{
*val = s.values[s.top];
*top = 0;
}
}
......@@ -95,36 +100,42 @@ void stack_sort(stack *sG, stack *sD, int32_t tab[], int size){
int32_t next_val_left;
int32_t next_val_right;
int32_t val_poped;
int32_t topG = 0;
int32_t topD = 0;
stack_peek(*sG, &next_val_left);
stack_peek(*sD, &next_val_right);
stack_peek(*sG, &next_val_left, &topG);
stack_peek(*sD, &next_val_right, &topD);
if(next_val_left < tab[i]){
if(topG != -1 && next_val_left < tab[i]){
stack_pop(sG, &val_poped);
stack_push(sD, val_poped);
for(int n = 0; n <= sG->top; n++){
stack_peek(*sG, &next_val_left);
stack_peek(*sG, &next_val_left, &topG);
if(next_val_left < tab[i]){
if(topG != -1 && next_val_left < tab[i]){
stack_pop(sG, &val_poped);
stack_push(sD, val_poped);
}
}
}else if(next_val_right > tab[i]){
}else if(topD != -1 && next_val_right > tab[i]){
stack_pop(sD, &val_poped);
stack_push(sG, val_poped);
for(int n = 0; n <= sD->top; n++){
stack_peek(*sD, &next_val_right);
stack_peek(*sD, &next_val_right, &topD);
if(next_val_right > tab[i]){
if(topD != -1 && next_val_right > tab[i]){
stack_pop(sD, &val_poped);
stack_push(sG, val_poped);
......
......@@ -18,7 +18,7 @@ void stack_push(stack *s, int32_t value);
void stack_pop(stack *s, int32_t *val);
void stack_peek(stack s, int32_t *val);
void stack_peek(stack s, int32_t *val, int32_t *top);
bool stack_is_empty(stack s);
......
File added
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment