Jean-Jacques BOURDIN
Premier fichier : Header
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <float.h> #include <time.h> typedef struct valeurs { int numero; float coef; float obj; float val; } valeurs_t; typedef struct cell * liste_t; typedef struct cell { int numero; float coef; float obj; float val; struct cell * suiv; } cell_t; |
Quelques fonctions utiles
valeurs_t car (liste_t l){ valeurs_t p; p.numero = INT_MIN; if (l) { p.numero = l-> numero; p.coef = l-> coef; p.val = l-> val; p.obj = l-> obj; } return p; } liste_t cdr (liste_t l){ if (l) { return (* l).suiv; } else { return (liste_t) 0; } } /* afficher tous les elements iterativement*/ void affl (liste_t l) { while (l) { printf("%3d => %5.2f %3.2f \t%6.2f\n",l->numero, l->coef, l->obj, l->val); l = cdr(l); } printf("\n"); } void affl2 (char * texte, liste_t l) { printf("%s l vaut %lld\n", texte, ((long long int)l) % 10000); while (l) { printf("%3d => %5.2f %3.2f \t%6.2f\n",l->numero, l->coef, l->obj, l->val); l = cdr(l); } printf("\n"); } liste_t cons (int n, float x, float y, float z, liste_t l){ liste_t newl; newl=(liste_t)malloc(sizeof (cell_t)); assert(newl); (* newl).numero = n; (* newl).obj = x; (* newl).coef = y; (* newl).val = z; (* newl).suiv = l; return newl; } liste_t creerliste (int n) { liste_t new; int num; float a, b, c, coef, value; a = 8.0; b = 5.0; coef = 2.1; num = 10; value = FLT_MIN; new = (liste_t) 0; while (n) { c = (float) ((int) (a + b) % 27); new = cons (num, c, coef, value, new); b = a; a = c; num++; if (num > 17) { num -= 13; coef = coef + 0.1; } n--; } return new; } |
La fonction main
int main () { liste_t lt, lt2, ll, new; int nb; clock_t dep, arr; nb = -1; printf("Combien de nombres pour votre liste ?\n"); while (nb < 0) scanf("%d", &nb); if (nb > 1000) nb = 30; lt = creerliste(nb); affl(lt); ajouteval(<); affl2("Valeurs comptees",lt); lt2 = rev(lt); affl2("renversant",lt2); ll = trii(lt2); affl2("triee par insertion", ll); lt2 = creerliste(nb); ajouteval(<2); ll = qs(lt2); affl2("quicksort : ", ll); } |
Liste des exercices à faire
Attention dans ces exercices il n‘y a jamais à utiliser malloc (ni cons).