daretofail87
17-11-2007, 13:37
mình có bài tập lớn về fan bảng băm và định xây dựng ứng dụng từ điển.bạn nào đã làm fan nay rui cho mình ý kiến với,nếu có code rui thì càng tốt.Mình gặp 1 số vấn đề sau
1.mình đã có 1 file từ điển rui (tiengviet.dat,tienganh.dat)thì khi minh tra từ or thao tác thì fai mở file và ghi vào bảng băm có đúng ko?
2.làm thế nào mình có thể chuyển dc chế độ từ tra tiếng việt sang tiêng anh thành tiếng anh sang tiêng việt
3.khi mình insert or delete 1 so từ thì mình fai lam thế nào để ghi vào file những thay đổi đó mà ko fai ghi lại toàn bộ mảng bucket (tức là lại ghi lại từng từ vào file)
Đây là chương trình của mình nhưng ko hiểu sao ko chạy.Nhờ các cao thủ chỉ bảo
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <alloc.h>
#include <stdlib.h>
struct mot
{
char tu[256];
char nghia[256];
mot *next;
};
typedef struct mot *MOT;
MOT bucket[26];
void open();
void close();
void initbucket();
void rechercher(char tg[256]);
void suppression(char tg[256]);
void insertion(char tg[256] , char ngh[256]);
void main()
{
int sg;
char tg[256],ngh[256];
clrscr();
initbucket();
open();
while(1)
{
printf("\n hay chon cac chuc nang sau:");
printf("\n1. Tra Tu");
printf("\n2. Xoa Tu");
printf("\n3. Them Tu");
printf("\n0. Thoat");
if(sg=='1')
{
printf("\n nhap tu can tra");
gets(tg);
rechercher(tg);
}
else if(sg=='2')
{
printf("\n nhap tu can xoa");
gets(tg);
suppression(tg);
}
else if(sg=='3')
{
printf("\n nhap tu can them");
gets(tg);
printf("\n nhap nghia cua tu");
gets(ngh);
insertion(tg,ngh);
}
else
{
exit(1);
clrscr();
}
}
printf(" \n cam on cac ban da su dung chuong trinh ");
close();
getch();
}
MOT getnode(void)//cap phat 1 nut moi
{
MOT p;
p=(MOT)malloc(sizeof(struct mot));
return(p);
}
void initbucket()//khoi dong bang bam
{
int b;
for(b=1;b<=26;b++)
bucket[b]=NULL;
}
void push( int b,char france[256], char vietnam[256] ) //them 1 nut moi vao dau bucket
{
MOT p;
p=getnode();
strcpy(p->tu,france);
strcpy(p->nghia,vietnam);
p->next=bucket[b];
bucket[b]=p;
}
void open() //lay du lieu tu file va ghi no vao bang bam
{
FILE *f,*g;
char tg1[256],tg2[256];
int i=0;
f=fopen("FRANCE.DAT","r");
g=fopen("VIETNAM.DAT","r");
while(!feof(f)&&!feof(g))
{
++i;
fgets(tg1,256,f);
fgets(tg2,256,g);
int b;
b=(tg1[1]-96);
push(b,tg1,tg2);
}
fclose(f);
fclose(g);
}
void rechercher(char france[256])//tra tu tieng phap
{
int b;
MOT p;
b=france[1]-96;
p=bucket[b];
while(!strcmp(p->tu,france)&&p!=NULL)
p=p->next;
if(p==NULL) printf("\n ko co tu nay trong tu dien");
else
printf("\n nghia cua tu nay la: %s",p->nghia);
}
void clearbucket(int b) //xoa bucket b
{
MOT p,q;
q=NULL;
p=bucket[b];
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
bucket[b]=NULL;
}
void clear() //xoa toan bo bang bam
{
int b;
for (b=1;b<=26;b++)
clearbucket(b);
}
void close()//cap nhat lai du lieu cua file tu bang bam va giai phong bo nho
{
int b;
MOT p;
FILE *f,*g;
f=fopen("FRANCE.DAT","w");
g=fopen("VIETNAM.DAT","w");
for(b=1;b<=26;b++)
{
p=bucket[b];
while(p!=NULL)
{
fputs(p->tu,f);
fputs(p->nghia,g);
p=p->next;
}
}
fclose(f);
fclose(g);
clear();
}
void suppression(char tg[256]) //xoa tu trong tu dien
{
MOT p,q;
int x= tg[1]-96;
p=bucket[x];
while((p!=NULL)&&!strcmp(p->tu,tg))
{
q=p;
p=p->next;
}
if (p==NULL) printf("\n khong tim thay tu nay");
else if (!strcmp(p->tu,tg))
{
q=p;
p=p->next;
free(q);
printf("\n da xoa tu nay khoi tu dien");
}
}
void insertion(char tg[256], char ngh[256])//them mot tu vao tu dien
{
MOT p;
int x=tg[1]-96;
push(x,tg,ngh);
}
bạn nào bit chỉ bảo cho mình sớm nhé.mình dang cần gấp.2 ngày nữa là fai nộp rui.thanks
1.mình đã có 1 file từ điển rui (tiengviet.dat,tienganh.dat)thì khi minh tra từ or thao tác thì fai mở file và ghi vào bảng băm có đúng ko?
2.làm thế nào mình có thể chuyển dc chế độ từ tra tiếng việt sang tiêng anh thành tiếng anh sang tiêng việt
3.khi mình insert or delete 1 so từ thì mình fai lam thế nào để ghi vào file những thay đổi đó mà ko fai ghi lại toàn bộ mảng bucket (tức là lại ghi lại từng từ vào file)
Đây là chương trình của mình nhưng ko hiểu sao ko chạy.Nhờ các cao thủ chỉ bảo
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <alloc.h>
#include <stdlib.h>
struct mot
{
char tu[256];
char nghia[256];
mot *next;
};
typedef struct mot *MOT;
MOT bucket[26];
void open();
void close();
void initbucket();
void rechercher(char tg[256]);
void suppression(char tg[256]);
void insertion(char tg[256] , char ngh[256]);
void main()
{
int sg;
char tg[256],ngh[256];
clrscr();
initbucket();
open();
while(1)
{
printf("\n hay chon cac chuc nang sau:");
printf("\n1. Tra Tu");
printf("\n2. Xoa Tu");
printf("\n3. Them Tu");
printf("\n0. Thoat");
if(sg=='1')
{
printf("\n nhap tu can tra");
gets(tg);
rechercher(tg);
}
else if(sg=='2')
{
printf("\n nhap tu can xoa");
gets(tg);
suppression(tg);
}
else if(sg=='3')
{
printf("\n nhap tu can them");
gets(tg);
printf("\n nhap nghia cua tu");
gets(ngh);
insertion(tg,ngh);
}
else
{
exit(1);
clrscr();
}
}
printf(" \n cam on cac ban da su dung chuong trinh ");
close();
getch();
}
MOT getnode(void)//cap phat 1 nut moi
{
MOT p;
p=(MOT)malloc(sizeof(struct mot));
return(p);
}
void initbucket()//khoi dong bang bam
{
int b;
for(b=1;b<=26;b++)
bucket[b]=NULL;
}
void push( int b,char france[256], char vietnam[256] ) //them 1 nut moi vao dau bucket
{
MOT p;
p=getnode();
strcpy(p->tu,france);
strcpy(p->nghia,vietnam);
p->next=bucket[b];
bucket[b]=p;
}
void open() //lay du lieu tu file va ghi no vao bang bam
{
FILE *f,*g;
char tg1[256],tg2[256];
int i=0;
f=fopen("FRANCE.DAT","r");
g=fopen("VIETNAM.DAT","r");
while(!feof(f)&&!feof(g))
{
++i;
fgets(tg1,256,f);
fgets(tg2,256,g);
int b;
b=(tg1[1]-96);
push(b,tg1,tg2);
}
fclose(f);
fclose(g);
}
void rechercher(char france[256])//tra tu tieng phap
{
int b;
MOT p;
b=france[1]-96;
p=bucket[b];
while(!strcmp(p->tu,france)&&p!=NULL)
p=p->next;
if(p==NULL) printf("\n ko co tu nay trong tu dien");
else
printf("\n nghia cua tu nay la: %s",p->nghia);
}
void clearbucket(int b) //xoa bucket b
{
MOT p,q;
q=NULL;
p=bucket[b];
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
bucket[b]=NULL;
}
void clear() //xoa toan bo bang bam
{
int b;
for (b=1;b<=26;b++)
clearbucket(b);
}
void close()//cap nhat lai du lieu cua file tu bang bam va giai phong bo nho
{
int b;
MOT p;
FILE *f,*g;
f=fopen("FRANCE.DAT","w");
g=fopen("VIETNAM.DAT","w");
for(b=1;b<=26;b++)
{
p=bucket[b];
while(p!=NULL)
{
fputs(p->tu,f);
fputs(p->nghia,g);
p=p->next;
}
}
fclose(f);
fclose(g);
clear();
}
void suppression(char tg[256]) //xoa tu trong tu dien
{
MOT p,q;
int x= tg[1]-96;
p=bucket[x];
while((p!=NULL)&&!strcmp(p->tu,tg))
{
q=p;
p=p->next;
}
if (p==NULL) printf("\n khong tim thay tu nay");
else if (!strcmp(p->tu,tg))
{
q=p;
p=p->next;
free(q);
printf("\n da xoa tu nay khoi tu dien");
}
}
void insertion(char tg[256], char ngh[256])//them mot tu vao tu dien
{
MOT p;
int x=tg[1]-96;
push(x,tg,ngh);
}
bạn nào bit chỉ bảo cho mình sớm nhé.mình dang cần gấp.2 ngày nữa là fai nộp rui.thanks