当前位置:文档之家› 英汉字典

英汉字典

#include <stdio.h>#include <conio.h>#include <string.h>#include <stdlib.h>struct node{char word[30];char ch[40];struct node *next;};struct node *head;struct node *q;void gongneng();void write();//查询单词void chaxun(){system("cls");struct node *p,*t;int flag=0;int n;p=(struct node *)malloc(sizeof(struct node));t=(struct node *)malloc(sizeof(struct node));t=head;printf("\n请输入要查询的单词:");scanf("%s",&p->word);do{if(strcmp(t->word,p->word)==0) {flag=1;break;}else t=t->next;}while(t);if(flag==1) {printf("%s %s\n",t->word,t->ch);}else {printf("\n该单词不存在!!!\n");}printf("\n\n1 继续查询!\n2 返回!\n其他任意键退出!\n\n\n请选择:");scanf("%d",&n);if(n==1) chaxun();else if(n==2) {system("CLS");gongneng();}else {printf("\n是否保存到文件\n\n\n1 是!\n0 否!\n请选择:");int m;scanf("%d",&m);if(m==1) write();exit(-1);}}//修改单词void change(){system("cls");struct node *p,*t;int flag=0,n,len;p=(struct node *)malloc(sizeof(struct node));t=(struct node *)malloc(sizeof(struct node));t=head;printf("\n请输入要修改的单词:");scanf("%s",&p->word);do{if(strcmp(t->word,p->word)==0) {flag=1;break;}else t=t->next;}while(t);if(flag==1) {printf("\n请输入该单词的信息:");scanf("%s",&t->ch);len=strlen(t->ch);t->ch[len]='\n';}else {printf("\n该单词不存在!!!!\n");}printf("\n\n\n1 继续修改!\n2 返回!\n其他任意键退出!\n\n请选择:");scanf("%d",&n);if(n==1) change();else if(n==2) {system("CLS");gongneng();}else {printf("\n是否保存到文件\n\n\n1 是!\n0 否!\n请选择:");int m;scanf("%d",&m);if(m==1) write();exit(-1);}}//添加单词void add(){system("cls");struct node *p,*t,*s;int n,flag=0,len;p=(struct node *)malloc( sizeof(struct node));t=(struct node *)malloc(sizeof(struct node));s=(struct node *)malloc(sizeof(struct node));s=head,t=head->next;printf("\n请输入添加的单词:");scanf("%s",&p->word);len=strlen(p->word);p->word[len]=' ';printf("\n请输入该单词的信息:");scanf("%s",&p->ch);len=strlen(p->ch);p->ch[len]='\n';while(s){if((strcmp(p->word,s->word)>0)&&(strcmp(p->word,t->word)<0)){p->next=s->next;s->next=p;flag=1;break;}else if(strcmp(p->word,s->word)==0) {printf("\n该单词已存在!!!\n");break;}else {s=s->next,t=t->next;}}if(flag==1) printf("\n\n添加成功!\n");printf("\n1 继续添加!\n2 返回!\n其他键退出!\n\n\n请选择:");scanf("%d",&n);if(n==1) add();else if(n==2) {system("cls");gongneng();}else {printf("\n是否保存到文件\n\n\n1 是!\n0 否!\n请选择:");int m;scanf("%d",&m);if(m==1) write();exit(-1);}}//删除单词void shanchu(){system("cls");struct node *p,*t,*s;int flag=0,n;p=(struct node *)malloc(sizeof(struct node));t=(struct node *)malloc(sizeof(struct node));s=(struct node *)malloc(sizeof(struct node));s=head,t=head->next;printf("\n请输入要删除的单词:");scanf("%s",&p->word);if(strcmp(head->word,p->word)==0) {head=head->next; printf("\n删除单词成功!\n");}//第一个单词else {while(t){if(strcmp(t->word,p->word)==0) {flag=1;break;}t=t->next;s=s->next;}if(flag==1){if(strcmp(q->word,p->word)==0) q=NULL;//最后一个单词else {s->next=t->next;free(t);}printf("\n删除单词成功!\n");}else {printf("\n该单词不存在!!!\n");}}printf("\n\n1 继续删除!\n2 返回!\n其他任意键退出!\n\n请选择:");scanf("%d",&n);if(n==1) shanchu();else if(n==2) {system("CLS");gongneng();}else {printf("\n是否保存到文件\n\n\n1 是!\n0 否!\n请选择:");int m;scanf("%d",&m);if(m==1) write();exit(-1);}}//读入文件内容到链表void read(){FILE *fp;struct node *p;p=q=(struct node *)malloc( sizeof(struct node));fp=fopen("F:\\English.txt","r");if(fp==NULL){printf("File open error!\n");exit(0);}head=NULL;head=p;while(!feof(fp)){fscanf(fp,"%s",p->word);fgets(p->ch,50,fp);q->next=p;q=p;p=(struct node *)malloc( sizeof(struct node));}q->next=NULL;fclose(fp);}//链表内容写入文件void write(){FILE *fp;struct node *p;p=(struct node *)malloc( sizeof(struct node));fp=fopen("F:\\english.txt","r+");if(fp==NULL){printf("File open error!!!\n");exit(0);}p=head;do{fprintf(fp,"%s",p->word);fputs(p->ch,fp);p=p->next;}while(p);fclose(fp);}void gongneng(){int choice;system("CLS");printf("\n\n\t\t\t欢迎使用本电子词典\n");printf("\t\t=======================================\n");printf("\n\t\t\t1 查找单词!\n\n\t\t\t2 修改单词!\n\n\t\t\t3 添加单词!\n\n\t\t\t4 删除单词!\n\n\t\t\t0 退出!\n\n\t\t\t请选择:");scanf("%d",&choice);switch(choice){case 1:chaxun();break;case 2:change();break;case 3:add(); break;case 4:shanchu();break;case 0:{printf("\n是否保存到文件\n\n\n1 是!\n0 否!\n请选择:");int m;scanf("%d",&m);if(m==1) write();exit(-1);}default:{printf("\n\n\t\t\t 输入有误!\n\n\t\t\t按任意键返回!");if(getch()!=127) gongneng();};break;}}int main(){system("color 1E"); //设计文本颜色read();gongneng();return 0;}。

相关主题