”牛津“词典,作于大二下学期,linux小组布置的作业。
实现的功能有查询单词,以及以该单词为一部分的单词。
现在看来当时的写法很单纯,代码的复用性几乎没有,整个代码就在main函数中,看起来很糟糕,逻辑性很差,而用到了goto函数,使程序阅读性比较差。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define WORD 40
#define W 10000
//--------------------单词结构体-------------------------------------
struct dic
{
char Eword[WORD];
char Cword[WORD];
}words[W];
FILE *dictionary;
char filename[]="F:\\C程序\\词典\\牛津词典.txt";
void main()
{
char word[WORD],c,word1[WORD];
int i=0,j,k,l,p;
if((dictionary=fopen(filename,"r"))==NULL)
{
printf("不能打开文件,请确认磁盘已满或文件是否存在!");
getchar();
return;
}
rewind(dictionary);
while(!feof(dictionary))
{
fscanf(dictionary,"%s%s",words[i].Eword,words[i].Cword);
for(l=0;l<WORD;l++)
if(words[i].Cword[l]=='.'&&words[i].Cword[l+1]=='\0') //解决 汉译换行错误打入结构体问题
{
fscanf(dictionary,"%s",word1);
strcat(words[i].Cword,word1);
}
else if(words[i].Eword[l]=='.') //解决 汉译之间也有空格问题
{
strcat(words[i-1].Cword,words[i].Eword);
strcpy(words[i].Eword,words[i].Cword);
fscanf(dictionary,"%s",words[i].Cword);
}
i++;
}
fclose(dictionary);
k=i;
printf("%d",k);
for(i=0;i<k;i++)//将单词前*号去掉
{
if(words[i].Eword[0]=='*')
for(j=0;j<WORD;j++)
words[i].Eword[j]=words[i].Eword[j+1];
}
while(c!='n')//查找单词
{
p=0;
printf("请输入要查找的单词\n");
scanf("%s",word);
for(i=0;i<k;i++)
{
if(!strcmp(word,words[i].Eword))
{
printf("\n%s\n%s\n",words[i].Eword,words[i].Cword);
goto tt;
}
}
for(i=0;i<k;i++)//输入首字母或未完全单词 则打出与之类似单词 比如输入ab就打出以ab开头的单词
{
for(j=0;j<WORD;j++)
{
if(word[j]==words[i].Eword[j]||word[j]=='\0')
{
if(p==22)//最多显示10个单词
goto tt;
else if(word[j-1]==words[i].Eword[j-1]&&word[j]=='\0')//从第一个字母开始比较
{
printf("%d: %-15s%-10s\n",p+1,words[i].Eword,words[i].Cword);
p++;
}
else if(word[j]!=words[i].Eword[j])//如果不相等,则跳出对比下一个单词
break;
}
else if(i==k) //如果一直到不到,则输入有误
printf("\n输入有误\n");
else
j=WORD-1;
}
}
tt: getchar();
printf("继续查询?y/n\n");
scanf("%c",&c);
system("cls");
}
}