计算段落中字母类型数量的程序,C++
Program that counts the number of types of letters in a paragraph, C++
我想要的是有一个多行文本输入,并且能够计算输入中小写字母、大写字母、句点、逗号、空格、换行符和其他字符的数量。
我试图在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';
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址