您的位置:首页 > 其它

使用 map 关联容器 统计文章中原音字母的个数

2011-04-22 23:34 363 查看
开始这样写
#include <iostream>    // 数据流输入/输出
#include <string>      // 字符串类
#include <algorithm>   // STL 通用算法
#include <vector>      // STL 动态数组容器
#include <fstream>     //文件输入/输出
#include <cstdio>      // 定义输入/输出函数
#include <cctype>      // 字符处理
#include <sstream>
#include <map>
using namespace std;
int main()
{
string IN_TEXT = "AEIOU, or A.E.I.O.U., was a symbolic device utilised by the Habsburg emperors. "
"Emperor Frederick III (1415-93), who had a fondness for mythical formulae,  "
"habitually signed buildings and objects with the acronym.[1] Frederick III did not  ";
istringstream instr(IN_TEXT);   // instr 代替 cin 人工输入测试字符
char ch;
map<char, int> letterCount;
while (instr >> ch) {
if (isalpha(ch)) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
++letterCount[ch];
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
++letterCount[ch];
}
}
int aCnt, eCnt, iCnt, oCnt, uCnt;   // 定义计数器,准备读出原音计数
aCnt = eCnt = iCnt = oCnt = uCnt = 0;
if (letterCount.count('a'))         // 读出小写 a 计数
aCnt = letterCount['a'];
if (letterCount.count('A'))         // 加上大写 A 计数
aCnt += letterCount['A'];
if (letterCount.count('e'))
eCnt = letterCount['e'];
if (letterCount.count('E'))
eCnt += letterCount['E'];
if (letterCount.count('i'))
iCnt = letterCount['i'];
if (letterCount.count('I'))
iCnt += letterCount['I'];
if (letterCount.count('o'))
oCnt = letterCount['o'];
if (letterCount.count('O'))
oCnt += letterCount['O'];
if (letterCount.count('u'))
uCnt = letterCount['u'];
if (letterCount.count('U'))
uCnt += letterCount['U'];
cout << "Number of vowel a and A:/t" << aCnt << endl;
cout << "Number of vowel e and E:/t" << eCnt << endl;
cout << "Number of vowel i and I:/t" << iCnt << endl;
cout << "Number of vowel o and O:/t" << oCnt << endl;
cout << "Number of vowel u and U:/t" << uCnt << endl;
return 0;
}


读出结果比较麻烦,后来 写了个

int getCount(std::map<char, int> &lCount , char ch);

读出的方法 简单点,但是还是不太方便

#include <iostream>    // 数据流输入/输出
#include <string>      // 字符串类
#include <algorithm>   // STL 通用算法
#include <vector>      // STL 动态数组容器
#include <fstream>     //文件输入/输出
#include <cstdio>      // 定义输入/输出函数
#include <cctype>      // 字符处理
#include <sstream>
#include <map>
int getCount(std::map<char, int> &lCount , char ch);
int main()
{
using namespace std;
string IN_TEXT = "AEIOU, or A.E.I.O.U., was a symbolic device utilised by the Habsburg emperors. "
"Emperor Frederick III (1415-93), who had a fondness for mythical formulae,  "
"habitually signed buildings and objects with the acronym.[1] Frederick III did not  ";
istringstream instr(IN_TEXT);   // instr 代替 cin 人工输入测试字符
char ch;
map<char, int> letterCount;
while (instr >> ch) {
if (isalpha(ch)) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
++letterCount[ch];
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
++letterCount[ch];
}
}
int aCnt, eCnt, iCnt, oCnt, uCnt;   // 定义计数器,准备读出原音计数
aCnt = getCount(letterCount,'a');   // 读出小写 a 计数
aCnt += getCount(letterCount,'A');  // 加上大写 A 计数
eCnt = getCount(letterCount,'e');
eCnt += getCount(letterCount,'E');
iCnt = getCount(letterCount,'i');
iCnt += getCount(letterCount,'I');
oCnt = getCount(letterCount,'o');
oCnt += getCount(letterCount,'O');
uCnt = getCount(letterCount,'u');
uCnt += getCount(letterCount,'U');
cout << "Number of vowel a and A:/t" << aCnt << endl;
cout << "Number of vowel e and E:/t" << eCnt << endl;
cout << "Number of vowel i and I:/t" << iCnt << endl;
cout << "Number of vowel o and O:/t" << oCnt << endl;
cout << "Number of vowel u and U:/t" << uCnt << endl;
return 0;
}
int getCount(std::map<char, int> &lCount , char ch)
{
int Count=0;
if (lCount.count(ch))
Count = lCount[ch];
return Count;
}


能否在包装一下呢,于是使用字符串当参数,可能多个参数一起传入

和前面的函数也不冲突,可以重载使用

#include <iostream>    // 数据流输入/输出
#include <string>      // 字符串类
#include <algorithm>   // STL 通用算法
#include <vector>      // STL 动态数组容器
#include <fstream>     //文件输入/输出
#include <cstdio>      // 定义输入/输出函数
#include <cctype>      // 字符处理
#include <sstream>
#include <map>
int getCount(std::map<char, int> &lCount , const char *str);
int main()
{
using namespace std;
string IN_TEXT = "AEIOU, or A.E.I.O.U., was a symbolic "
"device utilised by the Habsburg emperors. ";
istringstream instr(IN_TEXT);   // instr 代替 cin 人工输入测试字符
char ch;
map<char, int> letterCount;
while (instr.get(ch)) {   // 改成 cin.get() 就是人工输入
if (isalpha(ch)) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
++letterCount[ch];
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
++letterCount[ch];
}
if (isspace(ch)) {
++letterCount[ch];
}
}
int aCnt, eCnt, iCnt, oCnt, uCnt , spCnt , total ;  // 定义计数器,准备读出原音计数
total = getCount(letterCount, "aeiouAEIOU");
aCnt = getCount(letterCount, "aA");  // 读出小写 a 计数
eCnt = getCount(letterCount, "eE");
iCnt = getCount(letterCount, "iI");
oCnt = getCount(letterCount, "oO");
uCnt = getCount(letterCount, "uU");
spCnt = letterCount[' '];
cout << "Number of vowel a and A:/t" << aCnt << endl;
cout << "Number of vowel e and E:/t" << eCnt << endl;
cout << "Number of vowel i and I:/t" << iCnt << endl;
cout << "Number of vowel o and O:/t" << oCnt << endl;
cout << "Number of vowel u and U:/t" << uCnt << endl;
cout << "Number of vowel  total :/t" << total << endl;
cout << "Number of vowel  space :/t" << spCnt << endl;
return 0;
}
int getCount(std::map<char, int> &lCount , const char *str)
{
int Count = 0;
while(*str) {
if (lCount.count(*str))
Count += lCount[*str];
str++;
}
return Count;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐