期末课程设计运行时出问题,恳求前辈能指正
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define maxlen 1000
#define strlen1 20
#define strlen2 40
void menu();/*显示控制菜单*/
void lookup();/*查询单词释义*/
void input();/*读取电子词典*/
void insert();/*插入词条*/
void del();/*删除词条*/
void modify();/*修改词条*/
void quit();/*退出词典程序*/
void conti();/*重复执行操作*/
int search1(int len,char word);
int search2(int len,char word);
char *words[maxlen];
char *expls[maxlen];
int len,flag=0;
int main()/*主函数*/
{
int num;
input();
while(!flag)
{
menu();
scanf("%d",&num);
switch(num)
{
case 1:lookup();break;
case 2:insert();break;
case 3:del();break;
case 4:modify();break;
case 5:quit();break;
}
}
}
void input()/*读取词典*/
{
int i=0;
FILE *fp;
char str[strlen1],temp[strlen2];
if((fp=fopen("f2.txt","r"))==NULL)
{
printf("file open error!\n");
exit(0);
}
fscanf(fp,"%s",str);
while(!feof(fp))
{
i=i+1;
words[i]=str;
fscanf(fp,"%s",temp);
expls[i]=temp;
fscanf(fp,"%s",str);
}
len=i;
fclose(fp);
}
void menu()
{
system("cls");
printf("电子词典程序\n");
printf("说明:在本系统中与执行某功能请输入相应数字\n");
printf("1-查询单词释义\n");
printf("2-插入词条\n");
printf("3-删除词条\n");
printf("4-修改词条\n");
printf("5-退出词典程序\n");
}
void lookup()/*查询单词释义*/
{
char word[strlen1];
int i;
flag=1;
while(flag)
{
printf("请输入查询单词:\n");
scanf("%s",word);
i=search1(len,word[strlen1]);
if(i==-1)
printf("无此单词!\n");
else
printf("%s\n",expls[i]);
conti();
}
}
void insert()/*插入词条*/
{
char word[strlen1];
char expl[strlen2];
int i,j;
flag=1;
while(flag)
{
printf("请输入待输入的单词和释义\n");
scanf("%s%s",word,expl);
i=search2(len,word[strlen1]);
if(i==-1)
printf("已有此单词!\n");
else
{
for(j=len;j>i;j--)
{
strcpy(words[j],words[j-1]);
strcpy(expls[j],expls[j-1]);
}
strcpy(words[i],word);
strcpy(expls[i],expl);
len++;
}
conti();
}
}
void del()/*删除词条*/
{
char word[strlen1];
int i,j;
flag=1;
while(flag)
{
printf("请输入待删除的单词:\n");
scanf("%s",word);
i=search1(len,word[strlen1]);
if(i==-1)
printf("无此单词:\n");
else
{
for(j=i;j<len;j++)
{
strcpy(words[j],words[j+1]);
strcpy(expls[j],expls[j+1]);
}
len--;
}
conti();
}
}
void modify()
{
char word[strlen1];
char expl[strlen2];
int i;
flag=1;
while(flag)
{
printf("输入待修改的单词:\n");
scanf("%s",word);
i=search1(len,word[strlen1]);
if(i==-1)
printf("无此单词:\n");
else
{
printf("输入释义:\n");
scanf("%s",expl);
strcpy(expls[i],expl);
}
conti();
}
}
void quit()/*退出词典程序*/
{
int i;
FILE *fp;
char ch;
printf("是否存入词典(y/n)?\n");
ch=getch();
if(ch=='Y'||ch=='y')
{
if((fp=fopen("f1.docx","w"))==NULL)
{
printf("file open error\n");
exit(0);
}
for(i=0;i<len;i++)
fprintf(fp,"%-20s%-40s\n",words[i],expls[i]);
fclose(fp);
}
flag=1;
}
int search1(int len,char word)
{
int l=0;
int r=len -1;
{
int m;
while(l<=r)
{
m=(l+r)/2;
char *temp;
temp=&word;
if(strcmp(temp,words[m])==0)
return m;
else if(strcmp(temp,words[m])>0)
l=m+1;
else
l=m-1;
}
return -1;
}
}
int search2(int len,char word)/*查找插入位置*/
{
int r=len-1;
int l=0;
{
int m;
while(l<r)
{
m=(l+r)/2;
char *tem;
tem=&word;
if(strcmp(tem,words[m])==0)
return m;
else if(strcmp(tem,words[m])>0)
l=m+1;
else
l=m-1;
}
return -1;
}
}
void conti()/*重复执行操作*/
{
char ch;
printf("继续执行吗(Y/N)?");
ch=getch();
if(ch=='Y'||ch=='y')
flag=1;
else
flag=0;
}