提高项目39-电子词典
2016-07-20 23:47
369 查看
任务和代码:做一个简单的电子词典。在文件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
/*
文件名: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
相关文章推荐
- android 代码实现控件之间的间距
- 如何组织构建多文件 C 语言程序(二)
- [Android]在代码里运行另一个程序的方法
- 如何写好 C main 函数
- 肯特·贝克:改变人生的代码整理魔法
- 网页恶意代码的预防
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- 高手写的Tracer-Flash代码调试类代码下载
- 关于C#中排序函数的总结
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- CSS代码缩写技巧
- 非主流Q-zOne代码代码搜集第1/2页
- ASP下经常用的字符串等函数参考资料
- CreateWeb.vbs 代码
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记