第十六周任务(三)

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Word
{
private:
	string english;
	string chinese;
	string xing;
public:
	Word(){};
	Word(string w,string c,string x):english(w),chinese(c),xing(x){}
	void set(string w,string c,string x);
	friend void save_to_file(Word *t); 
	friend void find(Word *t,string word);
};
void Word::set(string w,string c,string x)
{
	english=w;
	chinese=chinese;
	xing=xing;
}
void save_to_file(Word *t)
{
	Word w[8000];
	string english;
	string chinese;
	string xing;
	ifstream infile("dictionary.txt",ios::in);
	if(!infile)
	{
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for(int i=0;i<8000;++i)
	{
		infile>>english;
	    infile>>chinese;
		infile>>xing;
		t[i].set(english,chinese,xing);
	}
	infile.close();
}
void find(Word *t,string word)
{
	   
    int  f=0;  
    int  s=7999;  
    int mid=(f+s)/2;  
  
  
    while(f<s&&t[mid].english!=word)  
    {  
        if(t[mid].english<word) f=mid+1;  
        if(t[mid].english>word) s=mid-1;  
        mid=f+(s-f)/2;  
        if(t[mid].english==word)  
        {  
            cout<<t[mid].chinese<<endl;  
            cout<<t[mid].xing<<endl;  
        }     
  
    }  
    if(t[mid].english!=word)  
        cout<<"没有查到"<<endl;  
}  

int main()  
{  
    Word t[8000];  
    save_to_file(t);  
    string word;  
    do{  
        cin>>word;  
        find(t,word);  
    }while(word!="0000");  
    system("pause");  
    return 0;  
}  

你可能感兴趣的:(ios,String,File,System,include,任务)