PHP Code:
// ta tao mot ham ho tro association
#ifndef BINTREE
#define BINTREE
#include <iostream.h>
#include <string.h>
#include<stdio.h>
typedef char Element[50];
typedef char KeyType[50];
int const PRE_ORDER=1;
int const IN_ORDER=2;
/* ****************************************************************** */
/* ***************** NODE CLASS **************************** */
/* ******************************************************************* */
class Node {
public:
Element data;
int posmean;
Node *left,*right;
Node()
{
left=0;
right=0;
};
Node(Element el,int x)
{
strcpy(data,el);
posmean=x;
left=0;
right=0;
};
};
/* ******************************************************************* */
/* ******************* BINSTREE CLASS ************************* */
/* ****************************************************************** */
class BinTree
{
private:
Node *root;
Node *current;
int error;
public:
BinTree()
{
root=0;
current=0;
error=0;
};
void insert(Element,int);
void remove(KeyType);
void find(KeyType);
void traverse(int order); // duyet cay
void traverse(FILE*,int order);
int retrieve(void);
void destroy(void);
int hasError(void);
private:
void preorder(Node *);
void preorder(FILE*,Node *);
void inorder(Node *);
void inorder(FILE*,Node *);
Node* findNode(KeyType key);
Node* findRighty(Node *);
Node* findParent(KeyType);
void destroyNode(Node *);
};
/* ******************************************************************* */
/* **************** BINSTREE CLASS IMPLEMENTATION **************** */
/* ******************************************************************* */
void BinTree::insert(Element el,int x)
{
if (root==0)
root= new Node(el,x);
else
{
Node *p=findNode(el); // khoi tao
if (p==0)
{
Node *parent=root;
if (p!=root)
parent=findParent(el);
if(strcmpi(parent->data,el)<0) // so sanh hai chuoi ko phan biet chu hoa va thuong
{
parent->right= new Node(el,x);
current=parent->right;
}
else
{
parent->left= new Node(el,x);
current=parent->left;
}
error=0;
}
else
error=1;
}
}
/* ******************************************************************* */
void BinTree::remove(KeyType key)
{
Node *p=findNode(key);
if (p==0) error=1; //Nut can xoa co hay ko
else
{
if (p->left==0 && p->right==0) //
{ //La' thi de xoa hon
if (p!=root)
{
Node *parent=findParent(key); // khoi tao lai
if (strcmpi(parent->data,key)<0)
parent->right=0;
else
parent->left=0;
}
else root=0;
delete(p);
error=0;
current=root; // nut hien tai la nut goc
}
else if (p->left!=0 && p->right==0)
{
if (p!=root)
{
Node *parent=findParent(key);
if (strcmpi(parent->data,key)<0)
parent->right=p->left;
else
parent->left=p->left;
}
else root=p->left;
delete(p);
error=0;
current=root;
}
else if (p->left==0 && p->right!=0)
{
if (p!=root)
{
Node *parent=findParent(key);
if (strcmpi(parent->data,key)<0)
parent->right=p->right;
else
parent->left=p->right;
}
else
root=p->right;
delete(p);
error=0;
current=root;
}
else
{
Node *righty=findRighty(p->left);
Node *parent=findParent(righty->data);
strcpy(p->data,righty->data);
p->posmean=righty->posmean;
if (parent != p)
parent->right = righty->left;
else
p->left = righty->left;
delete(righty);
error = 0;
current = root;
}
}
}
/* ******************************************************************* */
void BinTree::find(KeyType key)
{
Node *p=findNode(key);
if (p==0) error=1; // khong thay thi bay loi
else
{
error=0;
current=p; // nguoc lai tim thay
}
}
/* ******************************************************************* */
void BinTree::traverse(int order)
{
if (order == PRE_ORDER)
preorder(root); // duyet goc truoc
else if (order == IN_ORDER)
inorder(root); // duyet hai cay con truoc
else
cout << "no such order " << endl; // ko duyet duoc gi ca
}
/* ******************************************************************* */
void BinTree::traverse(FILE *f,int order) // Duyet cay
{
if (order == PRE_ORDER)
preorder(f,root);
else if (order == IN_ORDER)
inorder(f,root);
else
cout << "no such order " << endl;
}
/* ******************************************************************* */
int BinTree::retrieve() // lay vi tri cua nghia
{
return current->posmean;
}
/* ******************************************************************* */
void BinTree::destroy() // Huy nut goc
{
destroyNode(root);
}
/* ******************************************************************* */
int BinTree::hasError() // bay loi
{
return error;
}
/* ******************************************************************* */
void BinTree::preorder(Node *p) // function duyet theo goc truoc
{
if (p!=0)
{
cout << p->data << endl;
preorder(p->left);
preorder(p->right);
}
}
/* ******************************************************************* */
void BinTree::preorder(FILE *f,Node *p)
{
if (p!=0)
{
fprintf(f,"%s\t%d\n",p->data,p->posmean);
preorder(f,p->left);
preorder(f,p->right);
}
}
/* ******************************************************************* */
void BinTree::inorder(Node *p) // function duyet theo thu tu cay con truoc
{
if (p!=0)
{
preorder(p->left);
cout << p->data << endl;
preorder(p->right);
}
}
/* ******************************************************************* */
void BinTree::inorder(FILE *f,Node *p)
{
if (p!=0)
{
preorder(f,p->left);
fprintf(f,"%s\t%d\n",p->data,p->posmean);
preorder(f,p->right);
}
}
/* ******************************************************************* */
void BinTree::destroyNode(Node *p) // huy 1 nut
{
if (p!=0)
{
destroyNode(p->left);
destroyNode(p->right);
delete(p);
}
}
/* ******************************************************************* */
Node* BinTree::findParent(KeyType key)
{
Node *p=root;
Node *parent=0;
while (p!=0 && strcmpi(p->data,key)!=0)
{
parent=p;
if (stricmp(p->data,key)<0)
{
p=p->right;
}
else
{
p=p->left;
}
}
return parent;
}
/* ******************************************************************* */
Node* BinTree::findNode(KeyType key) // tim phan tu trong tu dien va cap nhat luon nghia
{
Node *p=root;
while(p!=0 && stricmp(p->data,key)!=0)
{
if (stricmp(p->data,key)<0) p=p->right;
else p=p->left;
}
return p;
}
/* ******************************************************************* */
Node* BinTree::findRighty(Node *p)
{
Node *Righty=p;
while (Righty->right!=0)
Righty=Righty->right;
return Righty;
}
#endif
//file thứ hai DictionaryApp.cpp
#include "BinTree.cpp"
#include <conio.h>
#include<stdio.h>
#include<stdlib.h>
#define DIR ".\\dict\\" //duong dan thu muc chua Tu Dien
char index[25] //ten cua file chua tu
,dict[25]; //ten cua file chua nghia
int count;
class DictionaryApp
{
private:
BinTree T;
public:
void selectDict();
void resetDict(); //xoa bo nho danh cho Tu Dien de load Tu Dien moi
void showMenu();
void loadDict();
void findWord();
void delWord();
void addWord();
};
void f_gets(char*,int,FILE*); //doc chuoi tu file ket thuc khi gap dau table
int f_seek_line(FILE*,int); //di chuyen con tro file di n-1 dong
/* *********************** Chuong trinh chinh ********************* */
void main()
{
DictionaryApp d;
char k;
count=0;
d.selectDict();
d.loadDict();
while (1)
{
d.showMenu();
k=getch();
switch (k)
{
case '1': d.findWord();
break;
case '2': d.addWord();
break;
case '3': d.delWord();
break;
case '4': d.resetDict();
d.selectDict();
d.loadDict();
break;
case 'x': exit(1);
default : break;
}
}
}
/* ******************************************************************* */
/* ******************************************************************* */
void DictionaryApp::selectDict()
{
char k;
cout <<"\n1 Tu Dien Thuat Ngu Tin Hoc";
cout <<"\n2 Tu Dien Dau Khi";
cout <<"\nLua Chon: ";
strcpy(index,DIR);
strcpy(dict,DIR);
while (k!='1' && k!='2')
{
k=getch();
switch (k)
{
case '1': strcat(index,"thuatngu.idx");
strcat(dict,"thuatngu.dict");
break;
case '2': strcat(index,"daukhi.idx");
strcat(dict,"daukhi.dict");
break;
// default: break;
}
}
}
void DictionaryApp::resetDict()
{
T.destroy();
}
/* ******************************************************************* */
void DictionaryApp::showMenu()
{
cout << "\n1 Tra Tu\n" ;
cout << "2 Them Tu Moi\n";
cout << "3 Xoa Tu\n";
cout << "4 Chon Tu Dien Khac\n";
cout << "x Ket Thuc Chuong Trinh\n";
cout << "Lua Chon Cua Ban: ";
}
/* ******************************************************************* */
void DictionaryApp::loadDict()
{
FILE *f;
Element el="";
int x;
if ((f=fopen(index,"rt"))==0)
{
cout << "Khong tim thay du lieu\n";
exit(1);
}
while (!feof(f))
{
f_gets(el,50,f);
if (strlen(el))
{
fscanf(f,"%d%*c",&x);
T.insert(el,x);
if (T.hasError()==1)
{ //Du lieu ko dung dang chuan(Co' the? van hoat dong dc)
cout << "Du lieu bi loi\n";
}
else count++;
}
}
fclose(f);
}
/* ******************************************************************* */
void DictionaryApp::findWord()
{
Element el="";
cout << "\nTu can tra: ";
gets(el);
T.find(el);
if (T.hasError()) cout <<"Khong tim thay\n";
else {
FILE *f;
if ((f=fopen(dict,"rt"))==0)
{
cout << "Khong doc duoc du lieu\n";
getch();
exit(1);
}
if (f_seek_line(f,T.retrieve()-1)!=0)
{
char mean[100];
fgets(mean,100,f);
fclose(f);
cout << el <<" : " << mean;
}
else cout << "Khong tim thay nghia\n";
}
}
/* ******************************************************************* */
void DictionaryApp::delWord()
{
Element el="";
cout << "\nTu can xoa: ";
gets(el);
if (el=="") return;
T.remove(el);
if (T.hasError()) cout << "Khong xoa duoc\n"; //khong co tu can xoa trong Tu Dien
else {
count--;
FILE *f=fopen(index,"wt");
if (f==0) {
cout << "Khong the ghi du lieu\n";
exit(1);
}
else T.traverse(f,PRE_ORDER);
fclose(f);
}
}
/* ******************************************************************* */
void DictionaryApp::addWord()
{
Element el="";
char mean[100];
cout << "\nTu can them: ";
gets(el);
if (el=="") return;
T.find(el);
if (!T.hasError())
{ //tim thay tu moi da co trong Tu Dien
cout << "Tu can them da co\n";
return;
}
cout << "Nghia cua tu: ";
gets(mean);
T.insert(el,count+1);
if (T.hasError()) cout <<"Khong them duoc tu moi\n";
else
{
count++;
FILE *f=fopen(index,"wt");
if (f==0)
{
cout << "Khong the ghi du lieu\n";
exit(1);
}
else T.traverse(f,IN_ORDER);
fclose(f);
if ((f=fopen(dict,"a+t"))==0)
{
cout << "Khong ghi duoc du lieu\n";
exit(1);
}
else fprintf(f,"\n%s",mean);
fclose(f);
}
}
/* ******************************************************************* */
void f_gets(char *s,int n,FILE *f)
{
int i=0;
char k;
while (!feof(f) && i<n)
{
k=getc(f);
if (k!='\t' && k!=10 && k!=EOF)
{
s[i]=k;
i++;
}
else break;
}
s[i]='\0';
}
int f_seek_line(FILE *f,int n)
{
int i=0;
char k;
while (i<n && !feof(f))
{
k=getc(f);
if (k==10) i++;
}
if (feof(f))
return 0;
return 0;
}