⑴ 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;
}