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
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
typedef struct phrase1
{
char phrase[MAX];
char cle1[MAX];
char cle2[MAX];
char cle3[MAX];
char cle4[MAX];
}phrase1;
void creer_phrase(FILE *fp,phrase1 *p);
void afficher_phrases(phrase1 *p, FILE *fp);
void conversation(phrase1 *q,FILE *fp,char *question);
int main(void)
{
FILE *fp;
int n;
phrase1 m,k,d;
char question[MAX];
if(!(fp = fopen("fichier_phrases.dat", "r+b"))) // On créé le fichier au cas où il n'existe pas
fp = fopen("fichier_phrases.dat", "w+b");
printf("1 : creer une réponse type \n");
printf("2 : afficher les reponses types \n");
printf("3 : Commencer une conversation \n");
scanf("%d",&n);
switch(n)
{
case 1 : creer_phrase(fp,&m);
break;
case 2 : afficher_phrases(&k,fp);
break;
case 3 :
printf("Bonjour ! \n\n je vais vous guider dans votre rechercher de voitures ! \n\n");
printf("Sam : Et si vous commenciez par poser une question : \n\n");
getchar();
fgets(question, 100, stdin); // le dernier caractère est à strlen(c->phrase)-1 c->phrase[strlen(c->phrase)-1]='\0'
question[strlen(question)] = '\0';
conversation(&d,fp,question);
break;
default : printf("Erreur !\n");
}
}
void creer_phrase(FILE *fp,phrase1 *p)
{
printf("Vous pouvez créer des phrases types associées chacune à 4 mots clés \n\n");
printf("Veuillez entrer une phrase : \n");
getchar();
fgets(p->phrase, 100, stdin); // le dernier caractère est à strlen(c->phrase)-1 c->phrase[strlen(c->phrase)-1]='\0'
p->phrase[strlen(p->phrase)-1] = '\0';
printf("Entrez un mot clé que vous voulez lui associer : \n\n");
scanf("%s",p->cle1);
printf("Entrez un mot clé que vous voulez lui associer : \n\n");
scanf("%s",p->cle2);
printf("Entrez un mot clé que vous voulez lui associer : \n\n");
scanf("%s",p->cle3);
printf("Entrez un mot clé que vous voulez lui associer : \n\n");
scanf("%s",p->cle4);
fseek(fp, 0, SEEK_END);
fwrite(p,sizeof(phrase1),1,fp);
}
void afficher_phrases(phrase1 *p, FILE *fp)
{
rewind(fp);
while (fread(p,sizeof(phrase1),1,fp)!=0)
{
printf("------------------------------------------------\n");
printf("Phrase type : %s \n",p->phrase);
printf("Mots clés associés : %s \n %s \n %s \n %s \n", p->cle1,p->cle2,p->cle3,p->cle4);
printf("--------------------------------------------------\n");
}
}
void conversation(phrase1 *q,FILE *fp,char *question)
{
char *token;
token = strtok(question, " ");
while (token != NULL)
{
rewind(fp);
while (fread(q,sizeof(phrase1),1,fp)!=0)
{
if ((strcasecmp(q->cle1, token) == 0)||(strcasecmp(q->cle2, token) == 0)||(strcasecmp(q->cle3, token) == 0)||(strcasecmp(q->cle4, token) == 0))
{
printf("Sam : %s",q->phrase);
}
else
{
printf("Veuillez préciser... \n");
}
token = strtok(NULL, " ");
}
}
} |
Partager