展开全部
以下程序是我背诵GRE单词时为查单词而编写的小型英汉字典。如果你需要的话,我可以把词典给你。
#include<iostream>
#include<fstream>
#include<windows.h>
#include<stdlib.h>
using namespace std;
int main()
{
int i;
bool T=true;
char Nword[20],word[121],line[121],t,explanation[121];
for(i=0;i<6;i++)
cout<<endl;
cout<<" 欢迎使用!"<<endl<<endl<<endl;
cout<<" 杨某制作"<<endl<<endl;
cout<<endl<<" 版权所有 盗版必究!"<<endl;
Sleep(1000);
er: while(T==true)
{
system("cls");
for(i=0;i<4;i++)
cout<<endl;
cout<<" 请输入要查询的单词:";
cin>>Nword;
FILE *fp;
if((fp=fopen("Dictionary.txt","r"))!=NULL)
{
fstream OpenDict("Dictionary.txt",ios_base::in);
for(i=0;i<16000;i++)//406
{
OpenDict.getline(word,120);
//if(i%2==0)
//{
if(!strcmp(word,Nword))
{
cout<<endl<<" "<<word<<endl<<endl;
break;
}
//}
}
if(i<16000)
{
OpenDict.getline(line,120);
cout<<" "<<line<<endl<<endl;
OpenDict.close();
}
else
{
cout<<endl<<" 单词未收录。是否添加该单词?(Y/N)";
fflush(stdin);
t=getchar();
fflush(stdin);
while(t!='y'&&t!='n'&&t!='Y'&&t!='N')
{
cout<<'\n'<<" 输入有误。请重新输入:";
t=getchar();
fflush(stdin);
}
if(t=='y'||t=='Y')
{
cout<<endl<<" 请输入单词含义:";
cin>>explanation;
ofstream WriteDict("Dictionary.txt",ios_base::out|ios_base::app);
WriteDict<<Nword<<endl;
WriteDict<<explanation<<endl;
WriteDict.close();
cout<<endl<<" 正在添加……"<<endl;
Sleep(400);
cout<<endl<<" 已添加。"<<endl;
cout<<endl<<" 是否继续查询?(Y/N)";
fflush(stdin);
t=getchar();
fflush(stdin);
while(t!='y'&&t!='n'&&t!='Y'&&t!='N')
{
cout<<'\n'<<" 输入有误。请重新输入:";
t=getchar();
fflush(stdin);
}
if(t=='y'||t=='Y')
{
T=true;
goto er;
}
else
{
T=false;
goto er;
}
}
else
{
cout<<endl<<" 是否继续查询?(Y/N)";
fflush(stdin);
t=getchar();
fflush(stdin);
while(t!='y'&&t!='n'&&t!='Y'&&t!='N')
{
cout<<'\n'<<" 输入有误。请重新输入:";
t=getchar();
fflush(stdin);
}
if(t=='y'||t=='Y')
{
T=true;
goto er;
}
else
{
T=false;
goto er;
}
}
}
}
else
{
//system("cls");
for(i=0;i<3;i++)
cout<<endl;
cout<<" 没有词典!"<<endl;
Sleep(1000);
goto re;
break;
}
fclose(fp);
fflush(stdin);
cout<<" 是否继续查询?(Y/N)";
t=getchar();
fflush(stdin);
while(t!='y'&&t!='n'&&t!='Y'&&t!='N')
{
cout<<'\n'<<" 输入有误。请重新输入:";
t=getchar();
fflush(stdin);
}
if(t=='y'||t=='Y')
{
T=true;
}
else
T=false;
}
re: system("cls");
fflush(stdin);
for(i=0;i<6;i++)
cout<<endl;
cout<<" 感谢使用!"<<endl<<endl;
cout<<" 杨某制作"<<endl<<endl;
cout<<endl<<" 版权所有 盗版必究!"<<endl;
cout<<'\n'<<" 按enter键退出。";
t=getchar();
fflush(stdin);
if(t!='\n')
{
cout<<'\n'<<" 请按enter键。";
t=getchar();
fflush(stdin);
}
return 0;
}
#include<iostream>
#include<fstream>
#include<windows.h>
#include<stdlib.h>
using namespace std;
int main()
{
int i;
bool T=true;
char Nword[20],word[121],line[121],t,explanation[121];
for(i=0;i<6;i++)
cout<<endl;
cout<<" 欢迎使用!"<<endl<<endl<<endl;
cout<<" 杨某制作"<<endl<<endl;
cout<<endl<<" 版权所有 盗版必究!"<<endl;
Sleep(1000);
er: while(T==true)
{
system("cls");
for(i=0;i<4;i++)
cout<<endl;
cout<<" 请输入要查询的单词:";
cin>>Nword;
FILE *fp;
if((fp=fopen("Dictionary.txt","r"))!=NULL)
{
fstream OpenDict("Dictionary.txt",ios_base::in);
for(i=0;i<16000;i++)//406
{
OpenDict.getline(word,120);
//if(i%2==0)
//{
if(!strcmp(word,Nword))
{
cout<<endl<<" "<<word<<endl<<endl;
break;
}
//}
}
if(i<16000)
{
OpenDict.getline(line,120);
cout<<" "<<line<<endl<<endl;
OpenDict.close();
}
else
{
cout<<endl<<" 单词未收录。是否添加该单词?(Y/N)";
fflush(stdin);
t=getchar();
fflush(stdin);
while(t!='y'&&t!='n'&&t!='Y'&&t!='N')
{
cout<<'\n'<<" 输入有误。请重新输入:";
t=getchar();
fflush(stdin);
}
if(t=='y'||t=='Y')
{
cout<<endl<<" 请输入单词含义:";
cin>>explanation;
ofstream WriteDict("Dictionary.txt",ios_base::out|ios_base::app);
WriteDict<<Nword<<endl;
WriteDict<<explanation<<endl;
WriteDict.close();
cout<<endl<<" 正在添加……"<<endl;
Sleep(400);
cout<<endl<<" 已添加。"<<endl;
cout<<endl<<" 是否继续查询?(Y/N)";
fflush(stdin);
t=getchar();
fflush(stdin);
while(t!='y'&&t!='n'&&t!='Y'&&t!='N')
{
cout<<'\n'<<" 输入有误。请重新输入:";
t=getchar();
fflush(stdin);
}
if(t=='y'||t=='Y')
{
T=true;
goto er;
}
else
{
T=false;
goto er;
}
}
else
{
cout<<endl<<" 是否继续查询?(Y/N)";
fflush(stdin);
t=getchar();
fflush(stdin);
while(t!='y'&&t!='n'&&t!='Y'&&t!='N')
{
cout<<'\n'<<" 输入有误。请重新输入:";
t=getchar();
fflush(stdin);
}
if(t=='y'||t=='Y')
{
T=true;
goto er;
}
else
{
T=false;
goto er;
}
}
}
}
else
{
//system("cls");
for(i=0;i<3;i++)
cout<<endl;
cout<<" 没有词典!"<<endl;
Sleep(1000);
goto re;
break;
}
fclose(fp);
fflush(stdin);
cout<<" 是否继续查询?(Y/N)";
t=getchar();
fflush(stdin);
while(t!='y'&&t!='n'&&t!='Y'&&t!='N')
{
cout<<'\n'<<" 输入有误。请重新输入:";
t=getchar();
fflush(stdin);
}
if(t=='y'||t=='Y')
{
T=true;
}
else
T=false;
}
re: system("cls");
fflush(stdin);
for(i=0;i<6;i++)
cout<<endl;
cout<<" 感谢使用!"<<endl<<endl;
cout<<" 杨某制作"<<endl<<endl;
cout<<endl<<" 版权所有 盗版必究!"<<endl;
cout<<'\n'<<" 按enter键退出。";
t=getchar();
fflush(stdin);
if(t!='\n')
{
cout<<'\n'<<" 请按enter键。";
t=getchar();
fflush(stdin);
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
个树是专门用来实现字典的。但是trie tree的删除操作比较麻烦。用二叉查找树可以实现,速度也可以很快。AVL tree只不过是平衡的二叉树,在字典这个应用上没有客观的速度提升,因为字典不会产生极端化的二叉树(链表)。
下面是我的二叉查找树的代码。二叉查找树的优点是实现容易,而且它的inorder traverse既是按照字母顺序的输出。
//binary search tree, not self-balancing
//by Qingxing Zhang, Dec 28,2009. prep for google interview
#include <iostream>
using namespace std;
struct BST
{
int data;
BST *left;
BST *right;
};
//runtime: O(logn) on average, O(n) worst case
bool search(BST *&root, int key)//return false if the key doesn't exist
{
if(root==NULL)
return false;
if(key < root->data)
return search(root->left,key);
else if(key > root->data)
return search(root->right,key);
else
return true;
}
//runtime: O(logn)on average, O(n) worst case
bool insert(BST *&root, int key)//return false if the key already exists
{
if(root==NULL)
{
BST *node = new BST;
node->data = key;
node->left = node->right = NULL;
root = node;
return true;
}
else if(key < root->data)
return insert(root->left,key);
else if(key > root->data)
return insert(root->right,key);
else
return false;
}
//runtime:O(logn) on average, O(n) worst case
bool remove(BST *&root,int key)//return false if the key doesn't exist.
{
if(root==NULL)//no such key
return false;
else if(key < root->data)
return remove(root->left,key);
else if(key > root->data)
return remove(root->right,key);
else//node found
{
if((root->left==NULL)&&(root->right==NULL))//no child(leaf node)
{
BST *tmp = root;
root = NULL;
delete tmp;
}
else if((root->left==NULL)||(root->right==NULL))//one child
{
BST *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
{
BST *tmp = root->right;
while(tmp->left!=NULL)
tmp = tmp->left;
int tmpdata = tmp->data;
remove(root,tmpdata);
root->data = tmpdata;
}
return true;
}
}
//runtime:O(n)
void inorder(BST *&node)
{
if(node!=NULL)
{
inorder(node->left);
cout << node->data << " ";
inorder(node->right);
}
}
//runtime:O(n)
void preorder(BST *&node)
{
if(node!=NULL)
{
cout << node->data << " ";
preorder(node->left);
preorder(node->right);
}
}
//runtime:O(n)
void postorder(BST *&node)
{
if(node!=NULL)
{
postorder(node->left);
postorder(node->right);
cout << node->data << " ";
}
}
int main()
{
bool b;
BST *root = NULL;
b = insert(root,1);
b = insert(root,3);
b = insert(root,7);
b = insert(root,5);
b = insert(root,77);
b = insert(root,10);
b = insert(root,4);
b = insert(root,13);
//inorder
cout << "In-order:";
inorder(root);
cout << endl;
//preorder
cout << "Pre-order:";
preorder(root);
cout << endl;
//postorder
cout << "Post-order:";
postorder(root);
cout << endl;
// search for 7
if(search(root,7))
cout << "7 found!" << endl;
else
cout << "7 doesn't exist!" << endl;
b = remove(root,7);
cout << "----------------" << endl;
//inorder
cout << "In-order:";
inorder(root);
cout << endl;
//preorder
cout << "Pre-order:";
preorder(root);
cout << endl;
//postorder
cout << "Post-order:";
postorder(root);
cout << endl;
if(search(root,7))
cout << "7 found!" << endl;
else
cout << "7 doesn't exist!" << endl;
return 0;
}
下面是我的二叉查找树的代码。二叉查找树的优点是实现容易,而且它的inorder traverse既是按照字母顺序的输出。
//binary search tree, not self-balancing
//by Qingxing Zhang, Dec 28,2009. prep for google interview
#include <iostream>
using namespace std;
struct BST
{
int data;
BST *left;
BST *right;
};
//runtime: O(logn) on average, O(n) worst case
bool search(BST *&root, int key)//return false if the key doesn't exist
{
if(root==NULL)
return false;
if(key < root->data)
return search(root->left,key);
else if(key > root->data)
return search(root->right,key);
else
return true;
}
//runtime: O(logn)on average, O(n) worst case
bool insert(BST *&root, int key)//return false if the key already exists
{
if(root==NULL)
{
BST *node = new BST;
node->data = key;
node->left = node->right = NULL;
root = node;
return true;
}
else if(key < root->data)
return insert(root->left,key);
else if(key > root->data)
return insert(root->right,key);
else
return false;
}
//runtime:O(logn) on average, O(n) worst case
bool remove(BST *&root,int key)//return false if the key doesn't exist.
{
if(root==NULL)//no such key
return false;
else if(key < root->data)
return remove(root->left,key);
else if(key > root->data)
return remove(root->right,key);
else//node found
{
if((root->left==NULL)&&(root->right==NULL))//no child(leaf node)
{
BST *tmp = root;
root = NULL;
delete tmp;
}
else if((root->left==NULL)||(root->right==NULL))//one child
{
BST *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
{
BST *tmp = root->right;
while(tmp->left!=NULL)
tmp = tmp->left;
int tmpdata = tmp->data;
remove(root,tmpdata);
root->data = tmpdata;
}
return true;
}
}
//runtime:O(n)
void inorder(BST *&node)
{
if(node!=NULL)
{
inorder(node->left);
cout << node->data << " ";
inorder(node->right);
}
}
//runtime:O(n)
void preorder(BST *&node)
{
if(node!=NULL)
{
cout << node->data << " ";
preorder(node->left);
preorder(node->right);
}
}
//runtime:O(n)
void postorder(BST *&node)
{
if(node!=NULL)
{
postorder(node->left);
postorder(node->right);
cout << node->data << " ";
}
}
int main()
{
bool b;
BST *root = NULL;
b = insert(root,1);
b = insert(root,3);
b = insert(root,7);
b = insert(root,5);
b = insert(root,77);
b = insert(root,10);
b = insert(root,4);
b = insert(root,13);
//inorder
cout << "In-order:";
inorder(root);
cout << endl;
//preorder
cout << "Pre-order:";
preorder(root);
cout << endl;
//postorder
cout << "Post-order:";
postorder(root);
cout << endl;
// search for 7
if(search(root,7))
cout << "7 found!" << endl;
else
cout << "7 doesn't exist!" << endl;
b = remove(root,7);
cout << "----------------" << endl;
//inorder
cout << "In-order:";
inorder(root);
cout << endl;
//preorder
cout << "Pre-order:";
preorder(root);
cout << endl;
//postorder
cout << "Post-order:";
postorder(root);
cout << endl;
if(search(root,7))
cout << "7 found!" << endl;
else
cout << "7 doesn't exist!" << endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
额 我
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询