๐Ÿ’ป Dev & CSC++ Programming

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
๐Ÿ’ป Dev & CSC++ Programming

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