Jean-Jacques BOURDIN
Hereafter is a function building a list of n numbers. It's written in scheme, you'll have to translate it.
Voici une fonction qui construit une liste de n nombres entiers. Elle est en Lisp, il faut donc la transcrire.
(define (creerl n)
(if (< n 1)
‘(0)
(cons n (creerl (- n 1)))))
|
typedef struct cell * liste_t;
typedef struct cell {
float obj;
struct cell * suiv;
} cell_t;
|
Here you see that a list is only the adress of a structure.
Où on voit que la liste est en fait uniquement l'adresse d'une structure qui contient une valeur et une liste.
int estvide (liste_t l) {
if (l == (liste_t) 0) {
return 1;
}
return 0;
}
int estnonvide (liste_t l) {
if (l == (liste_t) 0) {
return 0;
}
return 1;
}
|
float soml (liste_t l) {
if (estvide(l)) {
return 0.0;
}
return (*l).obj + soml((*l).suiv);
}
|
int howmany (liste_t l) {
if (estvide(l)) {
return 0;
}
return 1 + howmany ((*l).suiv);
}
|
liste_t cons (int nb, liste_t l) {
liste_t new;
new = (liste_t) malloc (sizeof(cell_t));
assert(new);
(*new).obj = (float) nb;
(*new).suiv = l;
return new;
}
liste_t creer (int nb) {
liste_t debut;
int i, j, k, t;
j = 13;
k = 21;
debut = (liste_t) 0;
for (i = 0; i < nb; i++) {
debut = cons (j, debut);
t = (j + k) % 31;
k = j;
j = t;
}
return debut;
}
|
void affl (liste_t l) {
if (estvide (l)) {
printf("\n");
}
else {
printf("%5.2f ", (*l).obj);
affl ((*l).suiv);
}
}
|
void affll (liste_t l) {
if (estvide (l)) {
printf("\n");
}
else {
printf("%5.2f ", l->obj);
affll (l->suiv);
}
}
void affw (liste_t l) {
while (estnonvide (l)) {
printf("%5.2f ", l->obj);
l = l->suiv;
}
printf("\n");
}
|
#include<stdio.h> #include<stdlib.h> #include<assert.h> |
int main () {
liste_t l;
int nb;
float s;
nb = 10;
l = creer(nb);
affw(l);
s = soml(l);
printf("Total : %f\n", s);
s = howmany(l);
printf("Il y a %d elements\n", s);
}
|
int plusgrandliste (liste_t l, int candidat) {
if (l) {
if (l->nb > candidat)
return plusgrandliste (l->next, l->nb);
return plusgrandliste (l->next, candidat);
}
return candidat; /* il n‘y a plus d‘autre possible*/
}
int plusgrandl (liste_t l) {
if (l)
return plusgrandliste (l->next, l->nb);
return INT_MIN; /* impose d‘avoir inclus limits.h */
}
|
float plusgrandliter (liste_t l) {
float plgd;
liste_t crt, ptrplgd;
if (! l)
return INT_MIN; /* il faut avoir inclus limits.h */
ptrplgd = l;
plgd = ptrplgd->obj;
crt = l->suiv;
while (crt) {
if (crt->obj > plgd) {
plgd = crt->obj;
ptrplgd = suiv;
}
crt = crt->nxt;
}
return plgd;
}
|
(define (re l)
(ifn (cdr l) l
(cons (car (re (cdr l)))
(re (cons (car l)
(re (cdr (re (cdr l)))))))))
|
Quelques solutions vues en cours
float moyl (liste_t l) {
float val;
int nb;
liste_t crt;
if (! l)
return 0.0;
val = 0.0;
nb = 0;
crt = l;
while (crt) {
val += car(crt);
crt = cdr(crt);
nb++;
}
return val/nb;
}
|
liste_t creel (int v) {
float n;
liste_t crt;
n = 0.0;
crt = (liste_t) 0;
while(v--) {
crt = cons(n, crt);
n = n + 1.0:
}
return crt;
}
|
"La beauté des arbres est de croître." (qui ?)
Nous présentons une structure pour des arbres binaires.
We start by defining some structures.
/* Arbres binaires tout simplement */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
typedef struct noeud {
int num;
float coef;
float unit;
float total;
struct noeud * fg;
struct noeud * fd;
} noeud_t;
typedef struct noeud * arbre;
|
arbre ajout (arbre a, int v, float x, float y) {
arbre new;
int rande;
if (! a) {
new = (arbre) malloc (sizeof (noeud_t));
assert(new);
new->num = v;
new->coef = y;
new->unit = x;
new->fg = (arbre) 0;
new->fd = (arbre) 0;
return new;
}
rande = rand ();
/*
printf(" insere %d en %d avec %2d\n", v, a->num, rande %100);
*/
if (rande % 2) {
/* on le met à gauche*/
if (! (a->fg)) {
/* s‘il ny a pas de fils gauche */
a->fg = (arbre) malloc (sizeof (noeud_t));
assert(a->fg);
a->fg->num = v;
a->fg->coef = y;
a->fg->unit = x;
a->fg->fg = (arbre) 0;
a->fg->fd = (arbre) 0;
return a;
}
a->fg = ajout(a->fg, v, x, y);
return a;
}
/* on le met donc a droite*/
if (! (a->fd)) {
/* s‘il ny a pas de fils droit */
a->fd = (arbre) malloc (sizeof (noeud_t));
assert(a->fd);
a->fd->num = v;
a->fd->coef = y;
a->fd->unit = x;
a->fd->fg = (arbre) 0;
a->fd->fd = (arbre) 0;
return a;
}
a->fd = ajout(a->fd, v, x, y);
return a;
}
/*
To create the tree, you add, one after the other, the values in the
tree.*/
arbre creation (int n) {
arbre a;
float x, y, coef;
x = 1.5;
y = 0.21;
coef = 120.4;
a = (arbre) 0;
// srand(time(NULL));
while (n--) {
a = ajout (a, n, x, coef);
x += y;
y += 0.06;
if (x > 3.0 || x < -2.0) {
y = - y;
}
if (! (n % 20))
coef += 0.5;
}
return a;
}
|
void afficharbre (arbre a) {
if (a) {
afficharbre (a->fg);
printf("%d %3.2f %5.2f %6.4f ", a->num, a->unit, a->coef, a->total);
afficharbre (a->fd);
}
}
|
int appartient (arbre a, int numero) {
if (! a)
return 0;
if (a->num == numero)
return 1;
if (appartient (a->fd, numero))
return 1;
return appartient (a->fg, numero);
}
|
int main () {
int n;
arbre a, b;
int x;
/*
srand(time(NULL));
*/
n = 20;
a = creation (n);
printf("\n Vois arbre \n");
afficharbre (a);
x = 18;
printf("\n %d dedans %s\n", x, appartient(a,x) ? "Oui" : "Non");
x = 22;
printf("%d dedans %s\n", x, appartient(a,x) ? "Oui" : "Non");
}
|
0.720000 dedans Non
|
int fappartient (arbre a, float x) {
if (! a)
return 0;
if (fabs(a->unit - x) < 0.0001)
return 1;
return fappartient(a->fg, x) || fappartient (a->fd, x);
}
|
int main () {
int n;
arbre a, b;
float x;
/*
srand(time(NULL));
*/
n = 20;
a = creation (n);
printf("\n Vois \n");
voisarbre (a,0);
x = 0.72;
printf("methode 1\t %f dedans %s\n", x, appartient(a,x) ? "Oui" : "Non");
printf("methode 2\t%f dedans %s\n", x, fappartient(a,x) ? "OUI" : "NON");
}
|
methode 1 0.720000 dedans Non
methode 2 0.720000 dedans OUI
|
arbre ajout (arbre a, int v, float x) {
arbre new;
if (! a) {
new = (arbre) malloc (sizeof (noeud));
new->num = v;
new->val = x;
new->fg = (arbre) 0;
new->fd = (arbre) 0;
return new;
}
if (x < a->val) {
/* on le met à gauche*/
if (! (a->fg)) {
/* s‘il ny a pas de fils gauche */
a->fg = (arbre) malloc (sizeof (noeud));
a->fg->num = v;
a->fg->val = x;
a->fg->fg = (arbre) 0;
a->fg->fd = (arbre) 0;
return a;
}
a->fg = ajout(a->fg, v, x);
return a;
}
/* on le met donc a droite*/
if (! (a->fd)) {
/* s‘il ny a pas de fils droit */
a->fd = (arbre) malloc (sizeof (noeud));
a->fd->num = v;
a->fd->val = x;
a->fd->fg = (arbre) 0;
a->fd->fd = (arbre) 0;
return a;
}
a->fd = ajout(a->fd, v, x);
return a;
}
arbre creation (int n) {
arbre a;
float x, y;
x = 1.5;
y = 0.21;
a = (arbre) 0;
while (n--) {
a = ajout (a, n, x);
x += y;
y += 0.06;
if (x > 3.0 || x < -2.0) {
y = - y;
}
}
return a;
}
|
int appartient (arbre a, float x) {
if (! a)
return 0;
if (a->val == x)
return 1;
if (a->val > x)
return appartient (a->sg, x);
return appartient (a->sd, x);
}
int appartientf (arbre a, float x) {
if (! a)
return 0;
if (fabsf(a->val - x) < 0.0001)
return 1;
if (a->val > x)
return appartientf (a->sg, x);
return appartientf (a->sd, x);
}
|
typedef void (* ptrfct) (); |
void fcta () {
printf(" a ");
}
void fctb () {
printf(" b ");
}
|
void g (void (* fct) ()) {
printf("g appelle :");
fct();
printf("\n");
}
void gg (ptrfct f) {
printf("gg appelle :");
f();
printf("\n");
}
void fcttest () {
g(fcta);
g(fctb);
gg(fcta);
gg(fctb);
}
|
int menu () {
int n;
n = 0;
while (!n) {
printf("Choisissez entre la fonction a (tapez 1)\n");
printf("et la fonction b (tapez 2) :\n");
scanf("%d",&n);
if (n==1 || n==2)
break;
n = 0;
}
return n-1; /* Notons le n - 1 */
}
|
void main () {
int n;
ptrfct tf [] = {fcta, fctb};
fcttest ();
n = menu();
tf[n]();
for (n=0; n < 2; n++)
gg(tf[n]);
}
|
typedef void (* ptrfct) (int);
void fcta (int a) {
printf("Fonction 1: ");
printf("%d\n",a);
}
void fctb (int a) {
printf("Fonction 2: ");
printf("%d\n",a);
}
void g (void (* fct) (), int n) {
printf("g appelle :");
fct(n);
printf("\n");
}
void gg (ptrfct f, int n) {
printf("gg appelle :");
f(n);
printf("\n");
}
int menu () {
int n;
n = 0;
while (!n)
{
printf("Choisissez entre la fonction a (tapez 1)\n");
printf("et la fonction b (tapez 2) :\n");
scanf("%d",&n);
if (n==1 || n==2)
break;
n = 0;
}
return n-1;
}
int main ()
{
int n, m;
ptrfct tf [] = {fcta, fctb};
m = 10;
n = menu();
g(tf[n], m);
gg(tf[n], m);
}
|