
Nguyên bản được gửi bởi
hoanghon
Bài anh pót cho em cao quá hà ....năn nỉ mấy anh chi dùm em ...Từ điển đơn giản thôi hà ....chứ đừng như ct đó cao quá hà
Này thì có bài của neverland làm này.
PHP Code:
/* **************************************************************
* Ho ten: Nguyen Minh Danh *
* Lop: 05CT1 *
* Bai 2: Chuong trinh minh hoa bang bam de lam tu dien *
* *
************************************************************** */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define M 26
typedef struct TuDien
{
char tu[12];
char nghia[100];
};
typedef struct node
{
TuDien key;
struct node *next;
} Nodetype;
typedef Nodetype *Nodeptr;
Nodeptr Bucket[M];
void Initialize()
{
for(int i=0;i<M;i++)
Bucket[i]=NULL;
}
int HashFunc(char c)
{
if (c>=97) c=c-32;
return (c%65);
}
Nodeptr MakeNode(TuDien k)
{
Nodeptr p;
p=(Nodeptr) malloc(sizeof(Nodetype));
p->key=k;
p->next=NULL;
return p;
}
void InsertListOrder(Nodeptr &Head,Nodeptr G)
{
Nodeptr P, Q;
P = Head;
while (P != NULL)
{
if (strcmp(P->key.tu,G->key.tu)>0) break;
Q = P;
P = Q->next;
}
if (P == Head)
{
G->next = Head;
Head = G;
}
else
{
G->next = Q->next;
Q->next = G;
}
}
void Place(int b,TuDien k)
{
Nodeptr p,t;
p=Bucket[b];
t=MakeNode(k);
if (p==NULL) Bucket[b]=t;
else InsertListOrder(Bucket[b],t);
}
void Insert(TuDien k)
{
int b=HashFunc(k.tu[0]);
Place(b,k);
}
void TraverseBucket(int b)
{
Nodeptr p;
p=Bucket[b];
while (p!=NULL)
{
printf("%3s,",p->key.tu);
p=p->next;
}
}
void Traverse()
{
for(int b=0;b<M;b++)
{
printf("\nBucket[%d]:",b);
TraverseBucket(b);
}
}
void TraNghia(char *s)
{
int b=HashFunc(s[0]);
Nodeptr p = Bucket[b];
while (p!=NULL && strcmp(p->key.tu,s)!=0)
p=p->next;
if (p==NULL) printf("\n\nKhong tim thay \"%s\" trong tu dien",s);
else
printf("\n\nNghia cua tu \"%s\":%s",s,p->key.nghia);
}
void CapNhatTu(char *s,char *s1)
{
fflush(stdin);
int b= HashFunc(s[0]);
Nodeptr p=Bucket[b];
while (p!=NULL && strcmp(p->key.tu,s)!=0)
p=p->next;
if (p==NULL) printf("\n\nKhong tim thay \"%s\" trong tu dien",s);
else
{
strcat(p->key.nghia,",");
strcat(p->key.nghia,s1);
}
}
void Pop(Nodeptr &p) // lay phan tu dau xau
{
TuDien k;
Nodeptr q;
q=p;
p=p->next;
q->next=NULL;
k=q->key;
free(q);
}
void DelAfter(Nodeptr &q) //xoa nut sau q
{
Nodeptr p;
p=q->next;
q->next=p->next;
p->next=NULL;
free(p);
}
void XoaTu(char *s)
{
int b=HashFunc(s[0]);
Nodeptr p,q;
p=Bucket[b];
while (p!=NULL && strcmp(p->key.tu,s)!=0)
{
q=p;
p=p->next;
}
if (p==NULL) printf("\n\nKhong tim thay \"%s\" trong tu dien",s);
if (p==Bucket[b]) Pop(Bucket[b]);
else DelAfter(q);
}
void MENU()
{
printf("\n\n\t\t\t\tTU DIEN ANH-VIET");
printf("\n\nCac chuc nang chinh cua tu dien:");
printf("\n\n1.Them tu moi vao tu dien");
printf("\n\n2.Xoa mot tu khoi tu dien");
printf("\n\n3.Cap nhat tu");
printf("\n\n4.Tra nghia cua tu");
printf("\n\n0.Thoat khoi tu dien");
printf("\n\nBan chon chuc nang nao:");
}
int main()
{
char *s,*s1;
TuDien td;
int choice;
Initialize();
do
{
MENU();
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("THEM TU MOI VAO TU DIEN");
printf("\n\nNhap tu can them:");fflush(stdin);
gets(td.tu);
printf("\n\nNhap nghia cua tu \"%s\":",td.tu);
fflush(stdin);
gets(td.nghia);
Insert(td);
break;
case 2:
printf("\n\nNhap tu can xoa:");fflush(stdin);
gets(s);
XoaTu(s);
break;
case 3:
printf("CAP NHAT NGHIA CUA TU");
printf("\n\nNhap tu can cap nhat:");
gets(s);
printf("\n\nNhap nghia cap nhat cua \"%s\":");
fflush(stdin);
gets(s1);
CapNhatTu(s,s1);
break;
case 4:
printf("TRA NGHIA CUA TU");
printf("\n\nNhap tu can tra nghia:");fflush(stdin);
gets(s);
TraNghia(s);
break;
}
} while (choice!=0);
return 0;
}