用C++设计一个英汉字典类 Dictionary, 每个字典对象包含若干个词条Item

2025-05-01 13:47:49
推荐回答(2个)
回答1:

class CItem
{
public:
CItem(string& eng,string& ch);
CItem(){m_EngWord = ""; m_ChPra = "";}
string Eng(){return m_EngWord;}
string Ch() {return m_ChPra;}
private:
string m_EngWord;
string m_ChPra;
};
CItem::CItem(string& eng,string& ch)
:
m_EngWord(eng),
m_ChPra(ch)
{
}

class CDictionary
{
public:
CItem FindItem(string& word);
bool AddItem(string& eng,string& ch);
// bool Save2File(string& file);
void PrintAll();
private:
map m_AllWord;
};

CItem CDictionary::FindItem(string& word)
{
if(m_AllWord.count(word) > 0)
return m_AllWord[word];
else
{
CItem temp;
return temp;
}
}
bool CDictionary::AddItem(string& eng,string& ch)
{
if(eng != "")
{
CItem temp(eng,ch);
m_AllWord[eng] = temp;
return true;
}
return false;
}
// bool CDictionary::Save2File(string& file)
// {
// return true;
// }
void CDictionary::PrintAll()
{
for(map::iterator it = m_AllWord.begin();it != m_AllWord.end();++it)
{
cout<<"Word:"<second.Eng()<cout<<"Parap:"<second.Ch()<cout<<"\n";
}
}

int main()
{
CDictionary dic;
string eng = "zhazha";
string ch = "渣渣";
bool re = dic.AddItem(eng,ch);
CItem te = dic.FindItem(eng);
te = dic.FindItem(ch);
dic.PrintAll();
system("pause");
}
是这意思吗?

回答2:

学厨师哪家强