본문 바로가기

언어/C++

히스토그램

#include <iostream>
#include <string>
using namespace std;

class Histogram {
	string his;
	int len;
	int count[26];
public :
	Histogram(string h) { 
		his=h+"\n";
		len=0;
		
		for(int i=0; i<h.size();i++){
			if(isalpha(his.at(i))) len++;
		}
		for(int i=0; i<26; i++){
			count[i]=0;
		}
	}
	
	void put(string h);
	void putc(char h);
	void print();
};


void Histogram::put(string h) {
	this->his += h;
	for(int i=0; i < h.size(); i++)
		if( isalpha( h.at(i)) ) len++;
}
void Histogram::putc( char h) {
	this->his += h;
	if( isalpha(h) ) len++;
}
void Histogram::print() {
cout << his << endl << endl << "총 알파벳 수 " << len << endl << endl;
char temp;
	for(int i=0; i < his.size(); i++) {
		if( isalpha( his.at(i) ) ) {
			temp = tolower( his.at(i) );
			count[temp-97]++;
		}
	}
	for(int i=0; i<26; i++) {
		printf("%c (%d)\t: ", 97+i, count[i]);
		for(int j=0; j < count[i]; j++)
			cout << "*";
		cout << endl;
	}
}
 
int main() {
	Histogram elvisHisto("Wise men say, only fools rush in But I Can't help, ");
	elvisHisto.put("falling in love with you");
	elvisHisto.putc('-');
	elvisHisto.put("Elvis Presley");
	elvisHisto.print();
}

'언어 > C++' 카테고리의 다른 글

5단원 실습문제 (참조, 복사생성자 )  (1) 2019.11.12
복사생성자  (0) 2019.11.12
함수와 참조  (0) 2019.11.12
원의 개수 와 면적 구하기  (0) 2019.11.11
전화번호 부  (0) 2019.11.11