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.
jisuanji-1803-longzuxiang/课程设计源代码_电子英汉词典设计系统_计算机类1803...

469 lines
10 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();
void modify();
FILE *fp; /*定义文件指针*/
int num=0;
struct word
{
char en[100];
char ch[100];
char ex[100];
}s[200]; /*s为可录入单词数*/
//清屏
void clean()
{
int choice;
system("cls");
printf("\n") ;
menus();
scanf("%d",&choice);
printf("\n") ;
switch(choice)
{
case 1:
add();break;
case 2:
finds();break;
case 3:
clean();break;
case 4:
delet();break;
case 5:
modify();break;
case 0:
break;
}
}
//创建文件并打开文件
void add()
{
int a,b,c,choice;
char next='1';
char word[100];
FILE *fp;
char en[20];//要存入的单词
char ch[10];
char ex[20];
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",en);
strcpy(s[num].en,en);
a=fprintf(fp,"%s\t",s[num].en);
printf("\n请输入单词的释义:");
scanf("%s",ch);
strcpy(s[num].ch,ch);
b=fprintf(fp,"%s\t",s[num].ch);
printf("\n请输入单词的例句:");
getchar() ;
gets(ex);
strcpy(s[num].ex,ex);
c=fprintf(fp,"%s\n",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=='#')
{
printf("\n") ;
menus();
scanf("%d",&choice);
printf("\n") ;
switch(choice)
{
case 1:
add();break;
case 2:
finds();break;
case 3:
clean();break;
case 4:
delet();break;
case 5:
modify();break;
case 0:
break;
}
}
if(fclose(fp))//关闭文件并防止文件关闭失败。
{
printf("cann't close file");
exit(0);
}
}
//查找单词
void finds()
{
rewind(fp);
int i,l,choice,flag=0;
char find='2';
char n,y;
char w[100];
if((fp=fopen("f4.txt","r"))==NULL)//打开文件,和防止文件打开失败。
{
printf("cann't open file");
exit(0);
}
while(find=='2')
{
y='y';
printf("\n请输入需要查找的单词:");
while(y=='y')
{
char i=getch(); //getch读取字符不显示在显示屏上
if(i==27)
return ;/*27为ESC*/
else
{
if(i==8&&flag>0)/*8为BS*/
{
w[--flag]='\0';
system("cls");
printf("ESC:返回\n\nBackSpace:删除\n\n请输入要查的词:");
puts(w); //输出单词
printf("\n");
}
else
{
w[flag]=i;
system("cls");
printf("ESC:返回\n\nBackSpace:删除\n\n请输入要查的词:");
puts(w);
printf("\n");
flag++;
}
for(l=0;l<=10;l++)
{
fscanf(fp,"%s\t%s\t%s\n",s[l].en,s[l].ch,s[l].ex);
if(w[0]!='\0')
{
if(strncmp(s[l].en,w,flag)==0) //不同但有相同
printf("单词:%s\n",s[l].en);
if(strcmp(s[l].en,w)==0) //比较两个单词是否相同
{
printf("\n英文形式 中文形式 例句 ");
printf("\n %-12s%8s%12s\n",s[l].en,s[l].ch,s[l].ex);
printf("\nq是否继续查找单词 y---是的/n---不是的\n");
y=getch();
}
}
}
}
}
printf("\nWould you like to enter more items(Yes--2/No--除2以外的任意键)\n");
printf("\n返回主菜单按'#'号键") ;
fflush(stdin);//清空输入缓冲区
find=getch();
}
if(find=='#')
{
printf("\n") ;
menus();
scanf("%d",&choice);
printf("\n") ;
switch(choice)
{
case 1:
add();break;
case 2:
finds();break;
case 3:
clean();break;
case 4:
delet();break;
case 5:
modify();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\n",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\n",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");
delets=getch();
}
if(delets=='#')
{
printf("\n") ;
menus();
scanf("%d",&choice);
printf("\n") ;
switch(choice)
{
case 1:
add();break;
case 2:
finds();break;
case 3:
clean();break;
case 4:
delet();break;
case 5:
modify();break;
case 0:
break;
}
}
}
//修改单词信息
void modify()
{
int choice;
int o,g=1,j=0;//chooses代表选项标识g代表是否找到单词
char choose='5';
char x[100]; //查找错误单词
char y[100]; //正确英文形式
char z[100]; //正确中文形式
char l[100]; //正确例句
FILE *fp;
FILE *fw;
while(choose=='5')
{
if((fp=fopen("f4.txt","a+"))==NULL)//创建并打开文件,和防止文件打开失败。
{
printf("cann't open f4 file");
exit(0);
}
if((fw=fopen("linshi.txt","a+"))==NULL)//创建并打开文件,和防止文件打开失败。
{
printf("cann't open linshi file");
exit(0);
}
printf("\n请输入你要修改的单词英文形式:");
scanf("%s",x);
fseek(fp,0,SEEK_SET);
for(o=0;o<10;o++)//先找到该英文形式对应的序号
{
fscanf(fp,"%s\t%s\t%s\t",s[o].en,s[o].ch,s[o].ex);
if(strcmp(s[o].en,x)!=0)
{
fprintf(fw,"%s\t%s\t%s\n",s[o].en,s[o].ch,s[o].ex);
}
else
{
printf("\n请输入单词正确的英文形式:");
scanf("%s",y);
printf("\n请输入此单词正确的中文形式:");
scanf("%s",z);
printf("\n请输入此单词正确的例句:");
getchar();
gets(l);
j=fprintf(fw,"%s\t%s\t%s\n",y,z,l);
}
}
if(fclose(fp))//关闭文件并防止文件关闭失败。
{
printf("cann't close f4 file");
exit(0);
}
if(fclose(fw))//关闭文件并防止文件关闭失败。
{
printf("cann't close linshi file");
exit(0);
}
if(j)
printf("\n修改成功!\n");
else
{
printf("\n修改不成功!\n");
printf("\n没有这个单词!\n");
}
remove("f4.txt");
rename("linshi.txt","f4.txt");
printf("\n继续修改请选5 ’,返回菜单请选‘# :");
choose=getch();
}
if(choose=='#')
{
printf("\n") ;
menus();
scanf("%d",&choice);
printf("\n") ;
switch(choice)
{
case 1:
add();break;
case 2:
finds();break;
case 3:
clean();break;
case 4:
delet();break;
case 5:
modify();break;
case 0:
break;
}
}
}
//系统菜单
void menus()
{
printf("\n");
printf(" *****************************\n");
printf(" # #\n");
printf(" # 欢迎使用迷你英汉字典 #\n");
printf(" # #\n");
printf(" *****************************\n");
printf(" # #\n");
printf(" #0.退出 #\n");
printf(" #1.增加单词 #\n");
printf(" #2.查找单词 #\n");
printf(" #3.清屏 #\n");
printf(" #4.删除单词 #\n");
printf(" #5.修改单词 #\n");
printf(" #<请输入0-5中任意一个数字> #\n");
printf(" *****************************\n");
printf("\n");
}
int main()
{
int choice;
printf("\n") ;
menus();
scanf("%d",&choice);
printf("\n") ;
switch(choice)
{
case 1:
add();break;
case 2:
finds();break;
case 3:
clean();break;
case 4:
delet();break;
case 5:
modify();break;
case 0:
break;
}
return 0;
}