Compare commits
No commits in common. "aff8effb1bbd94e85a4594365f0855dc4c065a73" and "964df6c718c93f7cf9c668362ae69e4e98f2ad92" have entirely different histories.
aff8effb1b
...
964df6c718
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +0,0 @@
|
|||||||
*.tar.gz
|
|
||||||
*.o
|
|
BIN
TD/TD2/sujet.pdf
BIN
TD/TD2/sujet.pdf
Binary file not shown.
@ -1,10 +0,0 @@
|
|||||||
CFLAGS = -Wall -Wextra -g -O3
|
|
||||||
|
|
||||||
main: main.c util.o
|
|
||||||
$(CC) $^ -o $@ $(CFLAGS)
|
|
||||||
|
|
||||||
%.o: %.c
|
|
||||||
$(CC) -c $< -o $@ $(CFLAGS)
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -f main util.o
|
|
@ -1,33 +0,0 @@
|
|||||||
#ifndef DEF_UTIL_H
|
|
||||||
#define DEF_UTIL_H
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Lit n chaînes de caractère
|
|
||||||
*/
|
|
||||||
char** read_strings(int n);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Libère n chaines de caractère
|
|
||||||
*/
|
|
||||||
void delete(char** elems, int n);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Affiche n chaines de caractère
|
|
||||||
*/
|
|
||||||
void print_strings(char** elems, int n);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tri par sélection
|
|
||||||
*/
|
|
||||||
void selsort(char** begin, char** end);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Plus petite chaine de caractères entre begin et end
|
|
||||||
*/
|
|
||||||
char** pos_min(char** begin, char** end);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* échanger *a et *b
|
|
||||||
*/
|
|
||||||
void swap(char** a, char** b);
|
|
||||||
#endif
|
|
@ -1,17 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "defns.h" // Pour les prototypes de fonctions implémentées
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
char **v;
|
|
||||||
int n;
|
|
||||||
printf("Nombre de chaînes de caractères à trier : ");
|
|
||||||
scanf("%d", &n);
|
|
||||||
v=read_strings(n);
|
|
||||||
if( v!=NULL ) {
|
|
||||||
selsort(v,v+n-1);
|
|
||||||
printf( "\nVecteur trié:\n" );
|
|
||||||
print_strings(v,n);
|
|
||||||
delete(v,n);
|
|
||||||
} else printf( "Erreur." );
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "defns.h"
|
|
||||||
|
|
||||||
char** read_strings(int n) {
|
|
||||||
char** elems = malloc(sizeof(char*)*n);
|
|
||||||
for (int i=0; i < n; i++) {
|
|
||||||
printf("Chaîne.%d : ", i+1);
|
|
||||||
scanf("%ms", &(elems[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
return elems;
|
|
||||||
}
|
|
||||||
|
|
||||||
void delete(char** elems, int n) {
|
|
||||||
for (int i=0; i < n; i++) {
|
|
||||||
free(elems[i]);
|
|
||||||
}
|
|
||||||
free(elems);
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_strings(char** elems, int n) {
|
|
||||||
for (int i=0; i < n; i++) {
|
|
||||||
printf(" %s\n", elems[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void selsort(char** begin, char** end) {
|
|
||||||
if (begin == end)
|
|
||||||
return;
|
|
||||||
|
|
||||||
char** mini = pos_min(begin, end);
|
|
||||||
swap(begin, mini);
|
|
||||||
|
|
||||||
return selsort(begin+1, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
char** pos_min(char** begin, char** end) {
|
|
||||||
char** mini = begin;
|
|
||||||
for (char** ptr=begin; ptr < end; ptr++) {
|
|
||||||
mini = strcmp(mini[0], ptr[0]) > 0 ? ptr : mini;
|
|
||||||
}
|
|
||||||
return mini;
|
|
||||||
}
|
|
||||||
|
|
||||||
void swap(char** a, char** b) {
|
|
||||||
char* tmp = *a;
|
|
||||||
*a = *b;
|
|
||||||
*b = tmp;
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user