1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
/*Definition de la liste */
typedef struct pall
{
char chaine[30];
int t[100];
struct pall *prev;
struct pall *next;
}
Pall;
typedef struct
{
Pall *first;
Pall *last;
int Nbre ;
}
liste_Pall;
/*Initialisation de la liste */
liste_Pall *initialiser_liste()
{liste_Pall *L;
printf("Intialisation de la liste chainee \n");
L = (liste_Pall *) malloc (sizeof(liste_Pall));
L->first=NULL;
L->last=NULL;
L->Nbre=0;
return(L);
}
/* Procedure creation fichier */
void Creer(FILE **fp)
{
char f[30];
printf("Fichier:");
scanf("%s",f);
*fp=fopen(f,"r") ;
if (*fp==NULL )
{printf("Probleme ouverture!!!");exit(EXIT_FAILURE); }
}
/* fonction palindrome */
int palindrome (char *s)
{int k,j,l;
l=strlen(s);
for(k=0,j=l-1;k<j;k++,j--)
{if(s[k]!=s[j])
return 0;}
return 1;
}
/* recherche un palindrome dans la liste */
Pall* Rechercher(char s[30],liste_Pall *L)
{ Pall *p ;
p=L->first;
while(p!= NULL && strcmp(p->chaine,s)!=0)
{p = p->next;printf("%s",p->chaine);}
return p;
}
/*Ajouter le palindrome dans la liste */
liste_Pall * Ajout_Pall (liste_Pall *l ,char s[30] ,int ligne)
{int i;
Pall *nouv = (Pall *)malloc(sizeof(Pall));
if(!nouv) exit(EXIT_FAILURE);
for(i=1;i<=100;i++)
{nouv->t[i] =0;}
strcpy(nouv->chaine,s);
nouv->t[ligne] = 1;
if (l->Nbre==0)
{ nouv->next = NULL;
nouv->prev = NULL;
l->last = nouv;
l->first = nouv;
l->Nbre++;
}
else
{
nouv->prev = l->first;
nouv->next = NULL;
l->first=nouv;
l->Nbre++;
}
return(l); }
/* Affichage de la liste de palindrome */
void affiche(liste_Pall *L)
{
Pall *courant;
courant = L->first; /* point du départ le 1er élément */
printf("les mots palaindrome dans la liste sont :\n ");
while(courant != NULL)
{
printf("%s\n",courant->chaine);
courant = courant->next;
}
}
int main()
{
FILE *fp=NULL;
char s[30];
int i,p;
int c;
Pall *q;liste_Pall *L;
Creer(&fp) ;
i=1;
L=initialiser_liste();
while((c=fgetc(fp))!=EOF)
{
if (c == '\n') {i=i+1;}
fscanf(fp,"%s",s);
p=palindrome(s);
/* Affichage du résultat */
if(p==1)
{
printf("Le mot %s est palindrome ",s);
q=Rechercher(s,L);
if (q==NULL)
{ printf("n'existe pas dans la liste on va l'ajouter\n");
L=Ajout_Pall (L,s,i);
}
else
{printf("existe :%d fois \n",q->t[i]+1);}
}
}
affiche(L);
getch();
fclose(fp);
return(0);
} |
Partager