西北农林科技大学OJ (C++)英文文本单词统计(STL)

Description

读入一篇英文文章,基于STL中的容器和算法(建议包含<map>、<algorithm>、<string>和<sstream>),删除所有标点符号,主要包括英文逗号“,”、句号“.”、分号“;”、感叹号“!”、问号“?”、双引号“"”和单引号“'”等,并将所有英文单词转化为小写,然后统计每个单词出现的频率并按“a-z”从小到大顺序输出结果。

Input

一段英文以exit为结束词的英文文本

Output

按从小到大顺序输出除标点外的小写单词及出现频率

Sample Input 1 

Saying Goodbye to Cambridge Again

Very quietly I take my leave
As quietly as I came here;
Quietly I wave goodbye
To the rosy clouds in the western sky.

The golden willows by the riverside
Are young brides in the setting sun;
Their reflections on the shimmering waves
Always linger in the depth of my heart.

The floating heart growing in the sludge
Sways leisurely under the water;
In the gentle waves of Cambridge
I would be a water plant!

exit

Sample Output 1

a 1
again 1
always 1
are 1
as 2
be 1
brides 1
by 1
cambridge 2
came 1
clouds 1
depth 1
floating 1
gentle 1
golden 1
goodbye 2
growing 1
heart 2
here 1
i 4
in 5
leave 1
leisurely 1
linger 1
my 2
of 2
on 1
plant 1
quietly 3
reflections 1
riverside 1
rosy 1
saying 1
setting 1
shimmering 1
sky 1
sludge 1
sun 1
sways 1
take 1
the 11
their 1
to 2
under 1
very 1
water 2
wave 1
waves 2
western 1
willows 1
would 1
young 1

Sample Input 2 

Stray Birds

"WHAT language is thine, O sea?"
"The language of eternal question."
"What language is thy answer, O sky?"
"The language of 'eternal' silence."

exit

Sample Output 2

answer 1
birds 1
eternal 2
is 2
language 4
o 2
of 2
question 1
sea 1
silence 1
sky 1
stray 1
the 2
thine 1
thy 1
what 2

**************************************************************************************************************

代码实现

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{

    string str[1000];
    string one="1";
    int i = 0;
    while (one != "exit")
    {
        cin >> one;
        if (one == "exit")
            break;
        
        for (string::iterator it = one.begin(); it != one.end(); it++)
        {
            if ((*it) >= 'A' && (*it) <= 'Z')
                *it = (*it) + 'a' - 'A';
        }
        for (string::iterator it = one.begin(); it != one.end(); )
        {
            if ((*it) == ',' || (*it) == '.' || (*it) == ';' || (*it) == '!' || (*it) == '?' || (*it) == '"' || (*it) == '\'')
            {
                it=one.erase(it);
            }
            else
            {
                ++it;
            }
        
        }
        str[i++] = one;
    }
    std::sort(str, str + i);
    int cnt = 1;
    for (int m = 0; m < i-1 ; m++)
    {
        if (str[m] == str[m + 1]) cnt++;
        else
        {
            cout << str[m] << " " << cnt << endl;
            cnt = 1;
        }
    }
    cnt = 1;
    for (int m = i-1; m >0; m--)
    {
        if (str[m] == str[m - 1]) cnt++;
        else
        {
            cout << str[m] << " " << cnt << endl;
            break;
        }
    }

    
    return 0;
}

  • 18
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值