๐Ÿ’ป 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 15 Solution

Problem: ๋ง์…ˆ(+), ๋บ„์…ˆ(-), ๊ณฑ์…ˆ(*), ๋‚˜๋ˆ—์…ˆ(/), ๋‚˜๋จธ์ง€(%)์˜ ์ •์ˆ˜ 5์ฒ™ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ž…๋ ฅ๋œ๋‹ค. ์ •์ˆ˜์™€ ์—ฐ์‚ฐ์ž๋Š” ํ•˜๋‚˜์˜ ๋นˆ์นธ์œผ๋กœ ๋ถ„๋ฆฌ๋œ๋‹ค. Objective & Hints: ๊ณต๋ฐฑ์„ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด ์ฝ๊ธฐ, C++ ํ”„๋กœ๊ทธ๋žจ ์ข…ํ•ฉ ์‘์šฉ ํ•œ ์ค„์„ ๋ฌธ์ž์—ด๋กœ ์ฝ๊ณ , ๊ณต๋ฐฑ ๋ฌธ์ž๋ฅผ ์ฐพ์•„ ์—ฐ์‚ฐ์ž์™€ ๋‘ ๊ฐœ์˜ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๊ตฌ๋ถ„ํ•œ ํ›„, ๊ณ„์‚ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ฐ”๊ฟ€ ๋•Œ atoi() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด atoi("34") = 34 ์ž…๋‹ˆ๋‹ค. 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 30 31 32 33 34 35 36 37 #include #include #include using namespace std; int main(){ char sic[100]; char *symbol; int front,rear; while(true){ cout << "? "; cin.getline(sic,100); front = atoi(strtok(sic, " ")); symbol = strtok(NULL, " "); rear = atoi(strtok(NULL, " ")); if(*symbol == '+'){ cout << front << " + " << rear << " = " << front+rear << endl; } else if(*symbol == '-'){ cout << front << " - " << rear << " = " << front-rear << endl; } else if(*symbol == '*'){ cout << front << " * " << rear << " = " << front*rear << endl; } else if(*symbol == '/'){ cout << front << " / " << rear << " = " << front/rear << endl; } else if(*symbol == '%'){ cout << front << " % " << rear << " = " << front%rear << endl; } } return 0; } Explanation: ...

February 28, 2020 ยท 2 min ยท Sobamemil
๐Ÿ’ป Dev & CSC++ Programming

C++ Programming Ch.2 Exercise 14 Solution

Problem: ์ปคํ”ผ๋ฅผ ์ฃผ๋ฌธํ•˜๋Š” ๊ฐ„๋‹จํ•œ C++ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด๋ณด์ž. ์ปคํ”ผ ์ข…๋ฅ˜๋Š” "์—์Šคํ”„๋ ˆ์†Œ", "์•„๋ฉ”๋ฆฌ์นด๋…ธ", "์นดํ‘ธ์น˜๋…ธ"์˜ 3๊ฐ€์ง€์ด๋ฉฐ ๊ฐ€๊ฒฉ์€ ๊ฐ๊ฐ 2000์›, 2300์›, 2500์›์ด๋‹ค. ํ•˜๋ฃจ์— 20000์› ์ด์ƒ ๋ฒŒ๊ฒŒ ๋˜๋ฉด ์นดํŽ˜๋ฅผ ๋‹ซ๋Š”๋‹ค. Execution Result์™€ ๊ฐ™์ด ์ž‘๋™ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. Objective & Hints: C++ ํ”„๋กœ๊ทธ๋žจ ๊ตฌ์„ฑ. cin, strcmp() ํ™œ์šฉ ์ข…ํ•ฉ ์—ฐ์Šต char coffee[100]; int num; cin >> coffee >> num; ์œผ๋กœ ์ปคํ”ผ ์ด๋ฆ„๊ณผ ์ž” ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ปคํ”ผ๋Š” if(strcmp(coffee, "์—์Šคํ”„๋ ˆ์†Œ") == 0 )๊ณผ ๊ฐ™์ด ๋น„๊ตํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Execution Result: ...

February 28, 2020 ยท 1 min ยท Sobamemil
๐Ÿ’ป Dev & CSC++ Programming

C++ Programming Ch.2 Exercise 8 Solution

Problem: ํ•œ ๋ผ์ธ์— ';' ์œผ๋กœ 5๊ฐœ์˜ ์ด๋ฆ„์„ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅ๋ฐ›์•„, ๊ฐ ์ด๋ฆ„์„ ๋Š์–ด๋‚ด์–ด ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ณ  ๊ฐ€์žฅ ๊ธด ์ด๋ฆ„์„ ํŒ๋ณ„ํ•˜๋ผ. Objective & Hints: cin.getline()์œผ๋กœ ๋ฌธ์ž์—ด ์ฝ๊ธฐ ';'๊นŒ์ง€ ๋ฌธ์ž์—ด์„ ์ฝ๊ณ ์ž ํ•˜๋ฉด ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  1 2 char name[100]; cin.getline(name, 100, ';'); 5๊ฐœ ์ฝ์–ด์•ผ ํ•˜๋‹ˆ 5๋ฒˆ ๋ฃจํ”„๋ฅผ ๋ˆ๋‹ค. 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 #include #include using namespace std; int main() { int A=0; int i; char name[100]; char longName[100]; cout << "5 ๋ช…์˜ ์ด๋ฆ„์„ ';' ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅํ•˜์„ธ์š” \n>>"; for(i=1;i<6;i++){ cin.getline(name,100,';'); cout << i << " : " << name << endl; if(A < strlen(name)) { A = strlen(name); strcpy(longName,name); } } cout << "๊ฐ€์žฅ ๊ธด ์ด๋ฆ„์€ " << longName; return 0; } Explanation: ...

February 28, 2020 ยท 1 min ยท Sobamemil
๐Ÿ’ป Dev & CSC++ Programming

C++ Programming Ch.2 Exercise 7 Solution

Problem: ๋‹ค์Œ๊ณผ ๊ฐ™์ด "yes"๊ฐ€ ์ž…๋ ฅ๋  ๋•Œ๊นŒ์ง€ ์ข…๋ฃŒํ•˜์ง€ ์•Š๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ์˜ ์ž…๋ ฅ์€ cin.getline() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ผ. Objective & Hints: ๊ณต๋ฐฑ์„ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด ์ฝ๊ธฐ Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include using namespace std; int main() { char A[] = "yes"; // ๋ฐฐ์—ด ์„ ์–ธ์‹œ "yes"๋กœ ์ดˆ๊ธฐํ™” char B[100]; while(true){ cout << "์ข…๋ฃŒํ•˜๊ณ  ์‹ถ์œผ๋ฉด yes๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>"; cin.getline(B,100); if(strcmp(A,B) == 0) break; } cout << "์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค..."; return 0; } Explanation: ...

February 28, 2020 ยท 1 min ยท Sobamemil
๐Ÿ’ป Dev & CSC++ Programming

C++ Programming Ch.2 Exercise 6 Solution

Problem: ๋ฌธ์ž์—ด์„ ๋‘ ๊ฐœ ์ž…๋ ฅ๋ฐ›๊ณ  ๋‘ ๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ๊ฐ™์€์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ๋งŒ์ผ ๊ฐ™์œผ๋ฉด "๊ฐ™์Šต๋‹ˆ๋‹ค", ์•„๋‹ˆ๋ฉด "๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค"๋ฅผ ์ถœ๋ ฅํ•˜๋ผ. Objective & Hints: ๊ณต๋ฐฑ ์—†์ด ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด ์ฝ๊ธฐ (๋นˆ์นธ ์—†์ด ์ž…๋ ฅ) Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include using namespace std; int main() { char pw1[100]; char pw2[100]; cout << "์ƒˆ ์•”ํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>"; cin.getline(pw1,100); cout << "์ƒˆ ์•”ํ˜ธ๋ฅผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์ž…๋ ฅํ•˜์„ธ์š”>>"; cin.getline(pw2,100); if(strcmp(pw1,pw2)==0) cout << "๊ฐ™์Šต๋‹ˆ๋‹ค"; else cout << "๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค"; return 0; } Explanation: ...

February 28, 2020 ยท 1 min ยท Sobamemil