如何用c语言做一个简单的英语词典

如何用c语言做一个简单的英语词典,第1张

//新建一个字典文本文件,命名为:dict.dat,内容格式如下: 或下载一个也可

//abandon v.抛弃,放弃

//abandonment n.放弃

//abbreviation n.缩写

//abeyance n.缓办,中止

//abide v.遵守

//ability n.能力

//able adj.有能力的,能干的

//abnormal adj.反常的,变态的

//aboard adv.船(车)上

//abolish v.废除,取消

//******************************

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct NODE

{

char e[20]

char c[50]

struct NODE* pnext

}

struct NODE* pN

struct NODE* pHead=NULL

struct NODE* pNode=NULL

struct NODE* pNext=NULL

struct NODE* pEnd=NULL

FILE* open_dict()

{

FILE *fp

if((fp=fopen("dict.dat","r"))==NULL)

{

printf("open dict error!\n")

return NULL

}

return fp

}

int getdata_from_dict(FILE* fp)

{

int i,j

char buf[50]

while(1)

{

pN=(struct NODE*)malloc(sizeof(struct NODE))

memset(pN->c,'\0',50)

memset(pN->e,'\0',20)

pN->pnext=NULL

if(fgets(buf,50,fp)==NULL)

{

fclose(fp)

break

}

sscanf(buf,"%s %s",pN->e,pN->c)

if(pHead==NULL)

{

pHead=pN

pNode=pN

continue

}

pNode->pnext=pN

pNode=pN

pEnd=pN

}

return 1

}

void write_dict()

{

FILE* fp=NULL

if((fp=fopen("dict.dat","w"))==NULL)

{

printf("open dict error!\n")

return

}

char buf[100]={'\0'}

pNode=pHead

while(1)

{

if(pNode==NULL)

{

break

}

fprintf(fp,"%s %s\n",pNode->e,pNode->c)

pNode=pNode->pnext

}

fclose(fp)

}

void del(char* char_del)

{

struct NODE* pDel=pHead

if(strcmp(pDel->e,char_del)==0)

{

pHead=pHead->pnext

write_dict()

return 

}

while(1)

{

if(strcmp(pDel->pnext->e,char_del)==0)

{

if(pDel->pnext->pnext!=NULL)

{

pDel->pnext=pDel->pnext->pnext

}

else

{

pDel->pnext=NULL

}

write_dict()

return 

}

else

{

pDel=pDel->pnext

if(pDel==NULL)

{

break

}

}

}

printf("\n未找到该单词!\n")

return

}

void add(char* char_add, char* char_add_chs)

{

pN=(struct NODE*)malloc(sizeof(struct NODE))

memset(pN->c,'\0',50)

memset(pN->e,'\0',20)

pN->pnext=NULL

strcpy(pN->e,char_add)

strcpy(pN->c,char_add_chs)

pEnd->pnext=pN

pEnd=pN

pNode=pN

write_dict()

}

int main(int argc, char *argv[]) 

{

FILE* fp=NULL

fp=open_dict()

if(fp==NULL)

{

printf("读取字典失败! 程序将退出!")

getchar()

return 1

}

getdata_from_dict(fp)

fclose(fp)

char buf[50]={'\0'}

while(1)

{

printf("\n\n{输入quit: 即退出, 输入del: 即删除, 输入add: 即增加}\n请输入英文单词 : ")

fflush(stdin)

scanf("%s",buf)

if(strcmp(buf,"quit:")==0)

{

break

}

else if(strcmp(buf,"del:")==0)

{

char char_del[20]={'\0'}

printf("请输入要删除的单词:")

fflush(stdin)

scanf("%s",char_del)

del(char_del)

continue

}

else if(strcmp(buf,"add:")==0)

{

char char_add[20]={'\0'}

char char_add_chs[100]={'\0'}

printf("\n请输入要增加的单词:")

fflush(stdin)

scanf("%s",char_add)

printf("\n请输入中文意思:")

fflush(stdin)

scanf("%[^\n]",char_add_chs)

add(char_add,char_add_chs)

continue

}

pNode=pHead

int find=0

while(pNode!=NULL)

{

if(strcmp(pNode->e,buf)==0)

{

printf("\n%s",pNode->c)

find=1

break

}

pNode=pNode->pnext

}

if(find==0)

{

printf("\n未找到!")

}

}

return 0

}

《Linux命令详解词典》百度网盘pdf最新全集下载:

链接:https://pan.baidu.com/s/1nfH8oVsI6-0McpiuvGdy5g

?pwd=v56d 提取码:v56d

简介:linux命令详解词典精选了381个Linux命令,详细罗列1729个范例,并提供3种查询索引方式。本书详细列出Linux命令的参数、功能说明、所属包、丰富的范例以及相关命令等信息,以统一的结构呈现,让Linux用户在短时间内找到需要的命令,了解命令的正确使用方式,并成功地将范例应用到实际中。  

/*基本的库函数*/

#include <conio.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define szWORD 32

#define szSTRN 224

#define szITEM sizeof(struct TItem)

char fileDict[szSTRN]

typedef struct TItem {

char word[szWORD]

char mean[szSTRN]

} Item

fpos_t lookup(char *word, char *mean)

{

FILE * f = 0Item i

int r = 0fpos_t p = 0

if(!word) return 0

f = fopen(fileDict, "rb")

if (!f) return 0

while(!feof(f)) {

fgetpos(f, &p)

r = fread(&i, szITEM, 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 append(void)

{

Item iFILE * f = 0fpos_t p = 0

memset(&i, 0, szITEM)

printf("请输入单词:")scanf("%s", i.word )

p = lookup(i.word, 0 )

if(p) {

printf("字典内已经有该单词记录!\n")

return

}

printf("请输入释义,按回车结束:")

fflush(stdin)

gets(i.mean )

f = fopen(fileDict, "ab")

fwrite(&i, szITEM, 1, f)

fclose(f)

printf("词条已新增\n")

}

void erase(void)

{

Item iFILE * f = 0fpos_t p = 0

memset(&i, 0, szITEM)

printf("请输入单词:")scanf("%s", i.word )

p = lookup(i.word, 0 )

if(p==0) {

printf("字典内没有该单词记录!\n")

return

}

p--

memset(&i, 0, szITEM)

f = fopen(fileDict, "rb+")

fsetpos(f, &p)

fwrite(&i, szITEM, 1, f)

fclose(f)

printf("词条已删除\n")

}

void edit(void)

{

Item iFILE * f = 0fpos_t p = 0

memset(&i, 0, szITEM)

printf("请输入单词:")scanf("%s", i.word )

p = lookup(i.word, 0 )

if(p==0) {

printf("字典内没有该单词记录!\n")

return

}

p--

printf("请输入释义,按回车结束(输入abort放弃修改):")

fflush(stdin)

gets(i.mean )

if(strstr(i.mean ,"abort")) {

printf("已放弃修改!\n")

return

}

f = fopen(fileDict, "rb+")

fsetpos(f, &p)

fwrite(&i, szITEM, 1, f)

fclose(f)

printf("词条已保存\n")

}

void query(void)

{

Item ifpos_t p = 0

memset(&i, 0, szITEM)

printf("请输入单词:")scanf("%s", i.word )

p = lookup(i.word, i.mean )

if(p==0) {

printf("字典内没有该单词记录!\n")

return

}

printf("【词条】%s\n【释义】%s", i.word , i.mean )

}

void set(void)

{

int cmd = 0

printf("当前字典为%s,需要改变吗(选择y或Y改变)?", fileDict)

cmd = getch()

if(cmd == 'y' || cmd == 'Y') {

printf("请输入字典文件名称(包含路径):")

scanf("%s", fileDict)

printf("设置成功!\n")

}

}

int main(int argc, char * argv[])

{

int cmd = 0

if(argc >1)

strcpy(fileDict, argv[1])

else

strcpy(fileDict, "c:\\dict.txt")

/*end if*/

for() {

printf("\n\

************************\n\

** 欢迎使用迷你字典!**\n\

************************\n\

** 0 - 设置字典 **\n\

** 1 - 查询词条 **\n\

** 2 - 新增词条 **\n\

** 3 - 编辑词条 **\n\

** 4 - 删除词条 **\n\

** 5 - 退出字典 **\n\

************************\n")

cmd = getch() - '0'

switch(cmd) {

case 0: set()break

case 1: query()break

case 2: append()break

case 3: edit()break

case 4: erase()break

default: return 0

}

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/9013934.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-23
下一篇2023-04-23

发表评论

登录后才能评论

评论列表(0条)

    保存