提高项目39-电子词典

来源:互联网 发布:mysql的insert语句 编辑:程序博客网 时间:2024/06/06 08:32

任务和代码:做一个简单的电子词典。在文件dictionary.txt中,保存的是英汉对照的一个词典,词汇量近8000个,英文与释义间用’\t’隔开。编程序,将文件中的内容读到两个数组e[]和c[]中,分别代表英文和中文,由用户输入英文词,显示中文意思。运行程序后,支持用户连续地查词典,直到输入“0000”结束

/*文件名:main.c作者:小风景完成日期:2016.7.18问题描述:做一个简单的电子词典。在文件dictionary.txt中,保存的是英汉对照的一个词典,词汇量近8000个,英文与释义间用’\t’隔开。编程序,将文件中的内容读到两个数组e[]和c[]中,分别代表英文和中文,由用户输入英文词,显示中文意思。运行程序后,支持用户连续地查词典,直到输入“0000”结束程序输出:*/#include <stdio.h>#include <stdlib.h>#include <string.h>int ReadInfo(char e[][20],char c[][20]);int SearchInfo(char e[][20],char *word,int countWord);int main(){    char e[8000][20] = {0};    char c[8000][20] = {0};  //定义存放中英文字段的二维数组    char word[20] = {0};     //定义输入的单词存放的数组    int countword = 0;    int index = 0;    countword = ReadInfo(e,c);   //讲文件中的数据读入数组之中    printf("文件中实际的单词数是:%d\n",countword);    while(1)  //使用while循环用于多次输入,在内部判断满足停止条件时跳出    {        printf("请输入要查找的单词:");        gets(word);        if(strcmp(word,"0000") == 0)    // 输入为0000时结束        {            printf("查找结束,谢谢使用!\n");            break;        }        index = SearchInfo(e,word,countword);        if(index != -1)        {            printf("您查找的单词%s的中文释义为:%s\n",e[index],c[index]);        }        else        {            printf("对不起,您查找的单词暂时没有被收录!\n");        }    }    return 0;}/*函数功能:将文件中的数据读入到数组之中*/int ReadInfo(char e[][20],char c[][20]){    int countWord = 0;    FILE *fp = NULL;    if((fp = fopen("dictionary.txt","r")) == NULL)    {        printf("open dictionary.txt error!\n");        exit(1);    }    while(!feof(fp))    {        fscanf(fp,"%s%s",e[countWord],c[countWord]);        countWord++;    }    fclose(fp);    return countWord;}/*函数功能:查找输入的单词返回值:如果找到单词,返回单词下标,否则返回0*/int SearchInfo(char e[][20],char *word,int countWord){    int imin = 0;    int imax = countWord - 1;    int mid = 0;    while(imin <= imax)    {        mid = (imin + imax) / 2;        if(strcmp(e[mid],word) > 0)        {            imax = mid - 1;        }        else if(strcmp(e[mid],word) < 0)        {            imin = mid + 1;        }        else        {            return mid;        }    }    return -1;}


程序运行结果:



总结:程序主要在于两个部分,一、将文件中的数据读到相应的数组中,二、用二分法进行查找。

文件下载路径:http://pan.baidu.com/s/1jGIMmBs

0 0
原创粉丝点击