Data Communications & Networking Ch.1 ๊ฐœ์š” Exercises & Solutions

Chapter 1 ๊ฐœ์š” Practice ExercisesSolution(๋‹ต)**** ๊ทผ๊ฑฐ๋ฆฌ ํ†ต์‹ ๋ง(LAN)์€ _______ ๋กœ ์ •์˜๋œ๋‹ค. -> a. ๋„คํŠธ์›Œํฌ์˜ ๊ธฐํ•˜ํ•™์  ํฌ๊ธฐ ๊ด‘์—ญ ํ†ต์‹ ๋ง์ด ํฌ๊ด„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ง€์—ญ์€ _______ ์ด๋‹ค. -> d. ์„ธ๊ณ„ ์ œ์•ˆ ํ‘œ์ค€์€ ์ ์–ด๋„ 2๊ฐœ์˜ ์„ฑ๊ณต์ ์ธ ์‹œ๋„ ํ›„ _______ ํ‘œ์ค€ ์ƒํƒœ๋กœ ์˜ฌ๋ผ๊ฐ„๋‹ค. -> c. ๋“œ๋ž˜ํ”„ํŠธ ๋‹ค์Œ ์ค‘ ๋ฐ์ดํ„ฐ ํ†ต์‹  ์‹œ์Šคํ…œ์˜ ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ์•„๋‹Œ ๊ฒƒ์€? -> d. a, b, c ๋ชจ๋‘ Answers & Solutions(a. ๋ฉ”์‹œ์ง€, b. ์†ก์‹ ์ž, c. ๋งค์ฒด) ๋‹ค์Œ ์ค‘ ๋ฐ์ดํ„ฐ ํ†ต์‹  ์‹œ์Šคํ…œ์˜ ํŠน์„ฑ์ด ์•„๋‹Œ ๊ฒƒ์€? -> d. a, b, c ๋ชจ๋‘ Answers & Solutions(a. ์ „๋‹ฌ์„ฑ, b. ์ •ํ™•์„ฑ, c. ์ง€ํ„ฐ) ...

April 12, 2020 ยท 2 min ยท Sobamemil

C++ Programming Ch.11 Exercise 12 Solution

Problem: ์ปคํ”ผ ์žํŒ๊ธฐ ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ๋ฅผ C++๋กœ ์ž‘์„ฑํ•ด๋ณด์ž. ์‹คํ–‰ ์‚ฌ๋ก€๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์žํŒ๊ธฐ๋Š” ๋ณดํ†ต ์ปคํ”ผ, ์„คํƒ• ์ปคํ”ผ, ๋ธ”๋ž™ ์ปคํ”ผ์˜ 3์ข…๋ฅ˜๋งŒ ํŒ๋งคํ•œ๋‹ค. ๋‹จ์ˆœํ™”๋ฅผ ์œ„ํ•ด ์‹คํ–‰ ์‚ด์—๋Š” ์ด 3์ธ๋ถ„์˜ ์žฌ๋ฃŒ๋งŒ ๊ฐ€์ง€๋„๋ก ํ•˜์˜€๋‹ค. ์ปคํ”ผ ๋ฉ”๋‰ด์— ๋”ฐ๋ผ ํ•„์š”ํ•œ ์žฌ๋ฃŒ๋“ค์ด ํ•˜๋‚˜์”ฉ ์—†์–ด์ง„๋‹ค. ๊ฐ์ฒด ์ง€ํ–ฅ ๊ตฌ์กฐ์— ๋”ฐ๋ผ ํ•„์š”ํ•œ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์™„์„ฑํ•˜๋ผ. 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 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 #include #include using namespace std; class Material { protected: string name; int amount; public: string getName() { return name; } int getAmount() { return amount; } void setAmount(int amount) { this->amount = amount; } bool subAmount(int amount) { if(this->amount <= 0) return false; else this->amount -= amount; return true; } }; class Coffee : public Material { public: Coffee() { name = "Coffee"; amount = 3; } }; class Sugar : public Material { public: Sugar() { name = "Sugar"; amount = 3; } }; class Cream : public Material { public: Cream() { name = "Cream"; amount = 3; } }; class Water : public Material { public: Water() { name = "Water"; amount = 3; } }; class Cup : public Material { public: Cup() { name = "Cup"; amount = 3; } }; class CoffeeMachine { Material *mat[]; // Material์˜ ๊ฐ์ฒด ๋ฐฐ์—ด ํฌ์ธํ„ฐ ์ƒ์„ฑ public: CoffeeMachine() { // ์ƒ์„ฑ์ž์—์„œ ์•ˆ๋‚ด ๋ฉ˜ํŠธ์™€ ์žฌ๋ฃŒ ์ƒํƒœ ์ถœ๋ ฅ cout << "-----๋ช…ํ’ˆ ์ปคํ”ผ ์žํŒ๊ธฐ ์ผญ๋‹ˆ๋‹ค.-----" << endl; mat[0] = new Coffee(); mat[1] = new Sugar(); mat[2] = new Cream(); mat[3] = new Water(); mat[4] = new Cup(); showCoffeeMachineState(); cout << endl; } void showCoffeeMachineState() { // ์žฌ๋ฃŒ ์ƒํƒœ ์ถœ๋ ฅ for(int i=0; i<5; i++) { cout << setw(10) << mat[i]->getName(); for(int j=0; j<mat[i]->getAmount(); j++) cout << "*"; cout << endl; } } void start() { // ๋ฉ”๋‰ด ์ถœ๋ ฅ ์‹œ์ž‘ int num; while(true) { showMenu(); num = selectMenu(); if(num == 3) { // ์ฑ„์šฐ๊ธฐ for(int i=0; i<5; i++) { mat[i]->setAmount(3); } cout << "๋ชจ๋“  ํ†ต์„ ์ฑ„์›๋‹ˆ๋‹ค~~" << endl; showCoffeeMachineState(); cout << endl; continue; } else if(num == 4) { // ์ข…๋ฃŒ cout << "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค..." << endl; exit(0); } if(mat[0]->subAmount(1) == false) { // coffee-1 cout << "์žฌ๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค." << endl; showCoffeeMachineState(); continue; } if(mat[3]->subAmount(1) == false) { // water-1 cout << "์žฌ๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค." << endl; showCoffeeMachineState(); continue; } if(mat[4]->subAmount(1) == false) { // cup-1 cout << "์žฌ๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค." << endl; showCoffeeMachineState(); continue; } // ๊ธฐ๋ณธ ์žฌ๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•˜์ง€ ์•Š์œผ๋ฉด ์‹คํ–‰ switch(num) { case 0: // ๋ณดํ†ต ์ปคํ”ผ๋Š” cream ์ถ”๊ฐ€ ์†Œ๋ชจ if(mat[2]->subAmount(1) == false) { // cream-1 cout << "์žฌ๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค." << endl; showCoffeeMachineState(); continue; } cout << "๋ง›์žˆ๋Š” ๋ณดํ†ต ์ปคํ”ผ ๋‚˜์™”์Šต๋‹ˆ๋‹ค~~" << endl; showCoffeeMachineState(); cout << endl; break; case 1: // ์„คํƒ• ์ปคํ”ผ๋Š” sugar ์ถ”๊ฐ€ ์†Œ๋ชจ if(mat[1]->subAmount(1) == false) { // sugar-1 cout << "์žฌ๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค." << endl; showCoffeeMachineState(); continue; } cout << "๋ง›์žˆ๋Š” ์„คํƒ• ์ปคํ”ผ ๋‚˜์™”์Šต๋‹ˆ๋‹ค~~" << endl; showCoffeeMachineState(); cout << endl; break; case 2: // ๋ธ”๋ž™ ์ปคํ”ผ๋Š” ์ถ”๊ฐ€ ์†Œ๋ชจ ์—†์Œ cout << "๋ง›์žˆ๋Š” ๋ธ”๋ž™ ์ปคํ”ผ ๋‚˜์™”์Šต๋‹ˆ๋‹ค~~" << endl; showCoffeeMachineState(); break; default : // ์ž˜๋ชป ์ž…๋ ฅ cout << "์ž˜๋ชป ์ž…๋ ฅ ํ•˜์…จ์Šต๋‹ˆ๋‹ค." << endl << endl; break; } } } void showMenu() { cout << "๋ณดํ†ต ์ปคํ”ผ:0, ์„คํƒ• ์ปคํ”ผ:1, ๋ธ”๋ž™ ์ปคํ”ผ:2, ์ฑ„์šฐ๊ธฐ:3, ์ข…๋ฃŒ:4>> "; } int selectMenu() { int num; cin >> num; return num; } }; int main() { cout.setf(ios::left); CoffeeMachine c; c.start(); } Explanation: ...

April 2, 2020 ยท 4 min ยท Sobamemil

C++ Programming Ch.11 Exercise 11 Solution

Problem: ๋‹ค์Œ์€ ํ”„๋กœ๊ทธ๋žจ๊ณผ Execution Result๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. pos ์กฐ์ž‘์ž๋ฅผ ์ž‘์„ฑํ•˜๋ผ. 1 2 3 4 5 6 7 8 9 #include using namespace std; int main() { int x, y; cin >> pos >> x; cin >> pos >> y; cout << x << ',' << y << endl; } Execution Result: Objective & Hints: ์กฐ์ž‘์ž ์ž‘์„ฑ ์—ฐ์Šต Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include using namespace std; istream& pos (istream& ins) { // pos ์กฐ์ž‘์ž cout << "์œ„์น˜๋Š”? "; return ins; } int main() { int x, y; cin >> pos >> x; cin >> pos >> y; cout << x << ',' << y << endl; } ]( ...

April 2, 2020 ยท 1 min ยท Sobamemil

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

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

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

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

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

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

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