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
* Lines 1..4: Four lines of upper case text, no more than 72 characters per line.
Output
* Lines 1..??: Several lines with asterisks and spaces followed by one line with the upper-case alphabet separated by spaces. Do not print unneeded blanks at the end of any line. Do not print any leading blank lines.
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 B C D E F G H I J K L M N O P Q R S T U V W X Y Z
//
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <map>
using namespace std;
int main()
{
map<char, int> a;
string s;
int MAX = 0;
for(int i=0; i<4; i++)
{
getline(cin, s);
for(int j=0; s[j]; j++)
{
// if(isalpha(s[i]))
if(s[j]>='A' && s[j] <= 'Z')
a[s[j]]++;
else continue;
if(a[s[j]] > MAX)
MAX = a[s[j]];
}
}
/* for (map<char, int>::iterator it = a.begin(); it != a.end(); it++) {
cout << it ->first << " " << it ->second << endl;
}*/
// cout << endl;
for(int i=MAX; i; i--) {
for (map<char, int>::iterator it = a.begin(); it != a.end(); it++) {
if (it->second == i && it->first != 'Z') {
cout << "* ";
it->second--;
}
else if (it->second == i && it->first == 'Z') {
cout << "*";
it->second--;
}
else cout << " ";
}
cout << endl;
}
cout << "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z " << endl;
/* for (map<char, int>::iterator it = a.begin(); it != a.end(); it++) {
cout << it ->first << " ";
}
cout << endl;
*/
return 0;
}

本文介绍了一种程序设计方法,用于读取四行大写英文输入,生成一个垂直直方图,展示每个字母出现的频率。程序排除空白、数字和标点符号,输出格式精确。示例输入包括一句话和三个短语,输出为一系列星号行和完整的英文字母表。

被折叠的 条评论
为什么被折叠?



