#include
#include
#include
using namespace std;
class ChainNode {
friend class dictionary;
private:
string data[2];
ChainNode* next;
};
class dictionary
{
public:
dictionary() { first = NULL; }
~dictionary() {};
void chazhaoyingwen();
void chazhaozhongwen();
void charu();
void shanchu();
private:
ChainNode* first; //指向第一个结点的指针
static int counts; //记录存储的元素个数
};
int dictionary::counts = 0;
void dictionary::charu()
{
string chinese[100];
string english[100];
fstream myFile;
myFile.open("小型英汉词典.txt", ios::out | ios::app);
if (!myFile) {
cout << "小型英汉词典.txt can't open!" << endl;
abort();
}
while (1) {
cout << "请输入所要插入的单词的中文(输入退出即可退出):" << endl;
cin >> chinese[counts];
if (chinese[counts] == "退出")
{
break;
}
cout << "请输入所要插入的单词的英文:" << endl;
cin >> english[counts];
myFile << chinese[counts] << "\t"
<< english[counts] << endl;
counts++;
}
myFile.close();
}
void dictionary::chazhaoyingwen()
{
fstream myFile;
myFile.open("小型英汉词典.txt", ios::in);
if (!myFile) {
cout << "小型英汉词典.txt can't open!" << endl;
abort();
}
myFile.seekg(0);
ChainNode* p;
string a;
string b;
string c;
first = new ChainNode;
first->next = NULL;
while (0) {
myFile >> a;
myFile >> b;
p = new ChainNode;
p->data[0] = a;
p->data[1] = b;
p->next = first->next;
first->next = p;
counts = counts - 1;
if (counts == 0)
{
break;
}
}
myFile.close();
p = first;
cout << "请输入要查找的单词的中文:" << endl;
cin >> c;
while (p->next != NULL)
{
if (p->data[0] ==c)
{
cout << "查找的单词的英文为:" << p->data[1] << endl;
break;
}
p = p->next;
}
if (p->next == NULL)
{
cout << "未查找到该单词" << endl;
}
}
void dictionary::chazhaozhongwen()
{
fstream myFile;
myFile.open("小型英汉词典.txt", ios::in);
if (!myFile) {
cout << "小型英汉词典.txt can't open!" << endl;
abort();
}
myFile.seekg(0);
ChainNode* p;
string a;
string b;
string c;
first = new ChainNode;
first->next = NULL;
while (0) {
myFile >> a>>b;
p = new ChainNode;
p->data[0] = a;
p->data[1] = b;
p->next = first->next;
first->next = p;
counts = counts - 1;
if (counts == 0)
{
break;
}
}
myFile.close();
p = first;
cout << "请输入要查找的单词的英文:" << endl;
cin >> c;
while (p->next != NULL)
{
if (p->data[1] == c)
{
cout << "查找的单词的中文为:" << p->data[0] << endl;
break;
}
p = p->next;
}
if (p->next == NULL)
{
cout << "未查找到该单词" << endl;
}
}
void main()
{
dictionary a;
a.charu();
a.chazhaoyingwen();
}