C++

[C++] 명품 C++ 프로그래밍 실습문제 7장 오픈챌린지/openchallenge

Hs’s Coding vlog 2023. 1. 11. 15:38

[C++]명품 C++ programming 실습문제 7장

명품 C++ 프로그래밍실습문제/연습문제 /C++/히스토그램

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;
class Histogram{
    string Sen;
public:
    Histogram(string sen){
        this-> Sen = sen;
    }
    Histogram &operator << (string sen){
        this->Sen = this->Sen + sen;
        return *this;
    }
    Histogram &operator << (char c){
        this->Sen = this->Sen + c;
        return *this;
    }
    
    
    void operator !() {
        int i, count=0;
        int alnum[26];
        for(i=0;i<26;i++) alnum[i]=0;

        cout << Sen << endl << endl;
        
        for(i=0; i< Sen.size(); i++) {
            if( isalpha( Sen.at(i) ) )
            {
                int c = tolower( Sen.at(i) );
                alnum[ c-97 ]++;
                count++;
            }
        }
        cout << "총 알파벳 수 " << count << endl;
        for(i=0; i<26; i++) {
            cout << (char)(i+97) << ":";
            for(int k=0; k< alnum[i]; k++) 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;//song 객체 출력하기
    
    return 0;
}