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.

285 lines
6.2 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>
#include<process.h>
void add();
void finds();
void clean();
void menus();
void delet();
FILE *fp;
int num=0;
struct word
{
char en[100];
char ch[100];
char ex[100];
}s[200];
void clean()
{
system("cls");
}
void add()//创建文件并打开文件
{
int a,b,c,choice;
char next='1';
char word[100];
FILE *fp;
if((fp=fopen("f4.txt","a+"))==NULL)//创建并打开文件,和防止文件打开失败。
{
printf("cann't open file");
exit(0);
}
fseek(fp,0,SEEK_END);//定位到文件末尾
while(next=='1')
{
printf("\n请输入单词的英文:");
scanf("%s",s[num].en);
a=fprintf(fp,"%s\t",s[num].en);
printf("\n请输入单词的释义:");
scanf("%s",s[num].ch);
b=fprintf(fp,"%s\t",s[num].ch);
printf("\n请输入单词的例句:");
scanf("%s",s[num].ex);
c=fprintf(fp,"%s\t",s[num].ex);
if(a&&b&&c)
{
printf("\n输入成功\n");
num++;
printf("%d",num);
}
else
printf("\n输入失败\n");
printf("\nWould you like to enter more items(Yes--1/No--除1以外的任意键)\n");
printf("\n返回主菜单按'#'号键") ;
fflush(stdin);//清空输入缓冲区
next=getch();
}
if(next=='#')
{
menus();
scanf("%d",&choice);
switch(choice)
{
case 1:
add();break;
case 2:
finds();break;
case 3:
clean();break;
case 4:
delet();break;
case 0:
break;
}
}
if(fclose(fp))//关闭文件并防止文件关闭失败。
{
printf("cann't close file");
exit(0);
}
}
void finds()
{
rewind(fp);
int i,choice,flag=0;
char find='2';
char n;
char w[100];
if((fp=fopen("f4.txt","r"))==NULL)//打开文件,和防止文件打开失败。
{
printf("cann't open file");
exit(0);
}
while(find=='2')
{
printf("\n请输入需要查找的单词:");
scanf("%s",w);
for(i=0;i<=10;i++)//先找到该中文形式对应的序号
{
fscanf(fp,"%s\t%s\t%s\t",s[i].en,s[i].ch,s[i].ex);
if(strcmp(s[i].en,w)==0)
{
printf("\n英文形式 中文形式 例句 ");
printf("\n %-12s%8s%12s",s[i].en,s[i].ch,s[i].ex);
find=1;//退出程序
}
else
{
char *res = strstr(s[i].en,w);
if(res != NULL)
{
printf("\n你要查询的可能是:");
printf("%s\n", res);
find=2;
}
}
}
printf("\nWould you like to enter more items(Yes--2/No--除2以外的任意键)\n");
printf("\n返回主菜单按'#'号键") ;
fflush(stdin);//清空输入缓冲区
find=getch();
}
if(find=='#')
{
menus();
scanf("%d",&choice);
switch(choice)
{
case 1:
add();break;
case 2:
finds();break;
case 3:
clean();break;
case 4:
delet();break;
case 0:
break;
}
}
if(fclose(fp))//关闭文件并防止文件关闭失败。
{
printf("cann't close file");
exit(0);
}
}
//删除单词信息
void delet()
{
int choice ;
FILE *fp;
FILE *fp1;
char set[20];
int j,d,h,g,u=0;
int sum=0;
char delets='4';
if((fp=fopen("f4.txt","a+"))==NULL)//创建并打开文件,和防止文件打开失败。
{
printf("cann't open f4 file");
exit(0);
}
if((fp1=fopen("linshi.txt","a+"))==NULL)//创建并打开文件,和防止文件打开失败。
{
printf("cann't open linshi file");
exit(0);
}
while(delets=='4')
{
printf("\n请输入要删除的单词:\n");
scanf("%s",set);
for(j=0;j<=10;j++)//先找到该单词形式对应的序号
{
fscanf(fp,"%s\t%s\t%s\t",s[j].en,s[j].ch,s[j].ex);
if(strcmp(s[j].en,set)!=0)
{
fprintf(fp1,"%s\t",s[j].en);
d=sizeof(s[j].en);
fprintf(fp1,"%s\t",s[j].ch);
h=sizeof(s[j].ch);
fprintf(fp1,"%s\t",s[j].ex);
g=sizeof(s[j].ex);
u=d+g+h;
sum=sum+u;
}
else
{
rewind(fp);
fseek(fp,sum,0);
}
}
if(fclose(fp))//关闭文件并防止文件关闭失败。
{
printf("cann't close f4 file");
exit(0);
}
if(fclose(fp1))//关闭文件并防止文件关闭失败。
{
printf("cann't close linshi file");
exit(0);
}
remove("f4.txt");
rename("linshi.txt","f4.txt");
printf("\n删除成功\n");
printf("\n继续删除按4\n\n返回菜单按‘#\n\n退出按任意键\n\n");
delets=getch();
}
if(delets=='#')
{
menus();
scanf("%d",&choice);
switch(choice)
{
case 1:
add();break;
case 2:
finds();break;
case 3:
clean();break;
case 4:
delet();break;
case 0:
break;
}
}
}
void menus()
{
printf("\n");
printf(" *********************************************************\n");
printf(" # #\n");
printf(" # 请选择系统功能选项 #\n");
printf(" # #\n");
printf(" *********************************************************\n");
printf(" # #\n");
printf(" # 0.退出 1.输入单词 #\n");
printf(" # #\n");
printf(" # 2.查找单词 3.清屏 #\n");
printf(" # #\n");
printf(" # 4.删除单词 #\n");
printf(" # #\n");
printf(" *********************************************************\n");
printf("\n");
}
int main()
{
int choice;
menus();
scanf("%d",&choice);
switch(choice)
{
case 1:
add();break;
case 2:
finds();break;
case 3:
clean();break;
case 4:
delet();break;
case 0:
break;
}
return 0;
}