C++统计各个单词出现的次数
从屏幕输入一段英文文本,统计各个单词出现的次数,然后按照单词出现频率降序输出单词及其出现的次数。例如输入:Onelittle,twolittle,threelittler...
从屏幕输入一段英文文本,统计各个单词出现的次数,然后按照单词出现频率降序输出单词及其出现的次数。
例如输入:
One little, two little, three little rabbits.
输出:
little 3
one 1
two 1
…….
急求,在线等!
分可以再加,20够不?
要按照单词出现频率降序输出
标点没去掉啊 展开
例如输入:
One little, two little, three little rabbits.
输出:
little 3
one 1
two 1
…….
急求,在线等!
分可以再加,20够不?
要按照单词出现频率降序输出
标点没去掉啊 展开
展开全部
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
//定义存储单词和出现次数的结构体
typedef struct{
string word;
int num;
}count;
int main()
{
vector<count> v; //定义count类型的向量,动态存储count变量
count tempstr; //临时存储count变量
tempstr.num=0; //num初始化为0
ifstream in("english.txt"); //打开文件
string temp; //临时变量,存储文件的一行信息
string str; //临时变量,存储单个单词
int count=0; //记录单词字符个数
int j=0;
//按行读取文件,对每行信息截取单词并计数
while(getline(in,temp))
{
for(int i=0;i<temp.length ();i++)
{
if((temp[i]>='a'&&temp[i]<='z')||(temp[i]>='A'&&temp[i]<='Z'))
count++; //如果是英文字符,则计数加1
else if(count) //单词遇非英文字符时,计数终止,截取单词
{
str=temp.substr (i-count,count); //取子串(截取单词)
if(v.size ()) //若向量的长度不为0,则将单词与已有单词比较
{
for(j=0;j<v.size ();j++)
if(str.compare(v[j].word )==0)
{
v[j].num ++; //单词相同,则将相应单词的数目加1
count=0; //计数变量重新赋值为0,以便记录新的单词
break;
}
} //end if
if(j>=v.size ()) //单词第一次出现,将其添加至向量中
{
tempstr.word = str;
tempstr.num =1;
v.push_back (tempstr);
count = 0; //单词添加完毕,计数变量归0,记录新单词
} //end if
} //end elseif
} //end for
} //end while
//打印单词及出现次数
for(int i=0;i<v.size ();i++)
cout<<"the word is:"<<v[i].word<<", the times is:"<<v[i].num<<endl;
return 0;
}
展开全部
//不好意思,这个程序没有考虑标点,要考虑的话又要有大量的代码了。。
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include
<algorithm>
#include
<functional>
using
namespace
std;
class
wordcount
{
public:
string
word;
int
times;
wordcount(string
s,int
t)
{
word=s;
times=t;
}
int
operator
<
(wordcount
&w){return
times<w.times;}
int
operator
>
(wordcount
&w){return
times>w.times;}
};
int
UDgreater
(
wordcount
elem1,
wordcount
elem2
)
{
return
elem1
>
elem2;
}
int
main()
{
string
s;
map<string,int>
word;
vector<wordcount>
word_count;
while(cin>>s)//ctrl+z结束
word[s]++;
for(map<string,int>::iterator
itr=word.begin();itr!=word.end();itr++)
{
wordcount
w(itr->first,itr->second);
word_count.push_back(w);
}
sort(word_count.begin(),word_count.end(),UDgreater);
for(vector<wordcount>::size_type
i=0;i<word_count.size();i++)
{
cout<<word_count[i].word<<"
"<<word_count[i].times<<endl;
}
return
0;
}
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include
<algorithm>
#include
<functional>
using
namespace
std;
class
wordcount
{
public:
string
word;
int
times;
wordcount(string
s,int
t)
{
word=s;
times=t;
}
int
operator
<
(wordcount
&w){return
times<w.times;}
int
operator
>
(wordcount
&w){return
times>w.times;}
};
int
UDgreater
(
wordcount
elem1,
wordcount
elem2
)
{
return
elem1
>
elem2;
}
int
main()
{
string
s;
map<string,int>
word;
vector<wordcount>
word_count;
while(cin>>s)//ctrl+z结束
word[s]++;
for(map<string,int>::iterator
itr=word.begin();itr!=word.end();itr++)
{
wordcount
w(itr->first,itr->second);
word_count.push_back(w);
}
sort(word_count.begin(),word_count.end(),UDgreater);
for(vector<wordcount>::size_type
i=0;i<word_count.size();i++)
{
cout<<word_count[i].word<<"
"<<word_count[i].times<<endl;
}
return
0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我以前写了个比这个难的,不仅输出单词出现频率降序输出单词及其出现的次数。,而且可以找到每个单词在文章里的第几段,第几行,第几个单词,要的话给我发邮件 xiudewu520@126.com,标明要什么 ,不要,我也不想发 了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//不好意思,这个程序没有考虑标点,要考虑的话又要有大量的代码了。。
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include <algorithm>
#include <functional>
using namespace std;
class wordcount
{
public:
string word;
int times;
wordcount(string s,int t)
{
word=s;
times=t;
}
int operator < (wordcount &w){return times<w.times;}
int operator > (wordcount &w){return times>w.times;}
};
int UDgreater ( wordcount elem1, wordcount elem2 )
{
return elem1 > elem2;
}
int main()
{
string s;
map<string,int> word;
vector<wordcount> word_count;
while(cin>>s)//ctrl+z结束
word[s]++;
for(map<string,int>::iterator itr=word.begin();itr!=word.end();itr++)
{
wordcount w(itr->first,itr->second);
word_count.push_back(w);
}
sort(word_count.begin(),word_count.end(),UDgreater);
for(vector<wordcount>::size_type i=0;i<word_count.size();i++)
{
cout<<word_count[i].word<<" "<<word_count[i].times<<endl;
}
return 0;
}
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include <algorithm>
#include <functional>
using namespace std;
class wordcount
{
public:
string word;
int times;
wordcount(string s,int t)
{
word=s;
times=t;
}
int operator < (wordcount &w){return times<w.times;}
int operator > (wordcount &w){return times>w.times;}
};
int UDgreater ( wordcount elem1, wordcount elem2 )
{
return elem1 > elem2;
}
int main()
{
string s;
map<string,int> word;
vector<wordcount> word_count;
while(cin>>s)//ctrl+z结束
word[s]++;
for(map<string,int>::iterator itr=word.begin();itr!=word.end();itr++)
{
wordcount w(itr->first,itr->second);
word_count.push_back(w);
}
sort(word_count.begin(),word_count.end(),UDgreater);
for(vector<wordcount>::size_type i=0;i<word_count.size();i++)
{
cout<<word_count[i].word<<" "<<word_count[i].times<<endl;
}
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询