#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Dict
{
private:
ifstream f1;
ofstream f2;
string s,temp,word;
public:
Dict()
{
f1.open("英汉词典.txt",ios::in);
f2.open("单串英汉词典.txt",ios::out);
s="-";
while(!f1.eof())//用“-”将单词隔开
{
getline(f1,temp);
s=s+temp+"-";
}
f2<<s;
}
void find_w()
{
cout<<"input a word:";
cin>>word;
int n=0,n1,n2;
f2<<s;//取出f2中的单串文件字符串
while(!f2.eof())
{
if(s.find(word,n)!=string::npos)
{
n=s.find(word,n);//找到那个单词所在的位置
n1=s.rfind("-",n);//找到单词前面的“-”
n2=s.find("-",n);// 找到单词后面的“-”
n=s.find(" ",n);// 位置移到把单词后面的空格,目的是用来取出单词
if(s.substr(n1+1,n-n1-1)==word)
{
cout<<s.substr(n1+1,n2-n1-1)<<endl ;
break;
}
}
}
}
~Dict()
{
f1.close();
f2.close();
}
};
int main()
{
Dict d;
d.find_w();
return 0;
}