计算段落中字母类型数量的程序,C++

Program that counts the number of types of letters in a paragraph, C++

本文关键字:程序 C++ 段落中 类型 计算      更新时间:2023-10-16

我想要的是有一个多行文本输入,并且能够计算输入中小写字母、大写字母、句点、逗号、空格、换行符和其他字符的数量。

我试图在while循环中使用getline中的一个字符串作为输入,每个标点符号类别都有一个运行计数。

我只是不知道如何真正计算出每行中每种字符类型有多少。给定一个字符串,如何计算每种类型的数量?

这是我到目前为止的代码(显然不完整):

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;
int main(){
    cout << "This program takes any number of sentences as inputs. " << endl;
    cout << "It will count the number of lower-case letters and upper-case letters. " << endl;
    cout << "It will also count the number of periods, exclamation marks, spaces, end-lines, etc. " << endl;
    cout << " " << endl;
    cout << "Please type your text, pressing enter whenever you wish to end a line. " << endl;
    cout << "Use the EOF key (CTRL + Z on Windows) when you are finished. " << endl;
    string InputString; // This is the string that will be used iteratively, for each line.
    int NumberOfLowerCase = 0;
    int NumberOfUpperCase = 0;
    int NumberOfSpaces = 0;     // spaces
    int NumberOfTabs = 0;       // tabs
    int NumberOfPeriods = 0;    // periods
    int NumberOfCommas = 0;     // commas
    int NumberOfOtherChars = 0; // other characters
    int NumberOfEnters = 0;     // end of line, will be incremented each loop
    do {
        getline(cin, InputString);   // input
        cout << InputString << endl; // filler just to test the input 
        NumberOfLowerCase = NumberOfLowerCase + 0   // I don't know what I should be adding
                                                    // (obviously not zero, that's just a filler)
    } while (!cin.eof() && cin.good());
    system("pause");
    return 0;
}

如果您只是想要唯一字符的数量,请使用set!你可以把你所有的角色都推到场景中,然后检查场景有多大,你就可以开始了!

如果你真的想知道每个字符有多少,你可以使用map(实际上它在引擎盖下使用了一个集合!)。有了地图,给定一些字符c,你就可以进行

std::map<char, int> counter;
//do stuff...
counter[c]++; //increment the number of character c we've found
//do more stuff...
std::cout << "Found " << counter['A'] << " A's!" << std::endl;

查看这些有用的函数。这里你会做什么:

std::string s = /*...*/;
for(auto c : s) {
    if(std::islower(c))      ++NumberOfLowerCase;
    else if(std::isupper(c)) ++NumberOfUpperCase;
    else if(c == ' ')        ++NumberOfSpaces;
    else if(c == 't')       ++NumberOfTabs;
    else if(c == '.')        ++NumberOfPeriods;
    else if(c == ',')        ++NumberOfCommas;
    else                     ++NumberOfOtherChars;
}

这里有一个我写得很快的非常简单的例子。当然,还有更好的方法,但这应该让你知道如何做到这一点。有一个问题:你是直接从控制台读取文件还是从istream读取?

int lowerCase = 0;
int upperCase = 0;
int spaces = 0;     // spaces
int tabs = 0;       // tabs
int newLines = 0;   // end of line, will be incremented each loop
int periods = 0;    // periods
int commas = 0;     // commas
int otherChars = 0;
// read from istream, char by char
for (char ch; cin >> noskipws >> ch;) {
    // test which classification or char ch is and increment its count
    if (islower(ch))
        ++lowerCase;
    else if (isupper(ch))
        ++upperCase;
    else if (ch == ' ')
        ++spaces;
    else if (ch == 't')
        ++tabs;
    else if (ch == 'n')
        ++newLines;
    else if (ch == '.')
        ++periods;
    else if (ch == ',')
        ++commas;
    else
        ++otherChars;
}
cout << "Number of characters of each type:n";
cout << "lowerCase:t" << lowerCase << 'n'
     << "upperCase:t" << upperCase << 'n'
     << "spaces:tt" << spaces << 'n'
     << "tabs:tt" << tabs << 'n'
     << "periods:t" << periods << 'n'
     << "commas:tt" << commas << 'n'
     << "newLines:t" << newLines << 'n'
     << "otherChars:t" << otherChars << 'n';