1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
using namespace std;
 
class Histogram {
    string histo;
public:
    Histogram(string histo) { this->histo = histo; }
    Histogram& operator<<(string str);
    Histogram& operator<<(char c);
    void operator!();
};
 
Histogram& Histogram::operator<<(string str) {
    histo += str;
    return *this;
}
 
Histogram& Histogram::operator<<(char c) {
    histo += c;
    return *this;
}
 
void Histogram::operator!() {
    int len = 0;
    int abc[26= { 0 };
    for (int i = 0; i < histo.length(); i++) {
        cout << histo[i];
        if (isalpha(histo[i])) {
            len++;
            for (int j = 0; j < 26; j++) {
                if ((char)(j + 97== tolower(histo[i])) abc[j]++;
            }
        }
    }
    cout << "\n\n" << "총 알파벳 수 " << len << '\n';
    for (int i = 0; i < 26; i++) {
        cout << (char)(i + 97<< ":";
        for (int j = 0; j < abc[i]; j++)
            cout << "*";
        cout << '\n';
    }
}
 
int main() {
    Histogram song("Wise men say, \nonly fools rush in But I can't help, \n");
    song << "falling" << " in love with you." << "- by ";
    song << 'k' << 'i' << 't';
    !song;
}
cs


+ Recent posts