// 小型英汉词典.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Node
{
public:
string English;
string Chinese;
Node *left;
Node *right;
};
//runtime: O(logn) on average, O(n) worst case
string search(Node *&root, string English)//return false if the key doesn't exist
{
if(root==NULL)
return "单词不存在";
if(English < root->English)
return search(root->left,English);
else if(English > root->English)
return search(root->right,English);
else
return root->Chinese;
}
//runtime: O(logn)on average, O(n) worst case
string insert(Node *&root, string English,string Chinese)//return false if the key already exists
{
if(root==NULL)
{
Node *node = new Node;
node->English = English;
node->Chinese = Chinese;
node->left = node->right = NULL;
root = node;
return "插入成功";
}
else if(English < root->English)
return insert(root->left,English,Chinese);
else if(English > root->English)
return insert(root->right,English,Chinese);
else
return "插入失败";
}
//runtime:O(logn) on average, O(n) worst case
string remove(Node *&root,string English)//return false if the key doesn't exist.
{
if(root==NULL)//no such key
return "删除失败";
else if(English < root->English)
return remove(root->left,English);
else if(English > root->English)
return remove(root->right,English);
else//node found
{
if((root->left==NULL)&&(root->right==NULL))//no child(leaf node)
{
Node *tmp = root;
root = NULL;
delete tmp;
}
else if((root->left==NULL)||(root->right==NULL))//one child
{
Node *tmp = root;
if(root->left==NULL)
root = root->right;
else
root = root->left;
delete tmp;
}
else//two children:replace node value with inorder successor and delete that node
{
Node *tmp = root->right;
while(tmp->left!=NULL)
tmp = tmp->left;
string tmpEnglish = tmp->English;
remove(root,tmpEnglish);
root->English = tmpEnglish;
}
return "删除成功";
}
}
string createIndex(Node *&root)//create a dictionary index
{
string s;
char ch[50];
for(char i='a';i<='z';i++)
{
s=i;
s="lib/"+s+".txt";
ifstream infile(s);
for(;!infile.eof();)
{
int k=0;
infile.getline(ch,50);
int i;
for(i=0;i<50;i++)
{
if(ch[i]=='\0')
break;
if(ch[i]==' ')
k=i;
}
string judgeword(ch);
if(judgeword.length()!=0){
string English = judgeword.substr(0,k);
string Chinese = judgeword.substr(k+1,i);
insert(root,English,Chinese);}
}
infile.close();
}
return "词典打开结束";
}
void menu()
{
cout<<"_________________________"<<endl;
cout<<"| 使用说明 |"<<endl;
cout<<"|1、打开词典 2、查找单词|"<<endl;
cout<<"|3、删除单词 4、添加单词|"<<endl;
cout<<"|5、关闭词典 |"<<endl;
cout<<"|查找和删除只需输入英文 |"<<endl;
cout<<"|添加需要输入中文和英文 |"<<endl;
cout<<"|_______________________|"<<endl;
}
void fun()
{
Node *root = NULL;
int k;
string Chinese,English;
while(true)
{
cin>>k;
if(k==1)
{
cout<<createIndex(root)<<endl;
}else if(k==2){
cout<<"输入英文:"<<endl;
cin>>English;
cout<<search(root,English)<<endl;
}else if(k==3){
cout<<"输入英文:"<<endl;
cin>>English;
cout<<remove(root,English)<<endl;
}else if(k==4){
cout<<""<<endl;
cin>>English;
cout<<""<<endl;
cin>>Chinese;
cout<<insert(root,English,Chinese)<<endl;
}else if(k==5){
break;
}else{}
}
}
int main()
{
menu();
fun();
return 0;
}
小型英汉词典(黑白界面下的)使用了二叉搜索树C++
最新推荐文章于 2023-03-03 13:22:14 发布