Typing diary

명품 c++ programming 7장 Open Challenge 본문

C, C++

명품 c++ programming 7장 Open Challenge

Jcon 2022. 8. 16. 15:19
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
using namespace std;
 
class Histogram
{
    string sen;
    int totCount = 0;
    int Count[26= { 0 };
public:
    Histogram(string sen);
    Histogram& operator << (string op1);
    Histogram& operator << (char op1);
    void operator ! ();
};
 
 
Histogram::Histogram(string sen)
{
    this->sen = sen;
}
 
Histogram& Histogram::operator<<(string op1)
{
    sen += op1;
    return *this;
}
 
Histogram& Histogram::operator<<(char op1)
{
    sen += op1;
    return *this;
}
 
void Histogram::operator!()
{
    cout << sen << endl;
    for (int i = 0; i < sen.size(); i++)
    {
        if (isalpha(sen.at(i)))
        {
            allCount++;
            Count[tolower(sen.at(i)) - 97]++;
        }
    }
    cout << "총 알파벳 수 " << totCount << endl;
 
    for (int i = 0; i < 26; i++)
    {
        cout << (char)(i + 97<< ':';
        for (int j = 0; j < Count[i]; j++)
        {
            cout << '*';
        }
        cout << endl;
    }
}
 
 
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

'C, C++' 카테고리의 다른 글

c++ static멤버  (0) 2022.08.16
명품 c++ programming 6장 8번  (0) 2022.08.16
명품 c++ programming 4장 10번  (0) 2022.08.16
c++ 참조  (0) 2022.08.16
c++ 복사 생성자  (0) 2022.08.16