C++ Primer(第5版) 练习 11.4
练习 11.4 扩展你的程序,忽略大小写和标点。例如,“example.”、"exmaple,"和”Example"应该递增相同的计数器。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************
> File Name: ex11.4.cpp
> Author:
> Mail:
> Created Time: Tue 02 Apr 2024 09:42:21 AM CST
************************************************************************/
#include<iostream>
#include<iomanip>
#include<cctype>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
map<string, size_t> wordCount;
set<string> unique = {".", ","};
string word;
cout<<"Enter words: ";
while(cin>>word){
transform(word.begin(), word.end(), word.begin(), ::tolower);
if(ispunct(word[word.size()-1])){
word.erase(word.size()-1);
}
++wordCount[word];
if(cin.get() == '\n'){
break;
}
}
cout<<"Word Count: "<<endl;
for(const auto &w : wordCount){
cout<<"Word: "<<setw(8)<<left<<w.first<<" Count: "<<w.second<<endl;
}
return 0;
}