资源简介
NULL
博文链接:https://touch-2011.iteye.com/blog/1038921
代码片段和文件信息
/******************************************************************************/
// 作者:刘海房
// 版本:第二版
// 时间:2011-04-07
/******************************************************************************/
/*
题目:
某英汉词典包含N个记录,每个记录有两个字段:一个是英文单词,另一个是中文解释。
各个记录按英文单词的词典顺序排列,各英文单词并不重复。输入英文单词和中文解释
(用空格隔开),若此单词已存在,则把这个单词的中文解释覆盖掉,若不存在,则把
此单词加入词典。(输入的大写字母全部转换成小写,词典中没有大写字母)。单词在文
本文件中的存储形式:(单词和中文有空格隔开)dictionary.txt
about 关于
boy 男
cat 猫
welcome 欢迎
*/
//注意事项:
//单词最多只能有一千个
//输入的时候英文单词和中文解释要用空格隔开
/*******************************************************************************/
#include
#include
#include
//函数声明
int Search(char words[][30]char word[30]int lowint high);//在字典中查找单词
void Insert(char words[][30]char word[30]int indexint length);//把单词插入到字典中
void Close_File(FILE *fp);//关闭文件
FILE * Open_File_W(FILE *fp);//以wt的方式打开文件
FILE * Open_File_R(FILE *fp);//以rt的方式打开文件
//主函数
void main()
{
//变量声明
int j=0;
int i=0;
FILE *fp;
int index=0;
char engwords[1000][30]; //存放词典中单词的英文部分
char words[1000][30]; //words存放词典
char engword[30]; //engword存放输入的单词的英文部分
char word[30]; //word存放输入的单词
//初始化engwords和words
for(i=0;i<1000;i++){
words[i][0]=‘\0‘;
engwords[i][0]=‘\0‘;
}
//打开文件并把文件中的单词都读入到words二维数组里面
fp=Open_File_R(fp);
i=0;
while(!feof(fp)){
fgets(words[i++]30fp);
}
Close_File(fp);
//取出词典中单词的英文部分
for(i=0;words[i][0]!=‘\0‘;i++){
for(j=0;words[i][j]!=‘ ‘;j++)
engwords[i][j]=words[i][j];
engwords[i][j]=‘\0‘;
}
//读取输入的单词并取出输入单词的英文部分
printf(“please input your wordseparation the english and chinese with empty bay:\n“);
gets(word);
j=0;
while(word[j]!=‘\0‘)
{
j++;
}
word[j]=‘\n‘;//在word后面加上换行符号因为词典中读出来的单词都有换行符
word[j+1]=‘\0‘;
j=0;
while(word[j]!=‘ ‘){
engword[j]=word[j];
j++;
}
engword[j]=‘\0‘;
//调用Search函数,在词典中查找是否存在输入的单词
strlwr(engword);//将大写字母转换成小写字母
index=Search(engwordsengword0i-1);
if(strcmp(engwordengwords[index])==0)//如果这个单词存在把这个单词覆盖掉
{
printf(“the word is exit!\n“);
strcpy(words[index]word);
}
else//此单词不存在,插入到词典中
{
printf(“the word is not exit! and alreadly insert into the dictionary!\n“);
Insert(wordswordindexi);
}
//更新文件dictionary.txt
fp=Open_File_W(fp);
for(i=0;words[i][0]!=‘\0‘;i++)
fputs(words[i]fp);
Close_File(fp);
}
//用二分查找法查找单词传入的参数是:词典,要查找的单词,词典首、尾。若找到则返回这个单词的下标,找不到,则返回这个单词应该插入的位置
int Search(char words[][30]char word[30]int lowint high)
{
int mid;
if(low>high)
return low;
mid=(low+high)/2;
if(strcmp(wordwords[mid])==0)
return mid;
if(strcmp(wordwords[mid])>0)
low=mid+1;
if(strcmp(wordwords[mid])<0)
high=mid-1;
Search(wordswordlowhigh);
}
//把单词插入到存放词典的数组中
void Insert(char words[][30]char word[30]int indexint length)
{
int i;
for(i=length-1;i>=index;i--)
strcpy(words[i+1]words[i]);
strcpy(words[index]word);
}
//以r属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4155 2011-04-08 20:31 源代码\1.c
文件 178 2011-04-06 20:49 源代码\document\a.cpp
文件 175 2011-04-07 21:36 源代码\document\a1.cpp
文件 142 2011-04-08 20:23 源代码\document\account.txt
文件 92 2011-04-08 20:32 源代码\document\dictionary.txt
文件 151 2011-04-10 21:55 源代码\document\file4.txt
文件 162 2011-04-10 22:21 源代码\document\file4_answer.txt
目录 0 2011-05-10 13:44 源代码\document
目录 0 2011-05-10 13:44 源代码
----------- --------- ---------- ----- ----
5055 9
相关资源
- C++头文件转delphi工具 + 源码
- 基于MFC的TCP调试助手源码95706
- C语言代码高亮html输出工具
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- 安科瑞智能电能表MODBUS通讯程序 VC6
- ESP8266-01S(使用手册+固件库+烧录工具
- 九齐单片机源码
- Qt画图工具源码(qgraphics draw)
- qt 串口助手源码
- modbus 主机源码
- 《LINUX C编程从初学到精通》光盘源码
- OLED驱动源码
- tm1650+stm32f103源码(board_tm1650.c)
- cheat engine 7.2源码
- CrySearch内存搜索器源码
- Qt 瑞士军刀开发工具
- FTP客户端源码(c++)
- MFC视频播放器源码(支持avi/wma/mp3等格
- CreatBitmap图片合成源码
- 使用Qt实现Excel读取工具
- Tcp自定义命令调试工具
- vs2008 can总线通讯源码
- 宠物管理系统课程设计(源码+数据库
- 对话框中实现浮动工具栏
- Windows扩展命令程序(源码)
- c语言实现火车订票系统(控制台)源
- 鼠标连点器(附源码)
- c++ 简易贪吃蛇源码
- 杀毒软件源码
川公网安备 51152502000135号
评论
共有 条评论