Implémentation des vecteurs en C
SOIT TAB UN TABLEAU (5, 10) ;
/** Implémentation **\: TABLEAU DE ENTIERS**/
/** Tableaux **/
typedef int Typeelem_V5_10i ;
typedef Typeelem_V5_10i * Typevect_V5_10i ;
Typeelem_V5_10i Element_V5_10i ( Typevect_V5_10i V , int I1 , int I2 )
{
return *(V + I2-1 + (I1-1) *5 ) ;
}
void Aff_element_V5_10i ( Typevect_V5_10i V , int I1 , int I2, Typeelem_V5_10i Val )
{
*(V + I2-1 + (I1-1) *5 ) = Val ;
}
/** Partie déclaration de variables **/
Typevect_V5_10i Tab;
int main(int argc, char *argv[])
{
Tab = malloc(5*10 * sizeof(int));
}
Implémentation des structures en C
soit S une structure (Chaine, entier);
typedef char * string256 ;
/** Structures statiques **/
typedef struct Tsi Type_Tsi ;
typedef Type_Tsi * Typestr_Tsi ;
typedef string256 Type1_Tsi ;
typedef int Type2_Tsi ;
struct Tsi
{
Type1_Tsi Champ1 ;
Type2_Tsi Champ2 ;
};
Type1_Tsi Struct1_Tsi ( Typestr_Tsi S)
{
return S->Champ1 ;
}
Type2_Tsi Struct2_Tsi ( Typestr_Tsi S)
{
return S->Champ2 ;
}
void Aff_struct1_Tsi ( Typestr_Tsi S, Type1_Tsi Val )
{
strcpy( S->Champ1 , Val );
}
void Aff_struct2_Tsi ( Typestr_Tsi S, Type2_Tsi Val )
{
S->Champ2 = Val ;
}
/** Partie déclaration de variables **/
Typestr_Tsi S;
int main(int argc, char *argv[])
{
S = malloc(sizeof(Typestr_Tsi));
S->Champ1 = malloc(sizeof(string256));
}
Implémentation des listes linéaires chaînées en C
SOIT L UNE LISTE ;
/** Implémentation **\: LISTE DE ENTIERS**/
/** Listes linéaires chaînées **/
typedef int Typeelem_Li ;
typedef struct Maillon_Li * Pointeur_Li ;
struct Maillon_Li
{
Typeelem_Li Val ;
Pointeur_Li Suiv ;
} ;
Pointeur_Li Allouer_Li (Pointeur_Li *P)
{
*P = (struct Maillon_Li *) malloc( sizeof( struct Maillon_Li)) ;
}
void Aff_val_Li(Pointeur_Li P, Typeelem_Li V)
{
P->Val = V ;
}
void Aff_adr_Li( Pointeur_Li P, Pointeur_Li Q)
{
P->Suiv = Q ;
}
Pointeur_Li Suivant_Li( Pointeur_Li P)
{ return( P->Suiv ) ; }
Typeelem_Li Valeur_Li( Pointeur_Li P)
{ return( P->Val) ; }
void Liberer_Li ( Pointeur_Li P)
{ free (P);}
/** Partie déclaration de variables **/
Pointeur_Li L;
Implémentation des listes bidirectionnelles en C
SOIT L UNE LISTEBI ;
/** Implémentation **\: LISTE BIDIRECTIONNELLE DE ENTIERS**/
/** Listes bidirectionnelles **/
typedef int Typeelem_Ri ;
typedef struct Maillon_Ri * Pointeur_Ri ;
struct Maillon_Ri
{
Typeelem_Ri Val ;
Pointeur_Ri Suiv ;
Pointeur_Ri Prec ;
} ;
Pointeur_Ri Allouer_Ri (Pointeur_Ri *P)
{ *P = (struct Maillon_Ri *) malloc( sizeof( struct Maillon_Ri)) ; }
void Aff_val_Ri(Pointeur_Ri P, Typeelem_Ri V)
{ P->Val = V ; }
void Aff_adrd_Ri( Pointeur_Ri P, Pointeur_Ri Q)
{ P->Suiv = Q; }
void Aff_adrg_Ri( Pointeur_Ri P, Pointeur_Ri Q)
{ P->Prec = Q; }
Pointeur_Ri Suivant_Ri( Pointeur_Ri P)
{ return( P->Suiv ); }
Pointeur_Ri Precedent_Ri( Pointeur_Ri P)
{ return( P->Prec ); }
Typeelem_Ri Valeur_Ri( Pointeur_Ri P)
{ return( P->Val) ; }
void Liberer_Ri ( Pointeur_Ri P)
{ free (P) ; }
/** Partie déclaration de variables **/
Pointeur_Ri Lb;
Implémentation des arbres de recherche binaire en C
Soit A un arb ;
/** Implémentation **\: ARBRE BINAIRE DE ENTIERS**/
/** Arbres de recherche binaire **/
typedef int Typeelem_Ai ;
typedef struct Noeud_Ai * Pointeur_Ai ;
struct Noeud_Ai
{
Typeelem_Ai Val ;
Pointeur_Ai Fg ;
Pointeur_Ai Fd ;
Pointeur_Ai Pere ;
} ;
Typeelem_Ai Info_Ai( Pointeur_Ai P )
{ return P->Val; }
Pointeur_Ai Fg_Ai( Pointeur_Ai P)
{ return P->Fg ; }
Pointeur_Ai Fd_Ai( Pointeur_Ai P)
{ return P->Fd ; }
Pointeur_Ai Pere_Ai( Pointeur_Ai P)
{ return P->Pere ; }
void Aff_info_Ai ( Pointeur_Ai P, Typeelem_Ai Val)
{ P->Val = Val ; }
void Aff_fg_Ai( Pointeur_Ai P, Pointeur_Ai Q)
{ P->Fg = Q; }
void Aff_fd_Ai( Pointeur_Ai P, Pointeur_Ai Q)
{ P->Fd = Q ; }
void Aff_pere_Ai( Pointeur_Ai P, Pointeur_Ai Q)
{ P->Pere = Q ; }
void Creernoeud_Ai( Pointeur_Ai *P)
{
*P = (struct Noeud_Ai *) malloc( sizeof( struct Noeud_Ai)) ;
(*P)->Fg = NULL;
(*P)->Fd = NULL;
(*P)->Pere = NULL;
}
void Liberernoeud_Ai( Pointeur_Ai P)
{ free( P ) ; }
/** Partie déclaration de variables **/
Pointeur_Ai A;
Implémentation des arbres de recherche m-aire en C
Soit M un arM(4) ;
/** Implémentation **\: ARBRE M-AIRE DE ENTIERS**/
/** Arbres de recherche m-aire **/
typedef int Typeelem_M4i ;
typedef struct Noeud_M4i * Pointeur_M4i ;
struct Noeud_M4i
{
int Degre ;
Pointeur_M4i Fils[4] ;
Typeelem_M4i Infor[4] ;
Pointeur_M4i Parent ;
} ;
Typeelem_M4i Infor_M4i(Pointeur_M4i P, int I)
{ return P->Infor[I-1] ; }
Pointeur_M4i Fils_M4i( Pointeur_M4i P, int I)
{ return P->Fils[I-1] ; }
Pointeur_M4i Parent_M4i( Pointeur_M4i P)
{ return P->Parent ; }
void Aff_infor_M4i ( Pointeur_M4i P, int I, Typeelem_M4i Val)
{ P->Infor[I-1] = Val ; }
void Aff_fils_M4i( Pointeur_M4i P, int I, Pointeur_M4i Q)
{ P->Fils[I-1] = Q ; }
void Aff_parent_M4i( Pointeur_M4i P, Pointeur_M4i Q)
{ P->Parent = Q ; }
void Creernoeud_M4i( Pointeur_M4i *P )
{
int I ;
*P = (struct Noeud_M4i *) malloc( sizeof( struct Noeud_M4i)) ;
for (I=0; I< 4; ++I) (*P)->Fils[I] = NULL;
(*P)->Degre = 0 ;
}
int Degre_M4i ( Pointeur_M4i P )
{ return P->Degre ; }
void Aff_degre_M4i ( Pointeur_M4i P, int N)
{ P->Degre = N ; }
void Liberernoeud_M4i(Pointeur_M4i P)
{ free ( P );}
/** Partie déclaration de variables **/
Pointeur_M4i M;
Soit P une pile ;
typedef int bool ;
#define True 1
#define False 0
/** Implémentation **\: PILE DE ENTIERS**/
/** Piles **/
typedef int Typeelem_Pi ;
typedef struct Maillon_Pi * Pointeur_Pi ;
typedef Pointeur_Pi Typepile_Pi ;
struct Maillon_Pi
{
Typeelem_Pi Val ;
Pointeur_Pi Suiv ;
} ;
void Creerpile_Pi( Pointeur_Pi *P )
{ *P = NULL ; }
bool Pilevide_Pi ( Pointeur_Pi P )
{ return (P == NULL ); }
void Empiler_Pi ( Pointeur_Pi *P, Typeelem_Pi Val )
{
Pointeur_Pi Q;
Q = (struct Maillon_Pi *) malloc( sizeof( struct Maillon_Pi)) ;
Q->Val = Val ;
Q->Suiv = *P;
*P = Q;
}
void Depiler_Pi ( Pointeur_Pi *P, Typeelem_Pi *Val )
{
Pointeur_Pi Sauv;
if (! Pilevide_Pi (*P) )
{
*Val = (*P)->Val ;
Sauv = *P;
*P = (*P)->Suiv;
free(Sauv);
}
else printf ("%s \n", "Pile vide");
}
/** Partie déclaration de variables **/
Pointeur_Pi P;
Implémentation des files d'attente en C
Soit F une file ;
typedef int bool ;
#define True 1
#define False 0
/** Implémentation **\: FILE DE ENTIERS**/
/** Files d'attente **/
typedef int Typeelem_Fi ;
typedef struct Filedattente_Fi * Pointeur_Fi ;
typedef struct Maillon_Fi * Ptliste_Fi ;
struct Maillon_Fi
{
Typeelem_Fi Val ;
Ptliste_Fi Suiv ;
};
struct Filedattente_Fi
{
Ptliste_Fi Tete, Queue ;
};
void Creerfile_Fi ( Pointeur_Fi *Fil )
{
*Fil = (struct Filedattente_Fi *) malloc( sizeof( struct Filedattente_Fi)) ;
(*Fil)->Tete = NULL ;
(*Fil)->Queue = NULL ;
}
bool Filevide_Fi (Pointeur_Fi Fil )
{ return Fil->Tete == NULL ;}
void Enfiler_Fi ( Pointeur_Fi Fil , Typeelem_Fi Val )
{
Ptliste_Fi P;
P = (struct Maillon_Fi *) malloc( sizeof( struct Maillon_Fi)) ;
P->Val = Val ;
P->Suiv = NULL;
if ( ! Filevide_Fi(Fil) )
Fil->Queue->Suiv = P ;
else Fil->Tete = P;
Fil->Queue = P;
}
void Defiler_Fi (Pointeur_Fi Fil, Typeelem_Fi *Val )
{
if (! Filevide_Fi(Fil) )
{
*Val = Fil->Tete->Val ;
Fil->Tete = Fil->Tete->Suiv;
}
else printf ("%s \n", "File d'attente vide");
}
/** Partie déclaration de variables **/
Pointeur_Fi F;
Implémentation des fichiers en C
soit F un fichier de (chaines, entier) entete (ENTIER, entier) buffer B1;
#include <stdio.h>
#include <stdlib.h>
typedef int bool ;
typedef char * string255 ;
#define True 1
#define False 0
/** Implémentation **\: FICHIER
/* Traitement des fichiers ouverts */
/* Traitement des fichiers ouverts */
struct _Noeud
{
FILE * Var_fich ;
char * Nom_fich ;
int Sauv_pos;
struct _Noeud *Suiv ;
} ;
typedef struct _Noeud * _Ptr_Noeud;
_Ptr_Noeud _Pile_ouverts = NULL;
/* Teste si un fichier est ouvert */
_Ptr_Noeud _Ouvert ( char * Fp)
{
_Ptr_Noeud P;
bool Trouv ;
P = _Pile_ouverts; Trouv = False ;
while ((P != NULL) && ! Trouv )
if ( strcmp(P->Nom_fich, Fp) == 0)
Trouv = True;
else P = P->Suiv;
return P;
}
/* Ajouter un fichier ouvert */
void _Empiler_ouvert ( char *Fp, FILE *Fl)
{
_Ptr_Noeud P ;
P = (_Ptr_Noeud) malloc( sizeof( struct _Noeud)) ;
P->Nom_fich = Fp;
P->Var_fich = Fl;
P->Suiv = _Pile_ouverts;
_Pile_ouverts = P;
}
/* Supprimer un fichier ouvert et rendre son nom*/
char * _Depiler_ouvert ( FILE *Fl)
{
char * Fp = malloc (100);
_Ptr_Noeud P, Prec ;
P= _Pile_ouverts;
Prec = NULL;
while (P->Var_fich != Fl )
{ Prec = P ; P = P->Suiv ;}
strcpy(Fp, P->Nom_fich);
if (Prec != NULL)
Prec->Suiv = P->Suiv;
else _Pile_ouverts = P->Suiv;
free (P);
return Fp ;
}
/** Fichiers **/
typedef char _Tx[255];
/** Types des champs du bloc **/
typedef string255 Typechamp1_siEii;
typedef _Tx Typechamp1_siEii_Buf ;
typedef int Typechamp2_siEii;
/** Types des champs de l'en-tête */
typedef int Typeentete1_siEii ;
typedef int Typeentete2_siEii ;
/** Type du bloc de données du fichier **/
typedef struct
{
Typechamp1_siEii_Buf Champ1 ;
Typechamp2_siEii Champ2 ;
} Typestruct1_siEii_Buf;
/** Type de la structure du bloc de données du fichier **/
typedef struct
{
Typechamp1_siEii Champ1 ;
Typechamp2_siEii Champ2 ;
} Typestruct1_siEii_;
typedef Typestruct1_siEii_ * Typestruct1_siEii ;
typedef Typestruct1_siEii Typestr_Tsi;
typedef Typestruct1_siEii_ Type_Tsi;
/** Type du bloc des caractéristiques du fichier **/
typedef struct
{
Typeentete1_siEii Entete1 ;
Typeentete2_siEii Entete2 ;
} Typestruct2_siEii ;
/** Manipulation de la structure **/
Typechamp1_siEii Struct1_Tsi ( Typestruct1_siEii Buf )
{
return Buf->Champ1;
}
Typechamp2_siEii Struct2_Tsi ( Typestruct1_siEii Buf )
{
return Buf->Champ2;
}
void Aff_struct1_Tsi ( Typestruct1_siEii Buf, Typechamp1_siEii Val )
{
strcpy( Buf->Champ1 , Val );
}
void Aff_struct2_Tsi ( Typestruct1_siEii Buf, Typechamp2_siEii Val )
{
Buf->Champ2 = Val ;
}
/** Déclaration du buffer de l'en-tête **/
Typestruct2_siEii Bloc_caract_siEii;
/** Opérations sur les fichiers **/
void Ouvrir_siEii ( FILE **siEii , char *Fp , char * Mode )
{
_Ptr_Noeud P = _Ouvert(Fp);
if ( P != NULL )
/* Le fichier est déjà ouvert */
{
P->Sauv_pos = ftell (P->Var_fich);
fseek( P->Var_fich, 0, 0);
fwrite(&Bloc_caract_siEii, sizeof(Typestruct2_siEii), 1, P->Var_fich);
fclose(P->Var_fich);
}
/* Le fichier est non ouvert */
if ( strcmp(Mode,"A") == 0)
{
*siEii = fopen(Fp, "r+b");
fread(&Bloc_caract_siEii, sizeof(Typestruct2_siEii), 1, *siEii);
}
else
{
*siEii = fopen(Fp, "w+b");
fwrite(&Bloc_caract_siEii, sizeof(Typestruct2_siEii), 1, *siEii) ;
}
_Empiler_ouvert( Fp, *siEii);
}
void Fermer_siEii ( FILE * siEii )
{
char * Fp = malloc(100);
_Ptr_Noeud P ;
strcpy(Fp, _Depiler_ouvert(siEii));
fseek( siEii, 0, 0);
fwrite(&Bloc_caract_siEii, sizeof(Typestruct2_siEii), 1, siEii);
fclose(siEii) ;
/* Ya-til un fichier ouvert avec le même nom ? */
/* Si Oui, le Réouvrir à la position sauvegardée */
P = _Ouvert (Fp);
if ( P != NULL)
{
siEii = fopen(P->Nom_fich, "r+b");
fread(&Bloc_caract_siEii, sizeof(Typestruct2_siEii), 1, siEii);
fseek(siEii, P->Sauv_pos, 0);
}
}
Typeentete1_siEii Entete1_siEii( FILE * siEii)
{
return Bloc_caract_siEii.Entete1;
}
Typeentete2_siEii Entete2_siEii( FILE * siEii)
{
return Bloc_caract_siEii.Entete2;
}
void Aff_entete1_siEii ( FILE * siEii, Typeentete1_siEii VAL)
{
Bloc_caract_siEii.Entete1 = VAL ;
}
void Aff_entete2_siEii ( FILE * siEii, Typeentete2_siEii VAL)
{
Bloc_caract_siEii.Entete2 = VAL ;
}
void Ecrireseq_siEii ( FILE * siEii, Typestruct1_siEii Buf )
{
Typestruct1_siEii_Buf Buffer ;
int I, J;
for(J=0; J<= strlen(Buf->Champ1); ++J)
Buffer.Champ1[J] = Buf->Champ1[J];
Buffer.Champ2 = Buf->Champ2;
fwrite(&Buffer, sizeof( Typestruct1_siEii_Buf), 1, siEii) ;
}
void Ecriredir_siEii ( FILE * siEii, Typestruct1_siEii Buf, int N )
{
Typestruct1_siEii_Buf Buffer ;
int I, J;
for(J=0; J<= strlen(Buf->Champ1); ++J)
Buffer.Champ1[J] = Buf->Champ1[J];
Buffer.Champ2 = Buf->Champ2;
fseek(siEii, (long) ((N-1)* sizeof( Typestruct1_siEii_Buf) +
sizeof( Typestruct2_siEii)), 0 );
fwrite(&Buffer, sizeof( Typestruct1_siEii_Buf), 1, siEii) ;
}
void Lireseq_siEii ( FILE * siEii, Typestruct1_siEii Buf )
{
Typestruct1_siEii_Buf Buffer ;
int I, J;
if (fread(&Buffer, sizeof( Typestruct1_siEii_Buf), 1, siEii)!=0){
for(J=0; J<= strlen(Buffer.Champ1); ++J)
Buf->Champ1[J] = Buffer.Champ1[J];
Buf->Champ2= Buffer.Champ2;
}
}
void Liredir_siEii ( FILE * siEii, Typestruct1_siEii Buf, int N)
{
Typestruct1_siEii_Buf Buffer ;
int I, J;
fseek(siEii, (long) ((N-1)* sizeof( Typestruct1_siEii_Buf) +
sizeof( Typestruct2_siEii)), 0 );
fread(&Buffer, sizeof( Typestruct1_siEii_Buf), 1, siEii);
for(J=0; J<= strlen(Buffer.Champ1); ++J)
Buf->Champ1[J] = Buffer.Champ1[J];
Buf->Champ2= Buffer.Champ2;
}
void Rajouter_siEii ( FILE * siEii, Typestruct1_siEii Buf )
{
Typestruct1_siEii_Buf Buffer ;
int I, J;
for(J=0; J<= strlen(Buf->Champ1); ++J)
Buffer.Champ1[J] = Buf->Champ1[J];
Buffer.Champ2 = Buf->Champ2;
fseek(siEii, 0, 2); /* Fin du fichier */
fwrite(&Buffer, sizeof( Typestruct1_siEii_Buf), 1, siEii) ;
}
bool Finfich_siEii (FILE * siEii)
{
long K = ftell(siEii);
fseek(siEii, 0, 2); /* Fin du fichier */
long K2 = ftell(siEii); /* position à partir du debut */
if (K==K2)
{ fseek(siEii, K, 0); return 1;}
else
{ fseek(siEii, K, 0); return 0;}
}
int Alloc_bloc_siEii (FILE * siEii)
{
long K;
fseek(siEii, 0, 2); /* Fin du fichier */
K = ftell(siEii); /* position à partir du debut */
K = K - sizeof( Typestruct2_siEii); /* Ignorer l'en_tête */
K = K / sizeof (Typestruct1_siEii_Buf);
K ++;
return(K);
}
/** Variables du programme principal **/
FILE *F;
Typestruct1_siEii B1 ;
int main(int argc, char *argv[])
{
B1 = malloc(sizeof(Typestruct1_siEii));
B1->Champ1 = malloc(255 * sizeof(string255));
;
system("PAUSE");
return 0;
}