init projet
This commit is contained in:
47
tp2/compare.c
Normal file
47
tp2/compare.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "compare.h"
|
||||
|
||||
// tableau des tailles des types (ne pas modifier)
|
||||
int psize[] = {
|
||||
2, 1, 5, MAXSTR, 9,
|
||||
};
|
||||
|
||||
// tableau des fonctions de comparaison
|
||||
fcmp cmp[] = {
|
||||
fcmp_int, fcmp_char, fcmp_double, fcmp_string, fcmp_pointx,
|
||||
};
|
||||
|
||||
// tableau des types pour l'aide
|
||||
char *type[] = {
|
||||
"int", "integers in [0,100[ (default)",
|
||||
"char", "capital letters in ['A','Z'[",
|
||||
"double", "real numbers in ]-1.00,+1.00[",
|
||||
"string", "strings of length at most " xstr(MAXSTR),
|
||||
"point", "real points of [0,9.9]×[0,9.9] with 'x' like fcmp()",
|
||||
};
|
||||
|
||||
// tableau des tailles des types
|
||||
int size[] = {
|
||||
sizeof(int), sizeof(char), sizeof(double), sizeof(string), sizeof(point),
|
||||
};
|
||||
|
||||
int fcmp_int(const void *x, const void *y) {
|
||||
int a = *((int *)x);
|
||||
int b = *((int *)y);
|
||||
return (a < b) ? -1 : (a > b);
|
||||
}
|
||||
|
||||
int fcmp_char(const void *x, const void *y) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fcmp_double(const void *x, const void *y) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fcmp_string(const void *x, const void *y) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fcmp_pointx(const void *p, const void *q) {
|
||||
return 0;
|
||||
}
|
||||
61
tp2/compare.h
Normal file
61
tp2/compare.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#ifndef _COMPARE_H
|
||||
#define _COMPARE_H
|
||||
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// réel aléatoire dans [0,1]
|
||||
#define RAND01 ((double)random() / RAND_MAX)
|
||||
#define RANDINT(n) (random() % (n))
|
||||
|
||||
/* compare.c */
|
||||
int fcmp_int(const void *x, const void *y);
|
||||
int fcmp_char(const void *x, const void *y);
|
||||
int fcmp_double(const void *x, const void *y);
|
||||
int fcmp_string(const void *x, const void *y);
|
||||
int fcmp_pointx(const void *p, const void *q);
|
||||
|
||||
#define BAR "-" // un tiret
|
||||
#define MAXSTR 7 // taille max d'une string
|
||||
#define xstr(s) str(s) // permet l'expansion d'une macro
|
||||
#define str(s) #s // Ex: scanf("%"xstr(DMAX)"s",buffer);
|
||||
|
||||
typedef int (*fcmp)(const void *, const void *); // type fonction de comparaison
|
||||
|
||||
typedef char *string; // type chaîne de caractères
|
||||
|
||||
typedef struct {
|
||||
double x, y;
|
||||
} point;
|
||||
|
||||
#define NUMTYPES 5
|
||||
|
||||
typedef enum { // pour les switch()
|
||||
INT = 0,
|
||||
CHAR,
|
||||
DOUBLE,
|
||||
STRING,
|
||||
POINT,
|
||||
} type_t;
|
||||
|
||||
// tableau des types pour l'aide
|
||||
extern char *type[];
|
||||
|
||||
// tableau des tailles des types
|
||||
extern int size[];
|
||||
|
||||
// tableau des tailles d'affichage des types
|
||||
extern int psize[];
|
||||
|
||||
// tableau des fonctions de comparaison
|
||||
extern fcmp cmp[];
|
||||
|
||||
#endif
|
||||
31
tp2/genprint.c
Normal file
31
tp2/genprint.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "compare.h"
|
||||
|
||||
// Affiche T[i] avec le bon format suivant le type t
|
||||
void print(type_t t, void *T, int i) {
|
||||
// Pour l'affichage des types:
|
||||
// - entier, format %2i
|
||||
// - caractère, format %c
|
||||
// - double, format %+1.2lf
|
||||
// - chaîne, format %s
|
||||
// - point, format (%.1lf,%.1lf)
|
||||
}
|
||||
|
||||
// Initialisation aléatoire de l'élément T[i]
|
||||
void init(type_t t, void *T, int i) {
|
||||
// Pour INT: entiers aléatoires dans [0,100[
|
||||
// Pour CHAR: lettres majuscules aléatoires
|
||||
// Pour DOUBLE: double aléatoires dans [-1,+1] avec 2 chiffres
|
||||
// Pour STRING: construit une chaîne aléatoire d'au plus MAXSTR char
|
||||
// Pour POINT: point aléatoire de [0,10[ x [0,10[ avec 1 chiffre
|
||||
}
|
||||
|
||||
// Affiche n fois le même la chaîne s.
|
||||
void rule(int n, string s) {
|
||||
for (int i = 0; i < n; i++)
|
||||
printf("%s", s);
|
||||
}
|
||||
|
||||
// Affiche le contenu du tableau array d'éléments de type t
|
||||
void print_array(void *array, type_t t, int nb) {
|
||||
printf("\n\n");
|
||||
}
|
||||
16
tp2/genprint.h
Normal file
16
tp2/genprint.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef GENPRINT_H_
|
||||
#define GENPRINT_H_
|
||||
|
||||
#include "genprint.h"
|
||||
|
||||
/* genprint.c */
|
||||
// Affiche T[i] avec le bon format suivant le type t
|
||||
void print(type_t t, void *T, int i);
|
||||
|
||||
// Initialisation aléatoire de l'élément T[i]
|
||||
void init(type_t t, void *T, int i);
|
||||
|
||||
// Affiche le contenu du tableau array d'éléments de type t
|
||||
void print_array(void *array, type_t t, int nb);
|
||||
|
||||
#endif // GENPRINT_H_
|
||||
46
tp2/main.c
Normal file
46
tp2/main.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "compare.h"
|
||||
#include "genprint.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int i;
|
||||
type_t t;
|
||||
int ntype = NUMTYPES; // nombre de types possibles
|
||||
int n = (argc >= 2) ? atoi(argv[1]) : -1; // n = nombre d'éléments
|
||||
const char *s = (argc >= 3) ? argv[2] : type[INT]; // t = type des éléments
|
||||
|
||||
for (t = 0; t < ntype; t++)
|
||||
if (!strcmp(s, type[2 * t]))
|
||||
break; // type trouvé !
|
||||
if (n < 0 || t == ntype) { // erreur
|
||||
printf("\n Usage: %s n [t]", argv[0]);
|
||||
printf("\n Ex.: %s 10 int\n\n", argv[0]);
|
||||
printf(" n = number of random elements\n");
|
||||
printf(" t = type of elements & fcmp():\n\n");
|
||||
for (t = 0; t < ntype; t++) {
|
||||
printf(" '%s' ", type[2 * t]);
|
||||
// rule(12 - strlen(type[2 * t]), ".");
|
||||
printf(" %s\n", type[2 * t + 1]);
|
||||
}
|
||||
printf("\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
unsigned seed = time(NULL) % 1000;
|
||||
srandom(seed);
|
||||
printf("\nseed: %u\n", seed); // pour rejouer la même chose au cas où
|
||||
|
||||
void *T = malloc(n * size[t]); // tableau initial
|
||||
|
||||
printf("Tableau avec %i elements de type '%s'\n", n, type[2 * t]);
|
||||
printf("(%s)\n", type[2 * t + 1]);
|
||||
|
||||
// affichage et création de T[]
|
||||
printf("input array: ");
|
||||
for (i = 0; i < n; i++) {
|
||||
init(t, T, i);
|
||||
print(t, T, i);
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
13
tp2/sujet/.ccls
Normal file
13
tp2/sujet/.ccls
Normal file
@@ -0,0 +1,13 @@
|
||||
gcc-13
|
||||
%c -std=std2x
|
||||
%c -Wall
|
||||
%c -Wextra
|
||||
%c -pedantic
|
||||
%c -Wshadow
|
||||
%c -Wpointer-arith
|
||||
%c -Wcast-qual
|
||||
%c -Wstrict-prototypes
|
||||
%c -Wmissing-prototypes
|
||||
%c -Wno-gnu-zero-variadic-macro-arguments
|
||||
%c -I/opt/homebrew/include/
|
||||
%c --check-level=exhaustive
|
||||
3640
tp2/sujet/.uncrustify
Normal file
3640
tp2/sujet/.uncrustify
Normal file
File diff suppressed because it is too large
Load Diff
41
tp2/sujet/Makefile
Normal file
41
tp2/sujet/Makefile
Normal file
@@ -0,0 +1,41 @@
|
||||
# CC=gcc or clang or gcc-14 (on OSX)
|
||||
CC=gcc
|
||||
|
||||
# Math, big numbers and SDL2 fonts
|
||||
# LDLIBS=
|
||||
|
||||
# clang 14.x.x on Darwin does implement the C23 feature __VA_OPTS__ but issues
|
||||
# a spurious warning. The -Wno-gnu-zero-variadic-macro-arguments disables it.
|
||||
# This flag is ignored by gcc (which implements __VA_OPTS__ without any warning).
|
||||
override CFLAGS += -std=gnu2x -MMD -Wall -pedantic -Wextra -Wshadow -Wpointer-arith \
|
||||
-Wcast-qual -Wstrict-prototypes # -Wno-gnu-zero-variadic-macro-arguments
|
||||
|
||||
# For MacOS (assuming recent homebrew)
|
||||
ifeq ($(shell uname -s), Darwin)
|
||||
CPPFLAGS+=-I/opt/homebrew/include
|
||||
LDFLAGS+=-L/opt/homebrew/lib
|
||||
endif
|
||||
|
||||
SOURCES := $(wildcard *.c)
|
||||
OBJECTS := $(SOURCES:%.c=%.o)
|
||||
DEPS := $(SOURCES:%.c=%.d)
|
||||
|
||||
# Compilation in debug mode by default, to use gdb and valgrind.
|
||||
all: CFLAGS += -g -O0 -Werror -Wno-unused-parameter
|
||||
all: mysort
|
||||
|
||||
# Once the program works, optimized mode (and no error in case of warning).
|
||||
nowerror: CFLAGS += -O3
|
||||
nowerror: mysort
|
||||
|
||||
# Add parser.o scan.o if bison/flex interface.
|
||||
mysort: $(OBJECTS)
|
||||
$(CC) -o $@ $(LDFLAGS) $(CFLAGS) $^ $(LDLIBS)
|
||||
|
||||
# Include dependancies generated by gcc -MMD.
|
||||
-include $(DEPS)
|
||||
|
||||
# Clean all.
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f mysort *.o *.d TAGS core
|
||||
20
tp2/sujet/README.md
Normal file
20
tp2/sujet/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
# Compilation des fichiers.
|
||||
---
|
||||
Le fichier `Makefile` permet de recompiler les fichiers en tenant compte des changements depuis la dernière compilation.
|
||||
|
||||
+ Pour compiler, taper `make`.
|
||||
|
||||
+ Si la compilation réussit, l'exécutable produit s'appelle `mysort`.
|
||||
|
||||
+ On peut alors le lancer par `./mysort`.
|
||||
|
||||
+ La compilation par défaut,
|
||||
- ne vérifie pas que les arguments des fonctions sont bien utilisés, car les fonctions sont vides au départ. Cela produirait donc de trop nombreux avertissements.
|
||||
- produit une erreur en cas d'avertissement, et ne crée pas l'exécutable dans ce cas.
|
||||
|
||||
+ Pour vérifier que tous les arguments des fonctions sont utilisés, taper `make nowerror`.
|
||||
|
||||
+ Dans ce dernier cas, l'exécutable est créé même s'il y a des avertissements.
|
||||
|
||||
---
|
||||
8
tp2/sujet/array_char.txt
Normal file
8
tp2/sujet/array_char.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
104
|
||||
|
||||
char
|
||||
|
||||
Q W E R T Y U I O P A S D F G H J K L Z X C V B N M
|
||||
q w e r t y u i o p a s d f g h j k l z x c v b n m
|
||||
Q W E R T Y U I O P A S D F G H J K L Z X C V B N M
|
||||
q w e r t y u i o p a s d f g h j k l z x c v b n m
|
||||
5
tp2/sujet/array_double.txt
Normal file
5
tp2/sujet/array_double.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
100
|
||||
|
||||
double
|
||||
|
||||
79.39 39.82 82.9 9.5 5.85 85.7 7.64 64.20 20.22 22.97 97.37 37.38 38.2 2.55 55.51 51.45 45.68 68.24 24.6 6.23 23.18 18.66 66.50 50.62 62.93 93.1 1.94 94.28 28.10 10.96 96.19 19.91 91.3 3.58 58.99 99.59 59.73 73.42 42.57 57.17 17.81 81.53 53.8 8.61 61.98 98.30 30.67 67.69 69.15 15.92 92.87 87.40 40.88 88.76 76.49 49.32 32.65 65.54 54.27 27.60 60.83 83.35 35.12 12.16 16.75 75.89 89.29 29.80 80.70 70.100 100.52 52.25 25.44 44.14 14.84 84.13 13.26 26.34 34.4 4.31 31.74 74.36 36.47 47.77 77.21 21.43 43.72 72.86 86.11 11.71 71.63 63.46 46.41 41.48 48.95 95.33 33.56 56.78 78.90 90.79
|
||||
5
tp2/sujet/array_int.txt
Normal file
5
tp2/sujet/array_int.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
100
|
||||
|
||||
int
|
||||
|
||||
79 39 82 9 5 85 7 64 20 22 97 37 38 2 55 51 45 68 24 6 23 18 66 50 62 93 1 94 28 10 96 19 91 3 58 99 59 73 42 57 17 81 53 8 61 98 30 67 69 15 92 87 40 88 76 49 32 65 54 27 60 83 35 12 16 75 89 29 80 70 100 52 25 44 14 84 13 26 34 4 31 74 36 47 77 21 43 72 86 11 71 63 46 41 48 95 33 56 78 90
|
||||
5
tp2/sujet/array_pointx.txt
Normal file
5
tp2/sujet/array_pointx.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
100
|
||||
|
||||
pointx
|
||||
|
||||
(9.9, 7.7) (5.5, 1.1) (7.7, 2.2) (0.0, 1.1) (4.4, 7.7) (8.8, 9.9) (5.5, 5.5) (9.9, 0.0) (6.6, 6.6) (6.6, 5.5) (0.0, 3.3) (1.1, 3.3) (5.5, 7.7) (0.0, 4.4) (3.3, 7.7) (7.7, 5.5) (3.3, 0.0) (7.7, 4.4) (5.5, 4.4) (9.9, 4.4) (3.3, 4.4) (2.2, 0.0) (5.5, 0.0) (5.5, 6.6) (9.9, 9.9) (8.8, 1.1) (7.7, 7.7) (1.1, 1.1) (4.4, 2.2) (8.8, 3.3) (2.2, 2.2) (8.8, 6.6) (9.9, 6.6) (6.6, 3.3) (6.6, 4.4) (5.5, 9.9) (8.8, 7.7) (6.6, 7.7) (5.5, 3.3) (5.5, 9.9) (2.2, 4.4) (2.2, 3.3) (4.4, 0.0) (0.0, 0.0) (4.4, 6.6) (6.6, 9.9) (8.8, 0.0) (9.9, 3.3) (7.7, 9.9) (7.7, 3.3) (7.7, 0.0) (2.2, 9.9) (3.3, 9.9) (5.5, 2.2) (3.3, 2.2) (8.8, 5.5) (1.1, 0.0) (0.0, 7.7) (7.7, 1.1) (6.6, 1.1) (2.2, 1.1) (3.3, 1.1) (0.0, 6.6) (1.1, 9.9) (7.7, 6.6) (7.7, 9.9) (1.1, 4.4) (0.0, 5.5) (2.2, 5.5) (8.8, 4.4) (3.3, 3.3) (1.1, 5.5) (9.9, 5.5) (9.9, 9.9) (9.9, 2.2) (4.4, 9.9) (6.6, 0.0) (8.8, 2.2) (4.4, 3.3) (4.4, 5.5) (3.3, 5.5) (2.2, 7.7) (4.4, 1.1) (6.6, 2.2) (0.0, 2.2) (1.1, 7.7) (0.0, 9.9) (1.1, 6.6) (8.8, 9.9) (0.0, 9.9) (9.9, 1.1) (4.4, 4.4) (3.3, 6.6) (2.2, 6.6) (1.1, 2.2) (3.3, 9.9) (4.4, 9.9) (6.6, 9.9) (2.2, 9.9) (1.1, 9.9)
|
||||
5
tp2/sujet/array_pointy.txt
Normal file
5
tp2/sujet/array_pointy.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
100
|
||||
|
||||
pointy
|
||||
|
||||
(7.7, 0.0) (9.9, 5.5) (9.9, 9.9) (8.8, 3.3) (5.5, 2.2) (0.0, 5.5) (0.0, 6.6) (3.3, 1.1) (2.2, 9.9) (4.4, 9.9) (6.6, 6.6) (4.4, 7.7) (7.7, 6.6) (1.1, 5.5) (9.9, 6.6) (2.2, 3.3) (6.6, 1.1) (8.8, 9.9) (4.4, 3.3) (3.3, 7.7) (4.4, 4.4) (6.6, 5.5) (3.3, 9.9) (6.6, 4.4) (6.6, 9.9) (9.9, 4.4) (1.1, 0.0) (8.8, 4.4) (0.0, 1.1) (8.8, 2.2) (3.3, 3.3) (5.5, 9.9) (1.1, 7.7) (7.7, 2.2) (5.5, 3.3) (5.5, 1.1) (0.0, 9.9) (2.2, 7.7) (5.5, 0.0) (1.1, 3.3) (9.9, 2.2) (3.3, 2.2) (0.0, 0.0) (9.9, 9.9) (0.0, 7.7) (7.7, 9.9) (9.9, 3.3) (4.4, 1.1) (1.1, 4.4) (2.2, 9.9) (3.3, 4.4) (7.7, 3.3) (1.1, 1.1) (4.4, 9.9) (1.1, 9.9) (4.4, 0.0) (2.2, 1.1) (0.0, 3.3) (8.8, 5.5) (5.5, 9.9) (0.0, 2.2) (0.0, 4.4) (1.1, 6.6) (5.5, 4.4) (4.4, 2.2) (3.3, 5.5) (8.8, 7.7) (2.2, 5.5) (8.8, 9.9) (5.5, 6.6) (8.8, 6.6) (1.1, 2.2) (2.2, 0.0) (9.9, 0.0) (4.4, 5.5) (6.6, 3.3) (7.7, 1.1) (7.7, 9.9) (5.5, 5.5) (6.6, 0.0) (2.2, 6.6) (0.0, 9.9) (3.3, 9.9) (8.8, 1.1) (3.3, 6.6) (6.6, 2.2) (4.4, 6.6) (6.6, 9.9) (7.7, 4.4) (2.2, 4.4) (3.3, 0.0) (5.5, 7.7) (9.9, 1.1) (6.6, 7.7) (1.1, 9.9) (7.7, 5.5) (8.8, 0.0) (2.2, 2.2) (9.9, 7.7) (7.7, 7.7)
|
||||
18
tp2/sujet/array_pt.txt
Normal file
18
tp2/sujet/array_pt.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
100
|
||||
|
||||
pointx
|
||||
|
||||
|
||||
|
||||
(0.0,0.0) (1.1,0.0) (2.2,0.0) (3.3,0.0) (4.4,0.0) (5.5,0.0) (6.6,0.0) (7.7,0.0) (8.8,0.0) (9.9,0.0)
|
||||
(0.0,1.1) (1.1,1.1) (2.2,1.1) (3.3,1.1) (4.4,1.1) (5.5,1.1) (6.6,1.1) (7.7,1.1) (8.8,1.1) (9.9,1.1)
|
||||
(0.0,2.2) (1.1,2.2) (2.2,2.2) (3.3,2.2) (4.4,2.2) (5.5,2.2) (6.6,2.2) (7.7,2.2) (8.8,2.2) (9.9,2.2)
|
||||
(0.0,3.3) (1.1,3.3) (2.2,3.3) (3.3,3.3) (4.4,3.3) (5.5,3.3) (6.6,3.3) (7.7,3.3) (8.8,3.3) (9.9,3.3)
|
||||
(0.0,4.4) (1.1,4.4) (2.2,4.4) (3.3,4.4) (4.4,4.4) (5.5,4.4) (6.6,4.4) (7.7,4.4) (8.8,4.4) (9.9,4.4)
|
||||
(0.0,5.5) (1.1,5.5) (2.2,5.5) (3.3,5.5) (4.4,5.5) (5.5,5.5) (6.6,5.5) (7.7,5.5) (8.8,5.5) (9.9,5.5)
|
||||
(0.0,6.6) (1.1,6.6) (2.2,6.6) (3.3,6.6) (4.4,6.6) (5.5,6.6) (6.6,6.6) (7.7,6.6) (8.8,6.6) (9.9,6.6)
|
||||
(0.0,7.7) (1.1,7.7) (2.2,7.7) (3.3,7.7) (4.4,7.7) (5.5,7.7) (6.6,7.7) (7.7,7.7) (8.8,7.7) (9.9,7.7)
|
||||
(0.0,9.9) (1.1,9.9) (2.2,9.9) (3.3,9.9) (4.4,9.9) (5.5,9.9) (6.6,9.9) (7.7,9.9) (8.8,9.9) (9.9,9.9)
|
||||
(0.0,9.9) (1.1,9.9) (2.2,9.9) (3.3,9.9) (4.4,9.9) (5.5,9.9) (6.6,9.9) (7.7,9.9) (8.8,9.9) (9.9,9.9)
|
||||
|
||||
(6.6, 9.9) (7.7, 9.9) (3.3, 7.7) (2.2, 9.9) (5.5, 9.9) (7.7, 0.0) (6.6, 1.1) (5.5, 9.9) (4.4, 9.9) (3.3, 5.5) (3.3, 6.6) (2.2, 9.9) (7.7, 4.4) (9.9, 9.9) (7.7, 9.9) (6.6, 3.3) (7.7, 7.7) (4.4, 4.4) (8.8, 9.9) (7.7, 1.1) (7.7, 2.2) (1.1, 9.9) (0.0, 9.9) (6.6, 5.5) (3.3, 3.3) (9.9, 4.4) (9.9, 6.6) (0.0, 1.1) (9.9, 7.7) (7.7, 5.5) (6.6, 2.2) (1.1, 2.2) (0.0, 2.2) (6.6, 6.6) (9.9, 2.2) (5.5, 7.7) (8.8, 5.5) (8.8, 2.2) (9.9, 3.3) (1.1, 6.6) (0.0, 4.4) (5.5, 1.1) (1.1, 1.1) (3.3, 0.0) (8.8, 1.1) (4.4, 0.0) (0.0, 3.3) (1.1, 0.0) (6.6, 7.7) (5.5, 3.3) (5.5, 4.4) (5.5, 0.0) (2.2, 3.3) (8.8, 9.9) (1.1, 7.7) (2.2, 5.5) (1.1, 4.4) (6.6, 0.0) (9.9, 9.9) (5.5, 2.2) (9.9, 1.1) (4.4, 5.5) (8.8, 6.6) (2.2, 7.7) (3.3, 9.9) (7.7, 6.6) (8.8, 3.3) (5.5, 5.5) (2.2, 6.6) (9.9, 0.0) (1.1, 5.5) (0.0, 0.0) (4.4, 6.6) (6.6, 4.4) (5.5, 6.6) (1.1, 3.3) (2.2, 1.1) (6.6, 9.9) (4.4, 2.2) (3.3, 1.1) (9.9, 5.5) (3.3, 4.4) (4.4, 3.3) (3.3, 2.2) (2.2, 4.4) (8.8, 0.0) (8.8, 7.7) (1.1, 9.9) (2.2, 0.0) (0.0, 5.5) (2.2, 2.2) (0.0, 7.7) (7.7, 3.3) (8.8, 4.4) (4.4, 9.9) (4.4, 1.1) (0.0, 9.9) (3.3, 9.9) (4.4, 7.7) (0.0, 6.6)
|
||||
5
tp2/sujet/array_string.txt
Normal file
5
tp2/sujet/array_string.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
100
|
||||
|
||||
string
|
||||
|
||||
yfzg aunzfm nbqs mklksz xbrof iech hjjujoj dkmxj jui vpbwjrc bijluq dmf vzwilvr foujj mgu kb ukrocms tnktkug lsmz xlzltw dxxxh nlchzwv rffxor d hcd oyf bcb l qwuxx sjjzqjo ctckv tjga jc m h ne kyu kvmtj xczjyj jrcxa noxws cu bqull gxg zfnzor fwiiev cur wn xdmiow lcunkj yfzg aunzfm nbqs mklksz xbrof iech hjjujoj dkmxj jui vpbwjrc bijluq dmf vzwilvr foujj mgu kb ukrocms tnktkug lsmz xlzltw dxxxh nlchzwv rffxor d hcd oyf bcb l qwuxx sjjzqjo ctckv tjga jc m h ne kyu kvmtj xczjyj jrcxa noxws cu bqull gxg zfnzor fwiiev cur wn xdmiow lcunkj
|
||||
5
tp2/sujet/array_string2.txt
Normal file
5
tp2/sujet/array_string2.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
100
|
||||
|
||||
string2
|
||||
|
||||
yfzg aunzfm nbqs mklksz xbrof iech hjjujoj dkmxj jui vpbwjrc bijluq dmf vzwilvr foujj mgu kb ukrocms tnktkug lsmz xlzltw dxxxh nlchzwv rffxor d hcd oyf bcb l qwuxx sjjzqjo ctckv tjga jc m h ne kyu kvmtj xczjyj jrcxa noxws cu bqull gxg zfnzor fwiiev cur wn xdmiow lcunkj yfzg aunzfm nbqs mklksz xbrof iech hjjujoj dkmxj jui vpbwjrc bijluq dmf vzwilvr foujj mgu kb ukrocms tnktkug lsmz xlzltw dxxxh nlchzwv rffxor d hcd oyf bcb l qwuxx sjjzqjo ctckv tjga jc m h ne kyu kvmtj xczjyj jrcxa noxws cu bqull gxg zfnzor fwiiev cur wn xdmiow lcunkj
|
||||
66
tp2/sujet/compare.c
Normal file
66
tp2/sujet/compare.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "compare.h"
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
int fcmp_int(const void *x, const void *y) {
|
||||
if (*(const int *)x < *(const int *)y) {
|
||||
return -1;
|
||||
} else if (*(const int *)x > *(const int *)y) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int fcmp_reverse_int(const void *x, const void *y) {
|
||||
return fcmp_int(y, x);
|
||||
}
|
||||
|
||||
int fcmp_char(const void *x, const void *y) {
|
||||
if(*(const char *)x < *(const char *)y) {
|
||||
return -1;
|
||||
} else if(*(const char *)x > *(const char *)y) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int fcmp_double(const void *x, const void *y) {
|
||||
if (*(const double *)x < *(const double *)y) {
|
||||
return -1;
|
||||
} else if (*(const double *)x > *(const double *)y) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int fcmp_string(const void *x, const void *y) {
|
||||
int r = fcmp_char(x, y);
|
||||
if(r != 0) return r;
|
||||
return fcmp_char((const char *)x+1, (const char *)y+1);
|
||||
}
|
||||
|
||||
int fcmp_string_hiera(const void *x, const void *y) {
|
||||
if (strlen(*(const string *)x) < strlen(*(const string *)y)) {
|
||||
return -1;
|
||||
} else if (strlen(*(const string *)x) > strlen(*(const string *)y)) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int fcmp_pointx(const void *p, const void *q) {
|
||||
const point *pp = p;
|
||||
const point *qq = p;
|
||||
return fcmp_double((const void *)&(pp->x), (const void *)&(qq->x));
|
||||
}
|
||||
|
||||
int fcmp_pointy(const void *p, const void *q) {
|
||||
const point *pp = p;
|
||||
const point *qq = p;
|
||||
return fcmp_double((const void *)&(pp->y), (const void *)&(qq->y));
|
||||
}
|
||||
1
tp2/sujet/compare.d
Normal file
1
tp2/sujet/compare.d
Normal file
@@ -0,0 +1 @@
|
||||
compare.o: compare.c compare.h
|
||||
42
tp2/sujet/compare.h
Normal file
42
tp2/sujet/compare.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef _COMPARE_H
|
||||
#define _COMPARE_H
|
||||
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// réel aléatoire dans [0,1]
|
||||
#define RAND01 ((double)random() / RAND_MAX)
|
||||
#define RANDINT(n) (random() % (n))
|
||||
|
||||
/* compare.c */
|
||||
int fcmp_int(const void *x, const void *y);
|
||||
int fcmp_reverse_int(const void *x, const void *y);
|
||||
int fcmp_char(const void *x, const void *y);
|
||||
int fcmp_double(const void *x, const void *y);
|
||||
int fcmp_string(const void *x, const void *y);
|
||||
int fcmp_string_hiera(const void *x, const void *y);
|
||||
int fcmp_pointx(const void *p, const void *q);
|
||||
int fcmp_pointy(const void *p, const void *q);
|
||||
|
||||
// taille max d'une string
|
||||
#define MAXSTR 7
|
||||
|
||||
// type fonction de comparaison
|
||||
typedef int (*fcmp)(const void *, const void *);
|
||||
|
||||
// type chaîne de caractères
|
||||
typedef char *string;
|
||||
|
||||
typedef struct {
|
||||
double x, y;
|
||||
} point;
|
||||
|
||||
#endif
|
||||
BIN
tp2/sujet/compare.o
Normal file
BIN
tp2/sujet/compare.o
Normal file
Binary file not shown.
39
tp2/sujet/genprint.c
Normal file
39
tp2/sujet/genprint.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "genprint.h"
|
||||
|
||||
#define xstr(s) str(s) // permet l'expansion d'une macro
|
||||
#define str(s) #s // Ex: scanf("%"xstr(BLABLA)"s",buffer);
|
||||
|
||||
// Initialisation aléatoire de l'élément T[i]
|
||||
void init(type_t t, void *T, int i) {
|
||||
if (T == NULL) {
|
||||
// À compléter
|
||||
}
|
||||
|
||||
// switch (t) {}
|
||||
}
|
||||
|
||||
// Affiche T[i] avec le bon format suivant le type t.
|
||||
void print(type_t t, const void *T, int i) {
|
||||
if (T == NULL) {
|
||||
// À compléter
|
||||
}
|
||||
|
||||
// switch (t) {}
|
||||
}
|
||||
|
||||
// Affiche le contenu du tableau T d'éléments de type t.
|
||||
void print_array(const void *T, type_t t, int nb) {
|
||||
if (T == NULL) {
|
||||
// À compléter
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
// Lit un tableau d'éléments depuis un flux.
|
||||
void *read_array(FILE *f, type_t *t, int *nb) {
|
||||
if (f == NULL) {
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
1
tp2/sujet/genprint.d
Normal file
1
tp2/sujet/genprint.d
Normal file
@@ -0,0 +1 @@
|
||||
genprint.o: genprint.c genprint.h compare.h
|
||||
57
tp2/sujet/genprint.h
Normal file
57
tp2/sujet/genprint.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#ifndef GENPRINT_H_
|
||||
#define GENPRINT_H_
|
||||
|
||||
#include "compare.h"
|
||||
|
||||
typedef enum type_t { // pour les switch()
|
||||
INT,
|
||||
INT2,
|
||||
CHAR,
|
||||
DOUBLE,
|
||||
STRING,
|
||||
STRING2,
|
||||
POINTX,
|
||||
POINTY,
|
||||
} type_t;
|
||||
|
||||
#define NUMTYPES (POINTY + 1)
|
||||
#define MAX_TYPE_NAME_LEN 8
|
||||
|
||||
// Initialisation aléatoire de l'élément T[i].
|
||||
//
|
||||
// Les entiers sont aléatoires dans [0,100[.
|
||||
// Les caractères sont des lettres majuscules aléatoires.
|
||||
// Les doubles sont aléatoires dans [-1,+1].
|
||||
// Les chaînes sont d'au plus MAXSTR minuscules aléatoires.
|
||||
// Les points sont aléatoires dans [0,10[ x [0,10[.
|
||||
void init(type_t t, void *T, int i);
|
||||
|
||||
// Affiche T[i] avec le bon format suivant le type t.
|
||||
//
|
||||
// Les entiers sont affichés sur 2 chiffres, sans signe si positifs.
|
||||
// Les doubles le sont avec leur signe et 2 chiffres après la virgule.
|
||||
// Les points le sont comme (5.2,3.1) avec 1 chiffre après la virgule.
|
||||
void print(type_t t, const void *T, int i);
|
||||
|
||||
// Affiche le contenu du tableau T d'éléments de type t de nb éléments.
|
||||
void print_array(const void *T, type_t t, int nb);
|
||||
|
||||
// Lit un tableau d'éléments depuis un flux.
|
||||
// - Le flux f doit être ouvert en lecture.
|
||||
//
|
||||
// En cas de succès :
|
||||
// - Le type des éléments est indiqué dans t.
|
||||
// - Le nombre d'éléments est indiqué dans nb.
|
||||
// - La fonction renvoie le tableau alloué dynamiquement.
|
||||
//
|
||||
// En cas d'erreur, NULL est retourné et le tableau n'est pas alloué.
|
||||
//
|
||||
// Le format du fichier est le suivant :
|
||||
// - une ligne avec le nombre d'éléments du tableau.
|
||||
// - une ligne avec le type des éléments du tableau
|
||||
// (int, int2, char, double, string, string2, pointx ou pointy).
|
||||
// - les éléments du tableau, séparés par des espaces.
|
||||
// Des fichiers d'exemple sont fournis (array*.txt).
|
||||
void *read_array(FILE *f, type_t *t, int *nb);
|
||||
|
||||
#endif // GENPRINT_H_
|
||||
BIN
tp2/sujet/genprint.o
Normal file
BIN
tp2/sujet/genprint.o
Normal file
Binary file not shown.
135
tp2/sujet/main.c
Normal file
135
tp2/sujet/main.c
Normal file
@@ -0,0 +1,135 @@
|
||||
#include "compare.h"
|
||||
#include "genprint.h"
|
||||
|
||||
#define xstr(s) str(s) // permet l'expansion d'une macro
|
||||
#define str(s) #s // Ex: scanf("%"xstr(DMAX)"s",buffer);
|
||||
|
||||
// Tableau des tailles des types (ne pas modifier)
|
||||
int psize[] = {
|
||||
2, 2, 1, 5, MAXSTR, 9, 9,
|
||||
};
|
||||
|
||||
// Tableau des fonctions de comparaison (ne pas modifier)
|
||||
fcmp cmp[] = {
|
||||
fcmp_int, fcmp_reverse_int, fcmp_char, fcmp_double,
|
||||
fcmp_string, fcmp_string_hiera, fcmp_pointx, fcmp_pointy,
|
||||
};
|
||||
|
||||
// Tableau des types pour l'aide (ne pas modifier)
|
||||
// clang-format off
|
||||
char *type[] = {
|
||||
"int", "Integers in [0,100[ (sort: in usual order).",
|
||||
"int2", "Integers in [0,100[ (sort: in reverse order).",
|
||||
"char", "Capital letters in ['A','Z'].",
|
||||
"double", "Real numbers in ]-1.00,+1.00[.",
|
||||
"string", "Strings of length ≤ " xstr(MAXSTR) " (sort: lexicographic).",
|
||||
"string2", "Strings of length ≤ " xstr(MAXSTR) " (sort by length, lexico if same length).",
|
||||
"pointx", "Real points of [0,9.9]×[0,9.9] with 'x'-like fcmp().",
|
||||
"pointy", "Real points of [0,9.9]×[0,9.9] with 'y'-like fcmp().",
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
// Tableau des tailles des types (ne pas modifier)
|
||||
int size[] = {
|
||||
sizeof(int), sizeof(int), sizeof(char), sizeof(double),
|
||||
sizeof(string), sizeof(string), sizeof(point), sizeof(point),
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
type_t t;
|
||||
|
||||
// nb_values = nombre d'éléments à générer.
|
||||
int nb_values = (argc >= 2) ? atoi(argv[1]) : -1;
|
||||
const char *s = (argc >= 3) ? argv[2] : type[INT];
|
||||
|
||||
for (t = 0; t < NUMTYPES; t++)
|
||||
if (!strcmp(s, type[2 * t]))
|
||||
break; // type trouvé !
|
||||
|
||||
if (nb_values < 0 || t == NUMTYPES) {
|
||||
// nombre d'éléments ou type incorrect
|
||||
fprintf(stderr, "\n Usage: %s nb_values [t]", argv[0]);
|
||||
fprintf(stderr, "\n Ex.: %s 42 int\n\n", argv[0]);
|
||||
fprintf(stderr, " nb_values = number of random elements\n");
|
||||
fprintf(stderr, " t = type of elements & fcmp():\n\n");
|
||||
for (t = 0; t < NUMTYPES; t++) {
|
||||
fprintf(stderr, "%8s ", type[2 * t]);
|
||||
fprintf(stderr, " %s\n", type[2 * t + 1]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
unsigned seed = time(NULL) % 1000;
|
||||
srandom(seed);
|
||||
printf("\nSeed: %u\n\n", seed); // pour rejouer la même chose au cas où
|
||||
|
||||
// Tableau initial
|
||||
void *T = malloc(nb_values * size[t]);
|
||||
|
||||
printf("Array of %i elements of type '%s'\n", nb_values, type[2 * t]);
|
||||
printf("%s\n", type[2 * t + 1]);
|
||||
|
||||
// Affichage et création de T[]
|
||||
printf("\nInput array:\n\n");
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
// COMPLÉTER À PARTIR D'ICI.
|
||||
|
||||
// 1. Initialiser chaque élément du tableau en utilisant init().
|
||||
|
||||
// 2. Afficher le tableau en utilisant print_array().
|
||||
|
||||
// 3. Trier ici le tableau.
|
||||
// qsort(...); // ou autre tri, comme mergesort ou heapsort.
|
||||
|
||||
printf("Array sorted:\n\n");
|
||||
|
||||
// 4. Afficher à nouveau le tableau (trié) en utilisant print_array().
|
||||
print_array(T, t, nb_values);
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
// À partir d'ici, tests de lecture des tableaux depuis des fichiers.
|
||||
|
||||
printf("Array of integers from array_int.txt\n\n");
|
||||
// 5. Ouvrir le fichier array_int.txt en lecture.
|
||||
// 6. Lire le tableau d'entiers depuis le flux obtenu avec read_array().
|
||||
// Par exemple : T = read_array(...);
|
||||
// 7. Afficher T.
|
||||
// 8. Trier T.
|
||||
// 9. Afficher T.
|
||||
// 10. Libérer la mémoire allouée pour T
|
||||
// 11. Fermer le flux f.
|
||||
printf("\n\n");
|
||||
|
||||
// Faire de même pour les autres types.
|
||||
printf("Array of characters from array_char.txt\n\n");
|
||||
printf("\n\n");
|
||||
|
||||
printf("Array of doubles from array_double.txt\n\n");
|
||||
printf("\n\n");
|
||||
|
||||
// Attention aux types chaînes de caractères, plus délicats à gérer.
|
||||
// En effet, il faut allouer de la mémoire pour chaque chaîne.
|
||||
printf("Array of strings from array_string.txt\n\n");
|
||||
printf("\n\n");
|
||||
|
||||
printf("Array of strings from array_string2.txt\n\n");
|
||||
printf("\n\n");
|
||||
|
||||
printf("Array of points from array_pointx.txt\n\n");
|
||||
printf("\n\n");
|
||||
|
||||
printf("Array of points from array_pointy.txt\n\n");
|
||||
printf("\n\n");
|
||||
|
||||
// Vous pouvez ajouter du code pour trier successivement les tableaux de
|
||||
// points suivant les deux critères x et y. Testez plusieurs fonctions de
|
||||
// tri. Que remarquez-vous ?
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
1
tp2/sujet/main.d
Normal file
1
tp2/sujet/main.d
Normal file
@@ -0,0 +1 @@
|
||||
main.o: main.c compare.h genprint.h
|
||||
BIN
tp2/sujet/main.o
Normal file
BIN
tp2/sujet/main.o
Normal file
Binary file not shown.
BIN
tp2/sujet/mysort
Executable file
BIN
tp2/sujet/mysort
Executable file
Binary file not shown.
Reference in New Issue
Block a user