说智能化,有点不太合适。程序用来查询用户输入的信息,如果查到,则输出;若没有查到,则向用户询问,且具有记忆和自动更新数据库功能。不足之处在于,没有实现模糊查询(利用字符串匹配的知识不难自行增加相应功能);没有实现删除错误信息的功能,以后有时间慢慢添加吧。暂时可以看作一个对文件操作的小程序。

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

void main()
{
FILE *fp;
char name[9]="",nameT[9]="",phone[9]="";
char yn; /*continue or not*/
int yesno=1; /*continue to cycle or not*/
int flag; /*found or not*/
fpos_t *pos;
while(yesno)
{
fp=fopen("bugeyes.dic","r");
if(fp==NULL)
{
printf("\nSorry.Something is wrong.I can not find my database.");
exit(-1);
}
flag=0;
printf("\nPlease input the name you want to search:");
scanf("%s",nameT);
while(!feof(fp))
{
fscanf(fp,"\n%s %s",name,phone);
if(strcmp(name,nameT)==0)
{
printf("\nOK.I have got it.\n");
printf("The phone of %s is %s",name,phone);
flag=1;
}
}
if(flag!=1)
{
printf("\nSorry.I donnot know the answer.Can you tell me?(y/n)");
scanf("%c",&yn);
while(yn!=''''y''''&&yn!=''''Y''''&&yn!=''''n''''&&yn!=''''N'''')
{
printf("\nCan you tell me?(y/n)");
scanf("%c",&yn);
}
if(yn==''''n''''||yn==''''N'''')
{
printf("\nSorry.I am very gret to hear that.Bye\n");
fclose(fp);
exit(-1);
}
else if(yn==''''y''''||yn==''''Y'''')
{
printf("\nVery good!What is it?\n");
scanf("%s",phone);
fclose(fp);
fp=fopen("bugeyes.dic","a");
if(fp==NULL)
{
printf("\nOpen database error.Sorry\n");
exit(-1);
}
fprintf(fp,"\n%s %s",nameT,phone);
printf("\nThank you very much.I haver rememberd it\n");
fclose(fp);
}
}
scanf("%c",&yn);
while(yn!=''''n''''&&yn!=''''N''''&&yn!=''''y''''&&yn!=''''Y'''')
{
printf("\nDo you want to search another one?(y/n)");
scanf("%c",&yn);
}
if(yn==''''N''''||yn==''''n'''')
yesno=0;
}
}

posted on 2007-01-29 13:53  mbskys  阅读(281)  评论(0)    收藏  举报