Dictionnaire d'anagramme

Contenu du snippet

Ce programme permet de creer un dictionnaire d'anagramme
Il cree un fichier à la sortie que l'on peut reutiliser .

Source / Exemple :


#include<stdio.h>
#include<stdlib.h>

typedef struct anagramme *panag;
struct anagramme
{char mot[15];
panag pnext;
};

typedef struct classe *pclasse;
struct classe
{char signature[15];
pclasse psuivant;
panag pana;
};

void *alloue(int taille);
pclasse InsererClasse(pclasse *ptete,char sig[],pclasse *pcourant);
pclasse CreeClasse(char sig[]);
panag InsererAnagramme(pclasse *ptete,char ana[],pclasse *pcourant,panag *pcana);
panag CreeAnagramme(char ana[]);
char *CreeSig(char *test,char *dest);
void PrintListe(pclasse ptete);
int FindSig(pclasse ptete,char sig[]);
void InsTriAna(pclasse *ptete,char sig[],char test[]);
void InsTriSig(pclasse *ptete,char sig[]);
void BackupListe(pclasse ptete);
void ClearListe(pclasse ptete);
int FindAna(pclasse ptete,char test[],char sig[]);
main()
{
FILE *in;
char sig[15],ana[15],test[15],retour[15],fin[4]="fin\0";
pclasse ptete=NULL,pcourant=NULL;
panag pcana;
int fs,fa;

in=fopen("anagramme.txt","rt");
if(in == NULL)
    {
    fprintf(stderr,"Fichier inexistant !, ENTER pour continuer\n");
    getch();
    }
else 
    {while(!feof(in))
        {
        if(fgetc(in) == '*')
                {
                fscanf(in,"%s\n",&sig);
                InsererClasse(&ptete,sig,&pcourant);
                pcana=NULL;
                }
        else
                {
                fscanf(in,"%s\n",&ana);
                InsererAnagramme(&ptete,ana,&pcourant,&pcana);
                }
        }
    fclose(in);
    }
PrintListe(ptete);

do
    {printf("Entrez un mot \(fin pour terminer) \n");
     scanf("%s",test);
     strcpy(sig,(CreeSig(test,retour))); 
     printf("sig = %s\n",sig);
     fs=0;
     fa=1;
     fs=FindSig(ptete,sig);
     if(fs)
          fa=FindAna(ptete,test,sig);
     if(fs && fa)
          {InsTriAna(&ptete,sig,test);
          }
     else if(!fa) printf("Le Mote existe deja !\n");
     else 
     {
     InsTriSig(&ptete,sig);
     InsTriAna(&ptete,sig,test);
     }
            
     }
while(strcmp(test,fin));
BackupListe(ptete);
ClearListe(ptete);
getch();
}

void *alloue(int taille)
{
  void *p;
  p=(void*)malloc(taille);
  if(p==NULL)
  {
    printf("Erreur d allocation memoire\n");
    exit(1);
  }
  else return p;
}

pclasse CreeClasse(char sig[])
{
pclasse p;
p=(pclasse)alloue(sizeof(struct classe));
strcpy(p->signature,sig);
p->psuivant=NULL;
p->pana=NULL;
return p;
}

panag CreeAnagramme(char ana[])
{panag p;
p=(panag)alloue(sizeof(struct anagramme));
strcpy(p->mot,ana);
p->pnext=NULL;
return p;
}

pclasse InsererClasse(pclasse *ptete,char sig[],pclasse *pcourant)
{pclasse p;
p=CreeClasse(sig);
if(*ptete == NULL)
    {

  • ptete=p;
return (*pcourant)=p; } else { (*pcourant)->psuivant=p; return (*pcourant)=p; } } panag InsererAnagramme(pclasse *ptete,char ana[],pclasse *pcourant,panag *pcana) {panag p; p=CreeAnagramme(ana); if((*pcourant)->pana == NULL) { (*pcourant)->pana=p; return (*pcana)=p; } else { (*pcana)->pnext=p; return (*pcana)=p; } } char *CreeSig(char *test,char *dest) { int i,j,l; char tmp; strcpy(dest,test); l=strlen(dest); for(i=l;i>0;i--) {for(j=0;j<i-1;j++) { tmp=dest[j]; if(tmp > dest[j+1]) { dest[j]=dest[j+1]; dest[j+1]=tmp; } } } return dest; } void PrintListe(pclasse ptete) {pclasse p; panag q; p=ptete; while(p != NULL) { printf("Signature = %s\n",p->signature); q=p->pana; while(q != NULL) { printf("Anagramme = %s\n",q->mot); q=q->pnext; } p=p->psuivant; } } int FindSig(pclasse ptete,char sig[]) { pclasse p=ptete; while((p != NULL) && (strcmp(sig,p->signature))) p=p->psuivant; if (p== NULL)return 0; else return 1; } void InsTriAna(pclasse *ptete,char sig[],char test[]) { panag p,r,s; pclasse q; printf("Test = %s\n",test); p=CreeAnagramme(test); q=(*ptete); while((q != NULL) && (strcmp(sig,q->signature))) q=q->psuivant; r=q->pana; if(r == NULL) { q->pana=p; } else if((strcmp(test,(r->mot))) < 0) { p->pnext=q->pana; q->pana=p; } else {s=r->pnext; while((s != NULL) && (strcmp(test,(s->mot)) > 0)) { r=s; s=s->pnext; } p->pnext=s; r->pnext=p; } } void InsTriSig(pclasse *ptete,char sig[]) { pclasse p; p=CreeClasse(sig); if((*ptete) == NULL) (*ptete) =p; else if((strcmp(sig,((*ptete)->signature))) <0) { p->psuivant=(*ptete); (*ptete)=p; } else InsTriSig(&((*ptete)->psuivant),sig); } void BackupListe(pclasse ptete) {pclasse p; panag q; FILE *out; out=fopen("anagramme.txt","wt+"); if(out == NULL) { fprintf(stderr,"Erreur d ouverture du fichier ! \n"); getch(); exit(1); } p=ptete; while(p != NULL) { fprintf(out,"*%s\n",p->signature); q=p->pana; while(q != NULL) { fprintf(out,"=%s\n",q->mot); q=q->pnext; } p=p->psuivant; } fclose(out); } void ClearListe(pclasse ptete) {pclasse p,r; panag q,s; p=ptete; while(p != NULL) { printf("Signature = %s\n",p->signature); r=p; q=p->pana; while(q != NULL) { s=q; printf("Anagramme = %s\n",q->mot); q=q->pnext; free(s); } p=p->psuivant; free(r); } } int FindAna(pclasse ptete,char test[],char sig[]) {pclasse q; panag p; while((q != NULL) && (strcmp(sig,q->signature))) q=q->psuivant; if(q->pana == NULL)return 1; p=q->pana; while((p !=NULL) && (strcmp(test,p->mot))) p=p->pnext; if(p != NULL) return 0; else return 1; }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.