Add TD2
This commit is contained in:
parent
964df6c718
commit
0a41153316
BIN
TD/TD2/sujet.pdf
Normal file
BIN
TD/TD2/sujet.pdf
Normal file
Binary file not shown.
10
TD/TD2/td2_lucas/Makefile
Normal file
10
TD/TD2/td2_lucas/Makefile
Normal file
@ -0,0 +1,10 @@
|
||||
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
|
33
TD/TD2/td2_lucas/defns.h
Normal file
33
TD/TD2/td2_lucas/defns.h
Normal file
@ -0,0 +1,33 @@
|
||||
#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
|
17
TD/TD2/td2_lucas/main.c
Normal file
17
TD/TD2/td2_lucas/main.c
Normal file
@ -0,0 +1,17 @@
|
||||
#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." );
|
||||
}
|
52
TD/TD2/td2_lucas/util.c
Normal file
52
TD/TD2/td2_lucas/util.c
Normal file
@ -0,0 +1,52 @@
|
||||
#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…
Reference in New Issue
Block a user