C++ Programming Ch.4 Exercise 13 Solution

Problem: 영문자로 구성된 텍스트에 대해 각 알파벳에 해당하는 문자가 몇 개인지 출력하는 히스토그램 클래스 Histogram을 만들어보자. 대문자는 모두 소문자로 변환하여 처리한다. Histogram 클래스를 활용하는 사례와 Execution Result는 다음과 같다. 1 2 3 4 5 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(); Execution Result: Objective & Hints: 클래스 만들기 종합 응용 Code: 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 #include #include using namespace std; class Histogram { string sent; public: Histogram(string sent); void put(string sent); void putc(char c); void print(); }; Histogram::Histogram(string text){ sent = text; cout << sent << endl; } void Histogram::put(string text){ sent += text; cout << text; } void Histogram::putc(char c){ sent += c; cout << c; } void Histogram::print(){ int alpha[26] = { 0 }; int num = 0; for (int i = 0; i < sent.length(); i++) { if(isalpha(sent[i])){ char c = tolower(sent[i]); alpha[c - 'a']++; num++; } } cout << endl << endl; cout << "총 알파벳 수 " << num; cout << endl << endl; for (int i = 0; i < 26; ++i) { char c = 'a' + i; cout << c << " (" << alpha[i] << ")\t: "; for (int j = 0; j < alpha[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(); } Explanation: ...

March 4, 2020 · 2 min · Sobamemil

C++ Programming Ch.2 Exercise 16 Solution

Problem: 영문 텍스트를 입력받아 알파벳 히스토그램을 그리는 프로그램을 작성하라. 대문자는 모두 소문자로 집계하며, 텍스트 입력의 끝은 ';' 문자로 한다. Objective & Hints: 문자열 읽기, C++ 프로그램 종합 응용 Input File: 입력할 텍스트 파일을 첨부하였습니다. [2-16.txt 0.00MB](https://blog.kakaocdn.net/dna/EsAHK/btqChFfPBnw/AAAAAAAAAAAAAAAAAAAAAOhcwRebixdRiSrFB2r5c8Z4mBZDeCt4ibUUEp2n5aCw/2-16.txt?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1788188399&allow_ip=&allow_referer=&signature=PEoHUr5ktP9MyFLvskuQj0jS1Q8%3D&attach=1&knm=tfile.txt) Execution Result: Code: 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 #include #include using namespace std; int main() { int tot=0; // 총 알파벳 개수 int alpha[27]={0}; // 각각의 알파벳을 나타냄 char text[10000]; // text를 저장할 배열 cout << "영문 텍스트를 입력하세요. 히스토그램을 그립니다.\n텍스트의 끝은 ; 입니다. 10000개까지 가능합니다.\n"; cin.getline(text,10000,';'); // 최대 10,000개의 영문 텍스트를 ';'전까지 입력받는다 for (int i = 0; i<strlen(text); i++) { // text[0]부터 text의 끝까지 if (isalpha(text[i])) { // text[i]가 알파벳이면 참 if(text[i]<91) text[i] = tolower(text[i]); // text[i]가 대문자이면 소문자로 변경 tot++; alpha[text[i] - 97]++; // 각각의 알파벳이 몇개인지 카운트 } } cout << "총 알파벳 수 " << tot << endl << endl; for (int i = 0; i < 26; ++i) { // 알파벳의 소문자 총 개수는 26개 (a~z) cout << (char)(i+'a') << "(" << alpha[i] << ")"; // a부터 z까지 출력 후 각 (알파벳의 개수) 출력 cout << "\t: "; // 포맷을 출력하기 위해 탭과 ":" 출력 for (int j = 1; j <= alpha[i]; j++) // 각 알파벳의 개수만큼 "*" 출력 cout << "*"; cout << endl; } } Explanation: ...

February 28, 2020 · 2 min · Sobamemil