๐Ÿ’ป 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
๐Ÿ’ป Dev & CSSystem & Embedded

์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ”„๋กœ์ ํŠธ #7 ์ตœ์ข… (Assembler in C)

Problem: ์ง€๊ธˆ๊นŒ์ง€์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ 2 pass assembler๋ฅผ ๋งŒ๋“ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋จผ์ € ์–ด์…ˆ๋ธ”๋Ÿฌ(Assembler)๋ž€? ํ•˜๋“œ์›จ์–ด๊ฐ€ ์ง์ ‘ ์ดํ•ดํ•˜์—ฌ ์‹คํ–‰ํ•˜๋Š” ๊ธฐ๊ณ„์–ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋น„ํŠธ ์—ด ๋˜๋Š” 16์ง„์ˆ˜๋กœ ํ‘œํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ๊ฐ„์ด ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ๊ทธ๋ž˜์„œ ์ธ๊ฐ„์ด ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋„๋ก ๊ธฐ๊ณ„์–ด์™€ ๊ฑฐ์˜ ์ผ๋Œ€์ผ๋กœ ๋Œ€์‘ํ•˜๋Š” ๊ธฐํ˜ธ๋กœ ํ‘œํ˜„๋œ ์–ธ์–ด๋กœ ์–ด์…ˆ๋ธ”๋Ÿฌ ์–ธ์–ด๊ฐ€ ์žˆ์œผ๋ฉฐ, ์–ด์…ˆ๋ธ”๋Ÿฌ ์–ธ์–ด๋ฅผ ๊ธฐ๊ณ„์–ด๋กœ ๋ฒˆ์—ญํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์–ด์…ˆ๋ธ”๋Ÿฌ, ๋ฒˆ์—ญํ•˜๋Š” ๊ฒƒ์„ ์–ด์…ˆ๋ธ”์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์–ด์…ˆ๋ธ”๋Ÿฌ์˜ ์—ญํ• ์„ ๊ทธ๋ฆผ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด ๊ธ€์—์„œ ๊ตฌํ˜„ ํ•  2 ํŒจ์Šค ์–ด์…ˆ๋ธ”๋Ÿฌ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. pass 1 : ...

March 19, 2020 ยท 6 min ยท Sobamemil
๐Ÿ’ป Dev & CS

Merge Sort Algorithm

ํ•ฉ๋ณ‘ ์ •๋ ฌ(Merge Sort)์ด๋ž€? ๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜(=Divide and conquer algorithm ์ฆ‰, ๊ทธ๋Œ€๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ฌธ์ œ๋กœ ๋ถ„ํ• ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‚˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.)์˜ ํ•˜๋‚˜๋กœ O(n log n)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ฉ๋ณ‘ ์ •๋ ฌ์˜ ์ž‘๋™ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๊ฐ€ 1 ์ดํ•˜์ด๋ฉด ์ด๋ฏธ ์ •๋ ฌ๋œ ๊ฒƒ์œผ๋กœ ๋ณธ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ๋ถ„ํ• (divide) : ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ ˆ๋ฐ˜์œผ๋กœ ์ž˜๋ผ ๋น„์Šทํ•œ ํฌ๊ธฐ์˜ ๋‘ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋กœ ๋‚˜๋ˆˆ๋‹ค. ์ •๋ณต(conquer) : ๊ฐ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ํ•ฉ๋ณ‘ ์ •๋ ฌ์„ ์ด์šฉํ•ด ์ •๋ ฌํ•œ๋‹ค. ๊ฒฐํ•ฉ(combine) : ๋‘ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค์‹œ ํ•˜๋‚˜์˜ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๋กœ ํ•ฉ๋ณ‘ํ•œ๋‹ค. ์ด๋•Œ ์ •๋ ฌ ๊ฒฐ๊ณผ๊ฐ€ ์ž„์‹œ๋ฐฐ์—ด์— ์ €์žฅ๋œ๋‹ค. ๋ณต์‚ฌ(copy) : ์ž„์‹œ ๋ฐฐ์—ด์— ์ €์žฅ๋œ ๊ฒฐ๊ณผ๋ฅผ ์›๋ž˜ ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•œ๋‹ค. ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๊ฐ„๋‹ค๋ฉด ์•„๋ž˜ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ํ†ตํ•ด ์ž‘๋™ ์›๋ฆฌ๋ฅผ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ...

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

C++ Programming Ch.10 Exercise 16 Solution

Problem: vector<Shape*> v;๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ๊ทธ๋ž˜ํ”ฝ ํŽธ์ง‘๊ธฐ๋ฅผ ์ฝ˜์†” ๋ฐ”ํƒ•์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด์ž. ์ƒ์„ฑ๋œ ๋„ํ˜• ๊ฐ์ฒด๋ฅผ v์— ์‚ฝ์ž…ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋ผ. 9์žฅ Exercise Problem 10๋ฒˆ์˜ ํžŒํŠธ๋ฅผ ์ฐธ๊ณ ํ•˜๋ผ. Shape๊ณผ Circle, Line, Rect ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Execution Result: Objective & Hints: vector๋ฅผ ํ™œ์šฉํ•˜๋Š” ์ข…ํ•ฉ ์‘์šฉ 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 #include #include using namespace std; class Shape { protected: virtual void draw() = 0; public: void paint() { draw(); } }; class Circle : public Shape { protected: virtual void draw(){ cout << "Circle" << endl; } }; class Rect : public Shape { protected: virtual void draw() { cout << "Rectangle" << endl; } }; class Line : public Shape { protected: virtual void draw() { cout << "Line" << endl; } }; class UI { public: static int seleteMenu() { int n; cout << "์‚ฝ์ž…:1, ์‚ญ์ œ:2, ๋ชจ๋‘๋ณด๊ธฐ:3, ์ข…๋ฃŒ:4 >> "; cin >> n; return n; } static int seleteShape() { int n; cout << "์„ :1, ์›:2, ์‚ฌ๊ฐํ˜•:3 >> "; cin >> n; return n; } static int seleteDelIndex() { int n; cout << "์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ๋„ํ˜•์˜ ์ธ๋ฑ์Šค >> "; cin >> n; return n; } static void showAll(vector<Shape*> &v, vector<Shape*>::iterator &it) { int i=0; for(it = v.begin();it!=v.end(); it++, i++){ // vector v์˜ ์ฒซ ์›์†Œ๋ถ€ํ„ฐ ๋ ์›์†Œ๊นŒ์ง€ ํƒ์ƒ‰ ๋ฐ ์ถœ๋ ฅ cout << i << ": "; v.at(i)->paint(); } } }; class GraphicEditor { vector<Shape*> v; vector<Shape*>::iterator it; public: GraphicEditor() { cout << "๊ทธ๋ž˜ํ”ฝ ์—๋””ํ„ฐ์ž…๋‹ˆ๋‹ค.\n"; start(); } void start() { while(true){ int n; n = UI::seleteMenu(); switch(n){ case 1: //์‚ฝ์ž…์„ ์„ ํƒํ•œ ๊ฒฝ์šฐ n = UI::seleteShape(); switch(n){ case 1: //์„ ์„ ์„ ํƒํ•œ ๊ฒฝ์šฐ v.push_back(new Line()); break; case 2: //์›์„ ์„ ํƒํ•œ ๊ฒฝ์šฐ v.push_back(new Circle()); break; case 3: //์‚ฌ๊ฐํ˜•์„ ์„ ํƒํ•œ ๊ฒฝ์šฐ v.push_back(new Rect()); break; default: cout << "์ž˜๋ชป ์„ ํƒํ•˜์…จ์Šต๋‹ˆ๋‹ค.\n"; break; } break; case 2:{ //์‚ญ์ œ๋ฅผ ์„ ํƒํ•œ ๊ฒฝ์šฐ n = UI::seleteDelIndex(); if(n >= v.size()

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

C++ Programming Ch.10 Exercise 15 Solution

Problem: vector๋ฅผ ์ด์šฉํ•˜์—ฌ ์•„๋ž˜ Circle ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์‚ฝ์ž…ํ•˜๊ณ  ์‚ญ์ œํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ์‚ญ์ œ ์‹œ์—๋Š” ์ด๋ฆ„์ด ๊ฐ™์€ ๋ชจ๋“  ์›์„ ์‚ญ์ œํ•œ๋‹ค. 1 2 3 4 5 6 7 8 9 10 class Circle { string name; // ์ด๋ฆ„ int radius; // ๋ฐ˜์ง€๋ฆ„ public: Circle(int radius, string name) { this->radius = radius; this->name = name; } double getArea() { return 3.14*radius*radius; } string getName() { return name; } }; Execution Result: ...

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

C++ Programming Ch.10 Exercise 14 Solution

Problem: ์•”ํ˜ธ ๊ด€๋ฆฌ ์‘์šฉํ”„๋กœ๊ทธ๋žจ์„ map์„ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜๋ผ. ์‹คํ–‰ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. Execution Result: Objective & Hints: map ์ปจํ…Œ์ด๋„ˆ์— ์‚ฝ์ž… ๋ฐ ์กฐํšŒ ์‘์šฉ ์ด๋ฆ„๊ณผ ์ ์ˆ˜๋ฅผ ์Œ์œผ๋กœ ์ €์žฅํ•  ๋งต ์ปจํ…Œ์ด๋„ˆ๋กœ map<string, string>์„ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๋งํฌ์— ์žˆ๋Š” Exercise Problem 10์žฅ 13๋ฒˆ์„ ์ฐธ๊ณ ํ•˜์„ธ์š”. 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 using namespace std; void insert(map<string, string> &pwManager){ string name, pw; cout << "์ด๋ฆ„ ์•”ํ˜ธ>> "; cin >> name >> pw; pwManager.insert(make_pair(name, pw)); } void checkNamePw(map<string, string> &pwManager){ string name, pw; cout << "์ด๋ฆ„? "; cin >> name; while(true){ cout << "์•”ํ˜ธ? "; cin >> pw; if(pwManager[name] == pw){ cout << "ํ†ต๊ณผ!!\n"; break; } else cout << "์‹คํŒจ~~\n"; // ํ‹€๋ฆฌ๋ฉด ์ถœ๋ ฅ ํ›„ ๋‹ค์‹œ ์•”ํ˜ธ ์งˆ๋ฌธ } } int main() { map<string, string> pwManager; cout << "***** ์•”ํ˜ธ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ WHO๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค *****\n"; while(true){ cout << "์‚ฝ์ž…:1, ๊ฒ€์‚ฌ:2, ์ข…๋ฃŒ3>> "; int n; cin >> n; switch(n){ case 1: insert(pwManager); break; case 2: checkNamePw(pwManager); break; case 3: cout << "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค..."; return 0; } } } Explanation: ...

March 11, 2020 ยท 2 min ยท Sobamemil