๐Ÿ’ป Dev & CSC++ Programming

C++ Programming Ch.11 Exercise 10 Solution

Problem: ๋‹ค์Œ์€ ํ”„๋กœ๊ทธ๋žจ๊ณผ Execution Result๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. prompt ์กฐ์ž‘์ž๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์™„์„ฑํ•˜๋ผ. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include using namespace std; int main() { string password; while(true) { cin >> prompt >> password; if(password == "C++") { cout << "login success!!" << endl; break; } else cout << "login fail. try again!!" << endl; } } Execution Result: ...

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

C++ Programming Ch.11 Exercise 9 Solution

Problem: ๋‹ค์Œ์€ Phone ํด๋ž˜์Šค์ด๋‹ค. 1 2 3 4 5 6 7 8 9 10 11 class Phone { // ์ „ํ™” ๋ฒˆํ˜ธ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ํด๋ž˜์Šค string name; string telnum; string address; public: Phone(string name="", string telnum="", string address="") { this->name = name; this->telnum = telnum; this->address = address; } }; Phone ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ž…์ถœ๋ ฅํ•˜๋Š” ์•„๋ž˜ ์ฝ”๋“œ์™€ Execution Result๋ฅผ ์ฐธ์กฐํ•˜์—ฌ <<, >> ์—ฐ์‚ฐ์ž๋ฅผ ์ž‘์„ฑํ•˜๊ณ  Phone ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋“ฑ ํ”„๋กœ๊ทธ๋žจ์„ ์™„์„ฑํ•˜๋ผ. 1 2 3 Phone girl, boy; cin >> girl >> boy; cout << girl << endl << boy << endl; Execution Result: ...

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

C++ Programming Ch.11 Exercise 8 Solution

Problem: Circle ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1 2 3 4 5 6 7 8 class Circle { string name; int radius; public: Circle(int radius=1, string name="") { this->radius = radius; this->name = name; } }; Circle ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ž…์ถœ๋ ฅํ•˜๋Š” ๋‹ค์Œ ์ฝ”๋“œ์™€ Execution Result๋ฅผ ์ฐธ์กฐํ•˜์—ฌ <<, >> ์—ฐ์‚ฐ์ž๋ฅผ ์ž‘์„ฑํ•˜๊ณ  Circle ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋“ฑ ํ”„๋กœ๊ทธ๋žจ์„ ์™„์„ฑํ•˜๋ผ. 1 2 3 Circle d, w; cin >> d >> w; // ํ‚ค๋ณด๋“œ ์ž…๋ ฅ์„ ๋ฐ›์•„ ๊ฐ์ฒด d์™€ w๋ฅผ ์™„์„ฑ cout << d << w << endl; // ๊ฐ์ฒด d, w ์ถœ๋ ฅ Execution Result: ...

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

C++ Programming Ch.11 Exercise 7 Solution

Problem: 0์—์„œ 127๊นŒ์ง€ ASCII ์ฝ”๋“œ์™€ ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ํ™”๋ฉด์— ์ถœ๋ ฅ๊ฐ€๋Šฅํ•˜์ง€ ์•Š๋Š” ASCII ์ฝ”๋“œ๋Š” '.'์œผ๋กœ ์ถœ๋ ฅํ•˜๋ผ. Execution Result: Objective & Hints: cout์œผ๋กœ ํฌ๋งท ์ถœ๋ ฅ ์‘์šฉ ์—ฐ์Šต ๋ฌธ์ž๊ฐ€ ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•œ์ง€ ์•Œ๊ธฐ ์œ„ํ•ด bool isprint(int c); ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ ๋ณ€์ˆ˜ c๋Š” ๋ฌธ์ž ์ฝ”๋“œ ๊ฐ’์ด๊ณ , ํ—ค๋” ํŒŒ์ผ์„ include ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. 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 #include #include #include using namespace std; void showDec(int d) { // 10์ง„์ˆ˜ ์ถœ๋ ฅ cout << setw(10) << dec << d; } void showHexa(int h) { // 16์ง„์ˆ˜ ์ถœ๋ ฅ cout << setw(10) << hex << h; } void showChar(int c) { // ASCII ์ถœ๋ ฅ int i=0; if( (i = isprint(c)) != 0) // ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์ธ์ง€ ํ™•์ธ cout << setw(10) << (char)c; else // ์ถœ๋ ฅ ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์ด๋ฉด "." ์ถœ๋ ฅ cout << setw(10) << "."; } void print() { for(int i=0; i<4; i++){ cout << setw(10) << "dec"; cout << setw(10) << "hexa"; cout << setw(10) << "char"; } cout << endl; for(int i=0; i<4; i++){ cout << setw(10) << "---"; cout << setw(10) << "----"; cout << setw(10) << "----"; } cout << endl; for(int i=0; i<128; i++){ // 127๋ฒˆ ๋ฐ˜๋ณตํ•˜์—ฌ dec, hexa, ASCII ์ถœ๋ ฅ if(i%4==0 && i!=0) cout << endl; showDec(i); showHexa(i); showChar(i); } } int main() { cout.setf(ios::left); // ์ถœ๋ ฅ ํฌ๋งท ์™ผ์ชฝ ์ •๋ ฌ print(); } Explanation: ...

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

C++ Programming Ch.11 Exercise 6 Solution

Problem: ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์ˆ˜, ์ œ๊ณฑ, ์ œ๊ณฑ๊ทผ์˜ ๊ฐ’์„ ํ˜•์‹์— ๋งž์ถ”์–ด ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ํ•„๋“œ์˜ ๊ฐ„๊ฒฉ์€ ์ด 15์นธ์ด๊ณ  ์ œ๊ณฑ๊ทผ์˜ ์œ ํšจ ์ˆซ์ž๋Š” ์ด 3์ž๋ฆฌ๋กœ ํ•œ๋‹ค. ๋นˆ์นธ์€ ๋ชจ๋‘ underline(_) ๋ฌธ์ž๋กœ ์‚ฝ์ž…ํ•œ๋‹ค. Execution Result: Objective & Hints: cout์œผ๋กœ ํฌ๋งท ์ถœ๋ ฅ ์‘์šฉ ์—ฐ์Šต ์ œ๊ณฑ๊ทผ์„ ๊ตฌํ•˜๋ ค๋ฉด ํ—ค๋” ํŒŒ์ผ์„ include ํ•˜๊ณ  sqrt(double x) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 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 #include using namespace std; void showNumber(const double &num){ cout << setw(15) << setfill('_') << num; } void showSqrt(const double &num){ cout << setprecision(3) << setw(15) << setfill('_') << sqrt(num) << endl; } int main() { cout.setf(ios::left); cout << setw(15) << "Number"; cout << setw(15) << "Square"; cout << setw(15) << "Square Root" << endl; for(double i=0; i<=45; i+=5){ cout.precision(4); showNumber(i); // Number ์ถœ๋ ฅ showNumber(i*i); // Square ์ถœ๋ ฅ showSqrt(i); // Square Root ์ถœ๋ ฅ } } Explanation: ...

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

C++ Programming Ch.11 Exercise 5 Solution

Problem: ๋‹ค์Œ ํ”„๋กœ๊ทธ๋žจ์€ ์˜ˆ์ œ 11-3์˜ ์ฝ”๋“œ์ด๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ char [] ๋Œ€์‹  string์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ์žฌ์ž‘์„ฑํ•˜๋ผ. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include using namespace std; int main() { char cmd[80]; cout << "cin.get(char*, int)๋กœ ๋ฌธ์ž์—ด์„ ์ฝ์Šต๋‹ˆ๋‹ค." << endl; while(true) { cout << "์ข…๋ฃŒํ•˜๋ ค๋ฉด exit๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” >> "; cin.get(cmd, 80); // 79๊ฐœ๊นŒ์ง€์˜ ๋ฌธ์ž ์ฝ์Œ if(strcmp(cmd, "exit") == 0 ) { cout << "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค...."; return 0; } else cin.ignore(1); // ๋ฒ„ํผ์— ๋‚จ์•„ ์žˆ๋Š” ํ‚ค ('\n') ์ œ๊ฑฐ } } Execution Result: ...

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

C++ Programming Ch.11 Exercise 4 Solution

Problem: ํ•œ ์ค„์— '์˜์–ด๋ฌธ์žฅ;ํ•œ๊ธ€๋ฌธ์ž' ํ˜•์‹์œผ๋กœ ํ‚ค ์ž…๋ ฅ๋  ๋•Œ, cin.ignore()๋ฅผ ์ด์šฉํ•˜์—ฌ ';'์ด์ „์— ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด๋งŒ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ์•„๋ž˜์—์„œ ^Z(ctrl-z) ํ‚ค๋Š” ์ž…๋ ฅ ์ข…๋ฃŒ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ‚ค์ด๋ฉฐ, cin.get()์€ EOF๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. Execution Result: Objective & Hints: cin.get(), EOF, cin.ignore() ํ™œ์šฉ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include using namespace std; int main() { int ch; while( (ch = cin.get()) != EOF ) { if(ch == ';'){ cout.put('\n'); cin.ignore(100, '\n'); } else cout.put(ch); } } Explanation: ...

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

C++ Programming Ch.11 Exercise 3 Solution

Problem: ํ•œ ์ค„์— '์˜์–ด๋ฌธ์žฅ;ํ•œ๊ธ€๋ฌธ์ž' ํ˜•์‹์œผ๋กœ ํ‚ค ์ž…๋ ฅ๋  ๋•Œ, cin.ignore()๋ฅผ ์ด์šฉํ•˜์—ฌ ';' ์ดํ›„์— ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ์•„๋ž˜์—์„œ ^Z(ctrl-z) ํ‚ค๋Š” ์ž…๋ ฅ ์ข…๋ฃŒ๋Š” ๋‚˜ํƒ€๋‚ด๋Š” ํ‚ค์ด๋ฉฐ, cin.get()์€ EOF๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. Execution Result: Objective & Hints: cin.get(), EOF, cin.ignore() ํ™œ์šฉ Code: 1 2 3 4 5 6 7 8 9 10 11 12 #include using namespace std; int main() { int ch; cin.ignore(100, ';'); // ์˜์–ด ๋ฌธ์žฅ์ด ์ตœ๋Œ€ 99๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ž…๋ ฅ๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. while((ch=cin.get()) != EOF) { cout.put(ch); if(ch == '\n') cin.ignore(100, ';'); // ์˜์–ด ๋ฌธ์žฅ์ด ์ตœ๋Œ€ 99๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ž…๋ ฅ๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. } } Explanation: ...

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

C++ Programming Ch.11 Exercise 2 Solution

Problem: istream& get(char& ch) ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•œ ๋ผ์ธ์„ ์ฝ๊ณ  ๋นˆ์นธ(' ')์ด ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. Execution Result: Objective & Hints: cin์œผ๋กœ ํ‚ค ์ž…๋ ฅ ์—ฐ์Šต Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include using namespace std; int main() { char ch; int cnt; while(true){ cin.get(ch); // ํ‚ค๋ฅผ ch์— ์ฝ์–ด์˜ด if(cin.eof()) // EOF ๋ฌธ์ž ์ฆ‰ ctrl-z ํ‚ค๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ, ์ฝ๊ธฐ ์ข…๋ฃŒ break; if(ch == '\n') // ํ‚ค๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ ์ฝ๊ธฐ ์ค‘๋‹จ break; else if(ch == ' ') cnt++; } cout << cnt; }

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

C++ Programming Ch.11 Exercise 1 Solution

Problem: int cin.get() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‚ค๋ณด๋“œ๋กœ๋ถ€ํ„ฐ ํ•œ ๋ผ์ธ์„ ์ฝ๊ณ  'a'๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. Execution Result: Objective & Hints: cin์œผ๋กœ ํ‚ค ์ž…๋ ฅ ์—ฐ์Šต Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include using namespace std; int main() { int a; int cnt=0; while( (a = cin.get() ) != EOF ){ if(a == 'a') cnt++; else if(a == '\n') break; } cout << cnt; } Explanation: ...

March 27, 2020 ยท 1 min ยท Sobamemil