C语言实现电子英汉词典系统

2022-11-13 09:11:36 语言 系统 英汉词典

本文实例为大家分享了C语言实现电子英汉词典系统的具体代码,供大家参考,具体内容如下

一、设计功能(文章仅供参考)

a. 词条录入:即添加单词记录。 
b. 信息显示:将所有的单词按字母顺序显示。 
c. 词条修改:对已经输入的单词信息进行修改。 
d. 词条删除:删除某个单词记录。 
e. 单词查询: 输入单词英文拼写,输出该单词的中文释义。 
f. 信息保存:将单词信息保存到文件。 
g. 退出系统 

二、功能展示

三、思维导图

四、程序源码

#include <stdio.h>    //引入库函数
#include <stdlib.h>
#include <string.h>
#define szWord 50    //单词长度最大50
#define szSTRN 200    //释义长度最大200
#define szProject sizeof(struct Dictionary)
char fileDict[szSTRN];
typedef struct   Dictionary{
char word[szWORD];
char mean[szSTRN];
} Project ; //定义字典结构体,定义两个字符型变量单词和释义
fpos_t consult(char *word, char *mean)
{
FILE * f = 0; Project  i; 
int r = 0; fpos_t p = 0;
if(!word) return 0;
f = fopen(fileDict, "rb");
if (!f) return 0;
while(!feof(f)) {
  fgetpos(f, &p);
  r = fread(&i, szProject , 1, f);
  if(r < 1) break;
  if(i.word[0] == 0) continue;
  if(strcmp(i.word , word)) continue;
  if(mean) strcpy(mean, i.mean );
  fclose(f);
  return p+1; }
fclose(f);
return 0;}
void Search(void);
void Append(void);
void Delete(void);
void Update(void);
 
int main(int argk, char * argh[])
{
int m= 0;
if(argk>1) 
  strcpy(fileDict, argh[1]);
else
  strcpy(fileDict, "c:\\dict.txt");
for(;;) {
printf("\n\
--------------------\n\
欢迎使用电子英汉词典!\n\
--------------------\n\
1 - 查询词条\n\
2 - 新增词条\n\
3 - 删除词条\n\
4 - 修改词条\n\
5 - 退出词典\n\
--------------------\n");
  m = getchar() - '0';
  switch(m) {
case 1: Search();break;
case 2: Append();break;
case 3: Delete();break;
case 4: Update();break;
default : return 0;}
}
return 0;
}
 
void Search(void)
{
 Project i; fpos_t p = 0;
memset(&i, 0, szProject );
printf("please input the word  you want:"); scanf("%s", i.word );
p = consult(i.word, i.mean );
if(p==0) {
  printf("sorry do not find what you want!\n");
  return;
}
printf("单词:%s\n释义:%s", i.word , i.mean );
}
void Append(void)
{
Project  i; FILE * f = 0; fpos_t p = 0;
memset(&i, 0, szProject );
printf("please input the word you want:"); scanf("%s", i.word );
p = consult(i.word,0);
if(p)     {
  printf("sorry do not find what you want!\n");
  return;
}
printf("please giving the meaning,按确认结束:"); 
fflush(stdin);
gets(i.mean );
f = fopen(fileDict, "ab");
fwrite(&i, szProject , 1, f);
fclose(f);
printf("词条已新增\n");
}
void Delete(void)
{
 Project  i; FILE * f = 0; fpos_t p = 0;
memset(&i, 0, szProject );
printf("please input the word you want:"); scanf("%s", i.word );
p = consult(i.word, 0 );
if(p==0) {
  printf("sorry do not find what you want!\n");
  return;
}
p--;
memset(&i, 0, szProject);
f = fopen(fileDict, "rb+");
fsetpos(f, &p);
fwrite(&i, szProject , 1, f);
fclose(f);
printf("词条已删除\n");
}
void Update(void)
{
 Project  i; FILE * f = 0; fpos_t p = 0; 
memset(&i, 0, szProject );
printf("please input the word you want:"); scanf("%s", i.word );
p = consult(i.word, 0 );
if(p==0) {
  printf("sorry do not find what you want!\n");
  return;
}
p--;
printf("please giving the meaning,按确认结束(输入drop放弃修改):"); 
fflush(stdin);
gets(i.mean );
if(strstr(i.mean ,"drop")) {
  printf("已放弃修改!\n");
  return ;
}
f = fopen(fileDict, "rb+");
fsetpos(f, &p);
fwrite(&i, szProject , 1, f);
fclose(f);
printf("词条已保存\n");
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关文章