设为首页 加入收藏

TOP

单词统计
2014-11-23 23:33:51 来源: 作者: 【 】 浏览:4
Tags:单词 统计

#include
#include
#include
#include
#include
#include


using namespace std;


class CWord
{
string word;
public:
CWord(string word)
{
this->word = word;
}
string GetWord()const{return word;}
bool operator<(const CWord&w)const{//用于添加集合
return word < w.GetWord();
}
bool operator ==(const string &s)const{//用于查询
return word == s;
}
};


class CWordSet
{
set wordSet;
public:
bool AddString(string s)
{
wordSet.insert(CWord(s));
return true;
}
void show(ostream &os)
{
set::iterator it = wordSet.begin();
while(it!=wordSet.end())
{
os<<(*it).GetWord()<<"\t";
it++;
}
}
};


class CWordMap
{
mapwordMap;
public:
bool AddString(string s)
{
map::iterator it = wordMap.find(s);
if(it == wordMap.end())
{
pairp(CWord(s),1);
wordMap.insert(p);
}
else{
(*it).second+=1;
}
return true;
}


void Show(ostream &os)
{
map::iterator it = wordMap.begin();
while(it!=wordMap.end())
{
string ss = ((*it).first).GetWord();
int n = (*it).second;
os< it++;
}
}
};


int main()
{
CWordSet wordSet;
CWordMap wordMap;


int pos = 0;
string s = "";
string delimset = ",.";
ifstream in("a.txt");
while(!in.eof())
{
getline(in,s);
if(s=="")
{
continue;
}
pos = 0;
while((pos=s.find_first_of(delimset,pos))!=string::npos)
{
s.replace(pos,1," ");
}
istringstream stringeam(s);
while(!stringeam.eof())
{
stringeam>>s;
if(s=="")
continue;
wordSet.AddString(s);
wordMap.AddString(s);
}
}
in.close();
cout<<"单词集合为"< wordSet.show(cout);
cout< cout<<"单词及出现次数"< wordMap.Show(cout);
cout< return 0;
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇c标准函数库--->assert.h 下一篇翻转字符串

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: