diff --git a/电子英汉词典设计.cpp b/电子英汉词典设计.cpp new file mode 100644 index 0000000..d59caa8 --- /dev/null +++ b/电子英汉词典设计.cpp @@ -0,0 +1,265 @@ +#include +#include +#include +#include +#define LEN sizeof(struct info) +struct info +{ + char word[40]; + char mean[40]; +}; +struct info inf[400]; +int m=0; +void enter(); +void add(); +void Delete(); +void Edit(); +void enter() +{ + int i,m=0; + char n; + char to[40]; + printf("뵥: "); + scanf("%s",to); + FILE *fp; + if((fp=fopen("data.txt","ab+"))==NULL) + { + printf("ļ!\n"); + exit(1); + } + while(!feof(fp)) + { + if(fread(&inf[m],LEN,1,fp)==1) + { + if(strcmp(to,inf[m].word)==0) + { + printf("%s ",to); + printf("%s\n",inf[m].mean); + printf("Ƿ?"); + printf("Yyʾ,\n"); + fclose(fp); + return; + } + } + m++; + } + printf("޴˵!\n"); + printf("Ƿ?"); + printf("Yyʾ,\n"); + fclose(fp); + return; + if(m==0) + printf("ļ޼¼!\n"); + if((fp=fopen("data.txt","ab+"))==NULL) + { + printf("ļ!\n"); + return; + } + fclose(fp); +} +void add() +{ + int i; + char n[20]; + FILE *fp; + if((fp=fopen("data.txt","ab+"))==NULL) + { + printf("ļ!\n"); + exit(0); + } + printf("뵥: \n"); + scanf("%s",n); + while(!feof(fp)) + { + if(fread(&inf[m],LEN,1,fp)==1) + { + if(strcmp(n,inf[m].word)==0) + { + printf("д˵ʣǷ޸?\n"); + printf("1. 2. \n"); + scanf("%d",&i); + switch(i) + { + case 1: + Edit(); + case 2: + break; + } + printf("Ƿ?"); + printf("Yyʾ,\n"); + fclose(fp); + return; + } + } + m++; + } + printf("壺\n"); + scanf("%s",inf[m].mean); + strcpy(inf[m].word,n); + if(fwrite(&inf[m],LEN,1,fp)!=1) + { + printf("ܱ"); + } + else + { + printf("ѱ\nǷӣ"); + printf("Yyʾ,\n"); + } + fclose(fp); +} +void Delete() +{ + FILE *fp,*ft; + char n[40],k[40]; + int m=0,l=0,i; + printf("Ҫɾĵʣ\n"); + scanf("%s",n); + fp=fopen("data.txt","rb+"); + ft=fopen("123.txt","wb+"); + if(fp==NULL||ft==NULL) + { + printf("ļ!\n"); + exit(1); + } + printf("Ƿɾ˵ʣǣ\n"); + printf("\t1."); + printf("\t\t2.\n"); + scanf("%d",&i); + switch(i) + { + case 1: + while(fread(k,sizeof(k),1,fp)) + { + if(strcmp(k,n)!=0) + { + fwrite(k,sizeof(k),1,ft); + m++; + } + else + { + rewind(fp); + fseek(fp,sizeof(k)*(m+2),0); + } + } + fclose(fp); + fclose(ft); + remove("data.txt"); + rename("123.txt","data.txt"); + printf("ɾɹ"); + break; + case 2:break; + } + printf("Ƿ?\n"); + printf("Yyʾ,\n"); + return; +} +void Edit() +{ + int t,a,m=0; + char n[40],k[40]; + FILE *fp; + printf("Ҫ޸ĵĵʣ"); + scanf("%s",n); + if((fp=fopen("data.txt","rb+"))==NULL) + { + printf("ļ!\n"); + exit(1); + } + rewind(fp); + while(!feof(fp)) + { + if(fread(&inf[m],LEN,1,fp)==1) + if(strcmp(n,inf[m].word)==0) + { + printf("Ƿ޸壿\n"); + printf("\t1.."); + printf("\t\t2..\n"); + scanf("%d",&a); + switch(a) + { + case 1:printf("޸ĺ壺"); + scanf("%s",k); + strcpy(inf[m].mean,k); + t=sizeof(inf[m].mean); + fseek(fp,-t,1); + if(fwrite(&inf[m].mean,t,1,fp)==1) + printf("޸ijɹ!"); + break; + case 2: + break; + } + printf("Ƿ?\n"); + printf("Yyʾ,\n"); + fclose(fp); + return; + } + m++; + } + printf("޴˵\n"); + printf("Ƿ޸?\n"); + printf("yYز˵\n"); + fclose(fp); + return; +} +int main(void) +{ +int choice; +char n[2]; +do +{ +printf("\n\t =============================="); +printf("\n\t *ӭʹ޹ģӢʵ*"); +printf("\n\t ==============================\n"); +printf("\n\t ***********˵ʾ***********\n"); +printf("\n\t ********1*********\n"); +printf("\n\t ********2*********\n"); +printf("\n\t ********3ɾ*********\n"); +printf("\n\t ********4޸*********\n"); +printf("\n\t ********0˳ʵ*********\n"); +printf("ѡҪеĹܣ"); +scanf("%d",&choice); +switch(choice) +{ + case 1: + enter(); + scanf("%s",&n); + while(n[0]=='Y'||n[0]=='y') + { + enter(); + scanf("%s",&n); + } + break; + case 2: + add(); + m++; + scanf("%s",&n); + while(n[0]=='Y'||n[0]=='y') + { + add(); + m++; + scanf("%s",&n); + } + break; + case 3: + Delete(); + scanf("%s",&n); + while(n[0]=='Y'||n[0]=='y') + { + Delete(); + scanf("%s",&n); + } + break; + case 4: + Edit(); + scanf("%s",&n); + while(n[0]=='Y'||n[0]=='y') + { + Edit(); + scanf("%s",&n); + } + break; + } +}while(choice!=0); +return 0; +} +