#include<iostream>
#include<fstream>
#include <string>
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 xieru();
void paixu();
void charu();
void shanchu();
private:
ChainNode* first; //指向第一个结点的指针
static int counts; //记录存储的元素个数
};
int dictionary::counts = 0;
void dictionary::xieru()
{
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];
counts++;
}
for (int i = 0; i < counts; i++)
{
myFile << chinese[i] << "\t"
<< english[i] << endl;
}
myFile.close();
}
void dictionary::chazhaoyingwen()
{
fstream myFile;
myFile.open("小型英汉词典.txt", ios::in|ios::binary);
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 (!myFile.eof())
{
myFile >> a;
myFile >> b;
p = new ChainNode;
p->data[0] = a;
p->data[1] = b;
p->next = first->next;
first->next = p;
}
myFile >> counts;
myFile.close();
p = first;
cout << "请输入要查找的单词的中文:" << endl;
cin >> c;
while (p)
{
if (p->data[0] ==c)
{
cout << "查找的单词的英文为:" << p->data[1] << endl;
break;
}
p = p->next;
}
if (p == NULL)
{
cout << "未查找到该单词" << endl;
}
}
void dictionary::chazhaozhongwen()
{
fstream myFile;
myFile.open("小型英汉词典.txt", ios::in | ios::binary);
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 (!myFile.eof())
{
myFile >> a>>b;
p = new ChainNode;
p->data[0] = a;
p->data[1] = b;
p->next = first->next;
first->next = p;
}
myFile.close();
p = first;
cout << "请输入要查找的单词的英文:" << endl;
cin >> c;
while (p)
{
if (p->data[1] == c)
{
cout << "查找的单词的中文为:" << p->data[0] << endl;
break;
}
p = p->next;
}
if (p == NULL)
{
cout << "未查找到该单词" << endl;
}
}
void dictionary::paixu()
{
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;
first = new ChainNode;
first->next = NULL;
while (!myFile.eof())
{
myFile >> a >> b;
p = new ChainNode;
p->data[0] = a;
p->data[1] = b;
p->next = first->next;
first->next = p;
}
myFile.close();
cout << "?" << endl;
p = first;
while (p)
{
cout << p->data[0] << "\t"
<< p->data[1] << endl;
p->next;
}
}
void dictionary::charu()
{
}
void main()
{
dictionary a;
//a.xieru();
//a.chazhaoyingwen();
//a.chazhaozhongwen();
a.paixu();
}
代码发的太乱了
注意发代码要用</>的文本形式,不然代码格式会出现错误。
while (myFile >> a >> b)
,不然会重复读取一次最后一行p = p->next;