当前位置:首页 » 编程语言 » c语言实现字典
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言实现字典

发布时间: 2022-04-20 08:58:55

c语言做小词典

/*基本的库函数*/
#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 = 0; Item 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, 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 i; FILE * f = 0; fpos_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 i; FILE * f = 0; fpos_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 i; FILE * f = 0; fpos_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 i; fpos_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;
}

⑵ 急急急!!!!用c语言实现字典 要求:用无序双向循环链表实现字典的基本操作如下

#include <stdafx.h> //这行是VC编译时要的头文件,你若TC就不要本行了
#include <stdio.h>

typedef struct dictnode{char *key; char *value; dictnode *pre; dictnode *next;} DictNode;

DictNode *pHead = NULL;

//(1)make:构造空的字典
int make()
{
if(pHead)return -1;
pHead = (DictNode*)malloc(sizeof(DictNode));
memset(pHead,0,sizeof(DictNode));
pHead->pre = pHead;
pHead->next = pHead;
return 0;
}

//(2)size:返回字的字典中记录数
int size()
{
int i;
DictNode*p;
for(i=0,p=pHead; p->next!=pHead; p=p->next,i++);
return i;
}

//(3)IsEmpy:如果字典为空则返回真,否则返回假
int IsEmpy()
{
return (pHead->pre==pHead);
}

//(4)Clear:将字典重置为空
void Clear()
{
DictNode* p,*ptmp;
for(p=pHead->next; p!=pHead ; )
{
ptmp = p;
p = p->next;
free(ptmp->key);
free(ptmp->value);
free(ptmp);
}
pHead->next = pHead->pre = pHead;//最后一个空节点也是第一个节点
}

//(5)Insert:插入记录到字典
int Insert(char *key,char*value)
{
DictNode* p, *ptmp;
if(!key||!value||!*key||!*value) return -100;//调用错误
ptmp = (DictNode*)malloc(sizeof(DictNode));
if(!ptmp) return -2; //内存不足这种极端的情况很难出现,但有可能
memset(ptmp,0,sizeof(DictNode));
ptmp->key =(char*) malloc(strlen(key)+1);
if(!ptmp->key){free(ptmp);return -2;} //内存不足这种极端的情况很难出现,但有可能
strcpy(ptmp->key,key);
ptmp->value = (char*)malloc(strlen(value)+1);
if(!ptmp->value){free(ptmp->key);free(ptmp); return -2;} //内存不足这种极端的情况很难出现,但有可能
strcpy(ptmp->value,value);
for(p=pHead->next; p->next!=pHead; p=p->next) if(p->key&&!strcmp(p->key,key)) return 1;//记录存在,插入失败
ptmp->next = pHead; pHead->pre = ptmp;
ptmp->pre = p; p->next = ptmp;
return 0;//操作成功返回0
}

//(6)remove:与给定关键字的记录相同则删除,该记录被返回,否则字典保持不变
DictNode* remove(char *key)
{
DictNode* p;
for(p=pHead->next; p!=pHead&&strcmp(p->key,key); p=p->next);
if(p==pHead) return NULL;
p->pre->next = p->next;
p->next->pre = p->pre;
p->pre = p->next = NULL;
return p;//结点p的空间(key value p三个)没有释放,外面要接收返回值并主动释放结点空间或做别的处理如插入另一表中等
}

//(7)IsPrensent:如果存在与给定关键字匹配的记录则返回真,否则返回假
int IsPrensent(char *key)
{
DictNode* p;
for(p=pHead->next; p!=pHead&&strcmp(p->key,key); p=p->next);
return (p!=pHead);
}

//(8)Find:如果存在与给定关键字相同的记录,则返回记录;如果没有找到,则返回空
DictNode* Find(char *key)
{
DictNode* p;
for(p=pHead->next; p!=pHead&&strcmp(p->key,key); p=p->next);
if(p==pHead) return NULL;
return p; //不要对Find返回的记录key值做变更,value值可以修改,value加长时要重新分配空间。因为记录还在字典链表中
}

void main()
{
const char *prtstr = "****************************";
DictNode* ptmp;
char keybuf[80];
char valuebuf[1024];
int c;
make();
while(1)
{
system("cls");//清屏
printf("%s 选择菜单 %s",prtstr,prtstr);
printf("\n\tF---词条查找\n\tI---插入新词条\n\tR---删除词条\n\tC---清空字典\n\tS---显示字典词条数\n\tQ---退出\n");
printf("请选择操作菜单:");
fflush(stdin);
c = getchar();
if(c>='a'&&c<='z') c -= ('a'-'A');//换大写
if(c!='F'&&c!='I'&&c!='R'&&c!='C'&&c!='S'&&c!='Q') continue;
fflush(stdin);
switch(c)
{
case 'F':
printf("词条查找:\n请输入Key值:");scanf("%s",keybuf);
fflush(stdin);
ptmp = Find(keybuf);
if(ptmp){printf("Key:%s Value:%s",ptmp->key,ptmp->value);}
else{printf("没找到词条:%s,你可以先选择插入这个新词条",keybuf);}
break;
case 'I':
printf("插入新词条:\n请输入Key值:");scanf("%s",keybuf);
fflush(stdin);
if(IsPrensent(keybuf)){printf("词条%s已存在\n",keybuf);}
else
{
printf("请输入它的解释:");gets(valuebuf);
if(!Insert(keybuf,valuebuf))printf("插入成功\n");
else printf("插入失败\n");
}
break;
case 'R':
printf("删除词条:\n请输入Key值:");scanf("%s",keybuf);
fflush(stdin);
ptmp = remove(keybuf);
if(ptmp)
{
free(ptmp->value);free(ptmp->key);free(ptmp);
printf("记录key:[%s]已删除\n",keybuf);
}
else
printf("未找到待删除的记录key:[%s]\n",keybuf);
break;
case 'C':
printf("清空字典:\n真的要清吗?\n请输入Yes以确认删除操作(首字母大写):");scanf("%s",keybuf);
fflush(stdin);
if(strcmp(keybuf,"Yes")){printf("放弃了清空操作\n");}
else {Clear();printf("Ok,你坚持操作,现在字典已清空了\n");}
break;
case 'S':
printf("显示字典词条数:\n当前词条总数:%d\n", size());
break;
case 'Q':
Clear(); free(pHead);
printf("Byebye");
exit(0);
}
printf("\n按回车键继续......");
fflush(stdin);
getchar();
}
}
//VC7.1 下调试通过,运行功能正常

⑶ 如何制作一个c语言的词典

以前写过一个类似的。
#include<stdio.h>
#include<string.h>
int main()
{
char *en[]={"airport",
"detective",
"parcel",
"diamond",
"stone",
"sand",
"flower",
"vegetable",
"pool",
"minute"};
char *ch[]={"飞机场",
"侦探",
"包裹",
"钻石",
"石头",
"沙",
"花",
"蔬菜",
"水池",
"分钟"};
int i,k=-1; //i作为循环变量,k保存字符串比较后的结果,初始值为-1
char s[20]; //s用来保存用户输入的单词
char **p=en; //定义一个指向指针的指针p,将它的值初始化为数组en的地址
printf("目前词库中仅收录了如下单词:\n");
for (i=0;i<10;i++)
{
puts(*p);
*p++;
}
printf("请输入一个单词,会给出该单词的解释:");
gets(s);
p=en;
for (i=0;i<10;i++)
{
if (strcmp(s,*p)==0)
{
k=i;
break;
}
*p++;
}
if (k!=-1)
printf("%s的意思是:%s\n",*p,ch[k]);
else
printf("该词库没有收录%s这个单词。\n",s);
return 0;
}

⑷ 用c语言编写电子英汉词典。

为了保证效率,建议使用哈希结构或者2岔树结构。
只需要提供插入删除和检索功能就可以了。
另外为字典的每个单元定义一个结构。
可以包含左右指针,自己的名词,及解释,还可以有其他东西。
然后实现树型结构的删除和检索功能就可以了。这个在数据结构的书上有吧。
然后写一个主函数就可以了。

⑸ C语言编写词典

用多级链表把单词读到内存中,最好是按字母顺序,第一层就按字母a-z,每个字母存一个节点,下面一层用单链表存单词(可以试试按字母的多少,分别建单独的链表,这样可以提高检索的效率吧)。图片随便画的,大概就这么个意思吧。希望对你有帮助。

⑹ 如何用c语言的数据结构做一个英语词典,要求是:用二叉树,能实现建立词典,查找插入删除单词

#include
<stdio.h>
#include
<malloc.h>
#include
<string.h>
#include
<ctype.h>
#define
MAXWORD
100
typedef
struct
tnode{
char
*word;
int
count;
struct
tnode
*left;
struct
tnode
*right;
//
tnode(char
*s,
int
w,
tnode
*p,
tnode
*q){
//
//
}
}*tnodeptr;
struct
tnode
*addtree(struct
tnode
*,
char
*);
void
deltree(struct
tnode
*,
char
*);
void
treeprint(struct
tnode*,
FILE
*fp,
int
n,
int
&m);
void
showMenu();
void
main()
{
int
N,
k;
FILE
*fp;
struct
tnode
*root;
char
word[MAXWORD],
txt[25];
root=NULL;
while
(1)
{
showMenu();
scanf("%d",
&N);
switch(N)
{
case
1:
printf("连续输入,字符
0
结束:\n");
while
(scanf("%s",
word)!=EOF)
{
if(word[0]=='0')
break;
if(isalpha(word[0]))
root=addtree(root,
word);
}
printf("单词已插入\n");
break;
case
2:
if(root==NULL)
{printf("NULL字典\n");
break;}
printf("字典:\n");
treeprint(root,
fp,
0,k
=
1);
break;
case
3:
printf("输入删除的单词\n");
scanf("%s",
word);
deltree(root,
word);
break;
case
4:
printf("输入保存文件名:
");
scanf("%s",
txt);
strcat(txt,
".txt");
fp=fopen(txt,
"w");
if(fp==NULL)
{printf("文件写错误!");
break;}
treeprint(root,
fp,
1,
k
=
1);
fclose(fp);
printf("以保存到%s
\n",
txt);
break;
case
0:
return
;
default:
printf("输入错误!");
break;
}
}
return
;
}
struct
tnode*
talloc();
char
*strp(char*);
struct
tnode
*addtree(struct
tnode
*p,
char
*w)
{
int
cond;
if
(p==NULL)
{
p=talloc();
p->word=strp(w);
p->count=1;
p->left=p->right=NULL;
}
else
if((cond=strcmp(w,
p->word))==0)
p->count++;
else
if
(cond<0)
p->left=addtree(p->left,
w);
else
p->right=addtree(p->right,
w);
return
p;
}
/**************************************************************************
*
分4种情况:
*
1.
叶子结点:直接删除;
*
2.
结点只有左孩子:将该左孩子连接到该结点的双亲;
*
3.
结点只有右孩子:将该右孩子连接到该结点的双亲;
*
4.
结点有左右孩子:
*
a.
将该结点左子树的最右结点与该结点互换,然后删除左子树的最右结点;
*
或者
*
b.
将该结点右子树的最左结点与该结点互换,然后删除右子树的最左结点。
***************************************************************************/
void
deltree(struct
tnode
*p,
char
*w)
{
int
co,
t=0;
struct
tnode
*q=NULL,
*r=NULL;
while
(p!=NULL
&&
(co=strcmp(w,
p->word))!=0)
{
if(co
<
0)
{q
=p;
p=p->left;
t
=1;}
else
{q
=p;
p=p->right;
t
=0;}
}
if(p==NULL)
printf("没有此单词!\n");
else
if
(p->left==NULL
&&
p->right==NULL)
//<1>
{
if(t==1)q->left=
NULL;
else
q->right=NULL;
}
else
if(p->left
&&
p->right==NULL)
//<2>
{
if(t==1)
q->left=p->left;
else
q->right
=p->left;
}
else
if(p->left==NULL
&&
p->right)
//<3>
{
if(t==1)q->left=p->right;
else
q->right=p->right;
}
else
//<4>
{
r=p->left;
while(r->right)r
=r->right;
r->right
=p->right;
if(t==1)q->left
=
r;
else
q->right
=
r;
}
printf("已删除:%s
\n",
w);
return
;
}
//------------------------
//print
//------------------------
void
treeprint(struct
tnode*
p,FILE
*fp,
int
n,
int
&m)
{
if(p!=NULL)
{
treeprint(p->left,
fp,
n,m);
if(n)fprintf(fp,
"%-4d%-4d%s\n",m++,p->count,p->word);
else
printf("%-4d%-4d%s\n",m++,p->count,p->word);
treeprint(p->right,
fp,
n,m);
}
}
struct
tnode
*talloc()
{
return
(struct
tnode*)malloc(sizeof(struct
tnode));
}
char
*strp(char*s)
{
char
*p;
p=(char*)malloc(strlen(s)+1);
if(p!=NULL)
strcpy(p,
s);
return
p;
}
void
showMenu()
{
printf("\n
输入选择\t
\
\n
1.
输入单词
\
\n
2.
查看字典
\
\n
3.
删除单词
\
\n
4.
保存字典
\
\n
0.
退出\n");
}

⑺ 运用c语言编写一个英汉字典~ 谢谢啦~先

原型:
int WINAPI icePub_dictionaryCodeTransfer2(char *strDictionaryFilename,char *strSrc,char *strCode,char *strFenge)
输入:strDictionaryFilename 字典文件名
strSrc 待处理单词
strFenge 字典里单词和code之间的分隔符字符串
输出:strCode strSrc对应信息
返回码:

原型:
int WINAPI icePub_dictionaryAddRecord(char *strDictionaryFilename,char *strSrc,char *strCode,char *strFenge)
输入:strDictionaryFilename 字典文件名
strSrc 待添加单词(如果存在则替换)
strCode strSrc对应信息
strFenge 字典里单词和code之间的分隔符字符串
输出:
返回码:

原型:
int WINAPI icePub_dictionaryDelRecord(char *strDictionaryFilename,char *strSrc,char *strFenge)
输入:strDictionaryFilename 字典文件名
strSrc 待删除单词
strFenge 字典里单词和code之间的分隔符字符串
输出:
返回码:

网络一个《icepubdll揭秘》有详细说明

⑻ C语言递归实现字典顺序枚

#include<stdio.h>
voidpermute(int);
#defineN7
inta[N];
intn=0;

voidmain()
{
inti;
for(i=0;i<N;i++)
{
a[i]=i+1;
}
permute(N);
printf("totalis:%d ",n);
}
voidpermute(intk)
{
inti,j,temp;
if(k==1)
{
for(i=0;i<N;i++)
{
printf("%d",a[i]);
}
printf(". ");
n++;
if(n%4==0)
{
printf(" ");
}
}
else
{
permute(k-1);
for(j=N-k+1;j<N;j++)
{
temp=a[N-k];
a[N-k]=a[j];
a[j]=temp;
permute(k-1);
temp=a[N-k];
a[N-k]=a[j];
a[j]=temp;
}
}

}

⑼ C语言实现的字典序排列

search1
for(i=len-2;i>0;i--) 数组是从0开始的

⑽ 用c语言编写英语字典

}
cout << endl;
int Scale = 5;
int dbn = 2;
cw.InitDecInfo2D(height, width, Scale, dbn);
double *dstcoef = new double[cw.m_msgCL2D.allSize];
cw.WaveDec2(s,dstcoef);

for (int i = 0; i < cw.m_msgCL2D.allSize; i++)
{
cout << dstcoef[i] << " ";
if ((i + 1) % 10 == 0)
cout << endl;
}
double *dst = new double[48];
for (int i = 0; i < 48; i++)
dst[i] = 0.0;
cw.WaveRec2(dstcoef, dst);
cout << endl; cout << endl;
for (int i = 0; i < 48; i++)
{
cout << dst[i] << " ";
if ((i + 1) % 8 == 0)
cout << endl;
}