句子成分分析(C++)

从键盘输入n个英文句子(“.”、“?”和“!”表示结束),每次输入一句,分别统计输出每个句子的大写字母个数、小写字母个数、数字个数、空格个数和其他字符个数,最后输出全部句子的统计结果。

题目:

从键盘输入n个英文句子(“.”、“?”和“!”表示结束),每次输入一句,分别统计输出每个句子的大写字母个数、小写字母个数、数字个数、空格个数和其他字符个数,最后输出全部句子的统计结果。

  • 每个句子不知道有多长,所以不适合用char []型存储,所以可以选择用string;
  • 由于不知道会输入多少个句子,所以可以选择动态数组vector来存储每个string;
  • 判断字符类型可以用库函数:<cctype>
  • 各种句子成分类型可以写在一个结构体内;

具体代码如下:

#include <iostream>
#include <vector>
#include <cctype>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
void analyse(string & );
struct component
{
    int uppercase;
    int lowercase;
    int digit;
    int space; 
    int other;
}cp;
int  main()
{
    cout << "请输入几个句子,每个句子以“.”,“?”或“!”结尾,以一个'.'作为最后一个句子。" << endl;
    std::vector<string> sentences;
    char ch=' ';
    string str("");
    while(str != ".")
    {
        str.erase(str.begin(),str.end());
        ch = ' ';
       while(ch!='.' && ch!='?' && ch!='!')
        {
            cin.get(ch);
            if (ch == '.' || ch == '?' || ch == '!')
                cin.get();
            str += ch;
        }
        sentences.push_back(str);
    }
    for (decltype(sentences.size()) i = 0; i < sentences.size(); i++)
    {
        if (i == sentences.size()-1)
            cout << endl << "总句子分析:" << endl;
        else
            cout << endl << "第" << i + 1 << "个句子分析:" << endl;
        analyse(sentences[i]);
    }
    
    cout << "bye!";
    system("pause");
    return 0;
}
void analyse(string & str)
{   
    static component sum_cpn={0};
    if (str==".")
    {
        cout << "总小写字母数:" << sum_cpn.lowercase << endl
             << "总大写字母数:" << sum_cpn.uppercase << endl
             << "总数字数:" << sum_cpn.digit << endl
             << "总空格数:" << sum_cpn.space << endl
             << "总其他字符数:" << sum_cpn.other << endl;
        return;
    }
    
    component cpn={0,0,0,0,0};
    for (decltype(str.size()) i = 0; i < str.size(); i++)
    {
        if (isupper(str[i]))
            {
                cpn.uppercase++;
                sum_cpn.uppercase++;
            }    
        else if (islower(str[i]))
            {
                cpn.lowercase++;
                sum_cpn.lowercase++;
            }
        else if (isdigit(str[i]))
            {
                cpn.digit++;
                sum_cpn.digit++;
            }
        else if (isspace(str[i]))
            {
                cpn.space++;
                sum_cpn.space++;
            }
        else
            {
                cpn.other++;
                sum_cpn.other++;
            }
    }
    cout << "小写字母数:" << cpn.lowercase << endl
         << "大写字母数:" << cpn.uppercase << endl
         << "数字数:" << cpn.digit << endl
         << "空格数:" << cpn.space << endl
         << "其他字符数:" << cpn.other << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值