You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dic_001/有限电子英汉词典.c

268 lines
4.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#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("按Y或y表示继续,按其它键返回\n");
fclose(fp);
return;
}
}
m++;
}
printf("无此单词!\n");
printf("是否继续?");
printf("按Y或y表示继续,按其它键返回\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("按Y或y表示继续,按其它键返回\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("按Y或y表示继续,按其它键返回\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("按Y或y表示继续,按其它键返回\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("修改成功!");
break;
case 2:
break;
}
printf("是否继续?\n");
printf("按Y或y表示继续,按其它键返回\n");
fclose(fp);
return;
}
m++;
}
printf("无此单词\n");
printf("是否继续修改?\n");
printf("输入y或Y继续操作输入其他返回菜单\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;
}