1. 创作起源
在linux环境下编程时,经常会使用man手册查看一些系统API的用法,偶尔遇到一些不认识的单词,这时我的常规步骤(1):使用鼠标复制单词后,到网页或者本地词典软件查找其释义。然而一些词是经常遇到的,只是由于每次都没有刻意去记,所以每次都要重复上述过程。在这样的启发下,我想着写一个小程序,可以用来查找我当前遇到的这个单词,若这个单词是我第一次查找,则我使用常规步骤(1)添加它的中文释义,下次我再查找时就能快速查找,久而久之,我的索引速度就能提升,且这样比查网页或者词典简洁,按照我自己输入的中文,多查几次就能记住该单词
2. 系统保留命令
__exit : 退出查询
_n:不增加
3. 运行效果
windows环境下
linux环境下:
4.代码如下
//2019/06/13
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SSP_ERR 0
#define SSP_OK 1
#define MAXWORDSIZE 5000
//单词信息结点
typedef struct st_word
{
char ENString[30];
char CHstring[30];
struct st_word* pnextWord;
}word;
//管理头
typedef struct st_list
{
int num;//当前单词个数
int maxsize;
word* pword;
}word_head;
word_head* g_stWord_head;
/*
* 读历史文件,将上次保存的单词读取并插入到链表中,作查找用
* 文件格式#ENstring @CHstring
*/
int parseConf(FILE *fp)
{
char *p,*q;
char buffer[70] = {0};
int word_i = 0;
int counterEN;
int counterCH;
word *pword = NULL;
if(fp == NULL)
{
return SSP_ERR;
}
pword = g_stWord_head->pword;
while(fgets(buffer,70,fp)!= NULL)
{
counterEN = 0;
counterCH = 0;
p = q = NULL;
if(buffer[0]!='\0')
{
if( (p = strchr(buffer,'#')) && (q = strchr(buffer,'@')) )
{
p++;
while( *p!=' '&& p!=q && p )
{
pword[word_i].ENString[counterEN] = *p;
counterEN++;
p++;
}
pword[word_i].ENString[counterEN] = '\0';
q++;
while( *q!=' ' && q )
{
pword[word_i].CHstring[counterCH] = *q;
counterCH++;
q++;
}
pword[word_i].CHstring[counterCH] = '\0';
g_stWord_head->num++;
word_i++;
}
}
memset(buffer,0,sizeof(char)*70);
}
return SSP_OK;
}
/*
* 单词查找,根据字符串pString,查找它的中文释义
*
*/
word* WordSearch(char *pString)
{
int i = 0;
if(g_stWord_head->num > 0)
{
word* pword = g_stWord_head->pword;
for(;i<g_stWord_head->num;i++)
{
if(strcmp(pword[i].ENString,pString) == 0)
{
return &pword[i];
}
}
}
return NULL;
}
/*
* 新增一个单词对
*
*/
int insert_word(char *ENstring,char *CHstring)
{
strcpy(g_stWord_head->pword[g_stWord_head->num].ENString,ENstring);
strcpy(g_stWord_head->pword[g_stWord_head->num].CHstring,CHstring);
g_stWord_head->num++;
return SSP_OK;
}
/*
* 在退出程序的时候,遍历链表,将每一个节点的中英文信息组织成:#英文@中文格式并写入到文件中
*
*/
int ReWriteFile(FILE *fp)
{
word* pword = NULL;
int i =0;
char bufferline[70] = {0};
if(g_stWord_head->num > 0)
{
pword = g_stWord_head->pword;
for(;i<g_stWord_head->num;i++) //构建即将要写入文件的一行字符串
{
memset(bufferline,0,sizeof(char)*70);
strncpy(bufferline,"#",sizeof(char)*1);
strncpy(bufferline+1,pword[i].ENString,strlen(pword[i].ENString));
strncpy(bufferline+1+strlen(pword[i].ENString)," @",sizeof(char)*2);
strncpy(bufferline+1+strlen(pword[i].ENString)+2,pword[i].CHstring,strlen(pword[i].CHstring));
strncpy(bufferline+1+strlen(pword[i].ENString)+2+strlen(pword[i].CHstring),"\n",sizeof(char)*1);
fputs(bufferline,fp);
}
}
return SSP_OK;
}
int main()
{
char Enbuffer[30] = {0};
char Chbuffer[30] = {0};
char Modbuffer[30] = {0};
word *presult = NULL;
FILE *fp = NULL;
g_stWord_head = (word_head*)malloc(sizeof(word_head));
memset(g_stWord_head,0,sizeof(word_head));
g_stWord_head->pword = (word*)malloc(sizeof(word)*MAXWORDSIZE);
memset(g_stWord_head->pword,0,sizeof(word)*MAXWORDSIZE);
g_stWord_head->maxsize = MAXWORDSIZE;
fp = fopen("D:/linux/word.txt","a+");
fseek(fp,0,SEEK_SET); //SEEK_SET:文件头,SEEK_END:尾巴,SEEK_CUR:当前位置
parseConf(fp); //初始化历史文件
fclose(fp);
while(1)
{
memset(Enbuffer,0,sizeof(char)*30);
memset(Chbuffer,0,sizeof(char)*30);
printf("\r\n search:");
scanf("%s",Enbuffer);
if(strncmp(Enbuffer,"__exit",6) == 0)
{
if(g_stWord_head->num == 0)
{
return SSP_OK;
}
fp = fopen("D:/linux/word.txt","w");//重写文件
ReWriteFile(fp);
fclose(fp);
free(g_stWord_head->pword);
free(g_stWord_head); //释放内存
return SSP_OK;
}
presult = WordSearch(Enbuffer);//查找单词
if(presult == NULL) //查找不到就增加其中文释义,后续插入用
{
printf("\r\n add it?");
scanf("%s",Chbuffer);
if(strncmp(Chbuffer,"__exit",6) == 0)
{
if(g_stWord_head->num == 0)
{
return SSP_OK;
}
fp = fopen("D:/linux/word.txt","w");
ReWriteFile(fp); //退出时重写文件,保存我们本次可能新增的单词
fclose(fp);
free(g_stWord_head->pword);
free(g_stWord_head); //释放内存
return SSP_OK;
}
else if(strncmp(Chbuffer,"_n",2) == 0)
{
continue;
}
if(SSP_ERR == insert_word(Enbuffer,Chbuffer)) //插入新的单词对
{
printf("\r\n add failed!");
return SSP_ERR;
}
}
else
{
printf("\r\n%s",presult->CHstring); //查找到的情况下,输出其中文释义
}
}
}
```c