//新建一个字典文本文件,命名为:dict.dat,内容格式如下: 或下载一个也可//abandon v.抛弃,放弃//abandonment n.放弃//abbreviation n.缩写//abeyance n.缓办,中止//abide v.遵守//ability n.能力//able adj.有能力的,能干的//abnormal adj.反常的,变态的//aboard adv.船(车)上//abolish v.废除,取消//******************************#include #include #include struct NODE{ char e[20]; char c[50]; struct NODE* pnext;};struct NODE* pN;struct NODE* pHead=NULL;struct NODE* pNode=NULL;struct NODE* pNext=NULL;struct NODE* pEnd=NULL;FILE* open_dict(){ FILE *fp; if((fp=fopen("dict.dat","r"))==NULL) { printf("open dict error!\n"); return NULL; } return fp;}int getdata_from_dict(FILE* fp){ int i,j; char buf[50]; while(1) { pN=(struct NODE*)malloc(sizeof(struct NODE)); memset(pN->c,'\0',50); memset(pN->e,'\0',20); pN->pnext=NULL; if(fgets(buf,50,fp)==NULL) { fclose(fp); break; } sscanf(buf,"%s %s",pN->e,pN->c); if(pHead==NULL) { pHead=pN; pNode=pN; continue; } pNode->pnext=pN; pNode=pN; pEnd=pN; } return 1;}void write_dict(){ FILE* fp=NULL; if((fp=fopen("dict.dat","w"))==NULL) { printf("open dict error!\n"); return; } char buf[100]={'\0'}; pNode=pHead; while(1) { if(pNode==NULL) { break; } fprintf(fp,"%s %s\n",pNode->e,pNode->c); pNode=pNode->pnext; } fclose(fp);}void del(char* char_del){ struct NODE* pDel=pHead; if(strcmp(pDel->e,char_del)==0) { pHead=pHead->pnext; write_dict(); return ; } while(1) { if(strcmp(pDel->pnext->e,char_del)==0) { if(pDel->pnext->pnext!=NULL) { pDel->pnext=pDel->pnext->pnext; } else { pDel->pnext=NULL; } write_dict(); return ; } else { pDel=pDel->pnext; if(pDel==NULL) { break; } } } printf("\n未找到该单词!\n"); return;}void add(char* char_add, char* char_add_chs){ pN=(struct NODE*)malloc(sizeof(struct NODE)); memset(pN->c,'\0',50); memset(pN->e,'\0',20); pN->pnext=NULL; strcpy(pN->e,char_add); strcpy(pN->c,char_add_chs); pEnd->pnext=pN; pEnd=pN; pNode=pN; write_dict();}int main(int argc, char *argv[]) { FILE* fp=NULL; fp=open_dict(); if(fp==NULL) { printf("读取字典失败! 程序将退出!"); getchar(); return 1; } getdata_from_dict(fp); fclose(fp); char buf[50]={'\0'}; while(1) { printf("\n\n{输入quit: 即退出, 输入del: 即删除, 输入add: 即增加}\n请输入英文单词 : "); fflush(stdin); scanf("%s",buf); if(strcmp(buf,"quit:")==0) { break; } else if(strcmp(buf,"del:")==0) { char char_del[20]={'\0'}; printf("请输入要删除的单词:"); fflush(stdin); scanf("%s",char_del); del(char_del); continue; } else if(strcmp(buf,"add:")==0) { char char_add[20]={'\0'}; char char_add_chs[100]={'\0'}; printf("\n请输入要增加的单词:"); fflush(stdin); scanf("%s",char_add); printf("\n请输入中文意思:"); fflush(stdin); scanf("%[^\n]",char_add_chs); add(char_add,char_add_chs); continue; } pNode=pHead; int find=0; while(pNode!=NULL) { if(strcmp(pNode->e,buf)==0) { printf("\n%s",pNode->c); find=1; break; } pNode=pNode->pnext; } if(find==0) { printf("\n未找到!"); } } return 0;}