Write a program to read four lines of upper case (i.e., all CAPITAL LETTERS) text input
(no more than 72 characters per line) from the input file and print a vertical
histogram that shows how many times each letter (but not blanks, digits, or punctuation)
appears in the all-upper-case input. Format your output exactly as shown.
Input
Output
Sample Input THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. THIS IS AN EXAMPLE TO TEST FOR YOUR HISTOGRAM PROGRAM. HELLO!
Sample Output
题目理解: 输入由四行字符串组成(中间可能有空格),每行文字均由A-Z 26个大写字母和其他字符组成,但你要做的只是统计每个大写字母的出现次数,并以图示的方式用*的个数表示出来。
注意事项:
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
const int MAXN = 26;
int acount[MAXN];
int main()
{
int linecount, max;
string s;
memset(acount, 0, sizeof(acount));
linecount = 0;
while (getline(cin, s)) {
// 统计字母
for(int i=0; i<(int)s.size(); i++)
// if(isalpha(s[i]))
// acount[s[i] - 'A']++;
if(isupper(s[i]))
acount[s[i] - 'A']++;
// 每4行输出一次结果
if(++linecount == 4) {
linecount = 0;
// 计算最大的统计值
max = 0;
for(int i=0; i<MAXN; i++)
if(acount[i] > max)
max = acount[i];
// 输出max行
for(int i=max; i>0; i--) {
for(int j=0; j<MAXN; j++) {
if(acount[j] >= i)
cout << "* ";
else
cout << " ";
}
cout << endl;
}
for(int i=0; i<MAXN; i++)
cout << (char)('A' + i) << " ";
cout << endl;
}
}
return 0;
}