πŸ’» 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 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
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 13 Solution

Problem: map μ»¨ν…Œμ΄λ„ˆλ₯Ό μ΄μš©ν•˜μ—¬ (이름, 성적)을 μ €μž₯ν•˜κ³  μ΄λ¦„μœΌλ‘œ 성적을 μ‘°νšŒν•˜λŠ” 점수 관리 ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“€μ–΄λΌ. 이름은 빈칸 없이 μž…λ ₯ν•˜λŠ” 것을 μ›μΉ™μœΌλ‘œ ν•œλ‹€. Execution Result: Objective & Hints: map μ»¨ν…Œμ΄λ„ˆ ν™œμš© 이름과 점수λ₯Ό 쌍으둜 μ €μž₯ν•  λ§΅ μ»¨ν…Œμ΄λ„ˆλ‘œ map<string, int>λ₯Ό μ΄μš©ν•˜λ©΄ λœλ‹€. 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 #include #include #include using namespace std; int main() { map<string, int> Score; // map μ»¨ν…Œμ΄λ„ˆ 생성. ν‚€λŠ” ν•œκΈ€ 이름, 값은 μ •μˆ˜ 점수 cout << "***** μ μˆ˜κ΄€λ¦¬ ν”„λ‘œκ·Έλž¨ HIGH SCORE을 μ‹œμž‘ν•©λ‹ˆλ‹€ *****\n"; while(true){ int num; int score; string name; cout << "μž…λ ₯:1, 쑰회:2, μ’…λ£Œ:3 >> "; cin >> num; switch (num){ case 1: cout << "이름과 점수>> "; cin >> name >> score; Score.insert(make_pair(name, score)); // map에 μ €μž₯ break; case 2: cout << "이름 >> "; cin >> name; if(Score.find(name) == Score.end()) // name 'ν‚€'λ₯Ό λκΉŒμ§€ μ°Ύμ•˜λŠ”λ° μ—†μŒ cout << "μ—†μŒ" << endl; else cout << name << "의 μ μˆ˜λŠ” " <<Score[name] << endl; // Scoreμ—μ„œ name의 값을 μ°Ύμ•„ 좜λ ₯ break; case 3: cout << "ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•©λ‹ˆλ‹€...\n"; return 0; default : cout << "μ œλŒ€λ‘œ μž…λ ₯ν•΄\n"; break; } } } Explanation: ...

March 11, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 12 Solution

Problem: Open Challengeλ₯Ό μˆ˜μ •ν•˜μ—¬ μ‚¬μš©μžκ°€ μ–΄νœ˜λ₯Ό μ‚½μž…ν•  수 μžˆλ„λ‘ κΈ°λŠ₯을 μΆ”κ°€ν•˜λΌ. Execution ResultλŠ” λ‹€μŒκ³Ό κ°™λ‹€. Execution Result: Objective & Hints: vector μ»¨ν…Œμ΄λ„ˆμ˜ μ’…ν•© μ‘μš© μ—°μŠ΅ 랜덀 μ •μˆ˜λ₯Ό λ°©μƒμ‹œν‚€κΈ° μœ„ν•΄ λ‹€μŒ 두 라인의 μ½”λ“œκ°€ ν•„μš”ν•˜λ©°, κ³Ό λ₯Ό include ν•΄μ•Ό ν•©λ‹ˆλ‹€. 1 2 srand((unsigned)time(0)); // μ‹œμž‘ν•  λ•Œλ§ˆλ‹€, λ‹€λ₯Έ 랜덀수λ₯Ό λ°œμƒμ‹œν‚€κΈ° μœ„ν•œ seed μ„€μ • int n = rand(); // 0μ—μ„œ RAND_MAX(32767) μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜κ°€ n에 λ°œμƒ 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 #include #include #include #include using namespace std; class Word { string engWord, korWord; public: Word(string engWord=0, string korWord=0){ this->engWord = engWord; this->korWord = korWord; } void inputWord(string engWord, string korWord){ this->engWord = engWord; this->korWord = korWord; } string getEngWord(){ return engWord; } string getKorWord(){ return korWord; } }; void inputWord(vector &v) { cout << "μ˜μ–΄ 단어에 exit을 μž…λ ₯ν•˜λ©΄ μž…λ ₯ 끝\n"; string engWord, korWord; while(true){ cout << "μ˜μ–΄ >>"; cin >> engWord; if(engWord == "exit") break; cout << "ν•œκΈ€ >>"; cin >> korWord; v.push_back(Word(engWord, korWord)); } } void gameStart(vector &v) { srand((unsigned)time(0)); int num; cout << "μ˜μ–΄ μ–΄νœ˜ ν…ŒμŠ€νŠΈλ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€. 1~4 μ™Έ λ‹€λ₯Έ μž…λ ₯μ‹œ μ’…λ£Œ.\n"; while(true){ int randNum = rand()%v.size(); cout << v.at(randNum).getEngWord() << "?\n"; string ex[4] = " "; int tmp = rand()%4; ex[tmp] = v.at(randNum).getKorWord(); for(int i=0; i<4; i++){ if(tmp == i) continue; while(true){ // μ€‘λ³΅λ˜λŠ” μˆ«μžκ°€ μ—†κ²Œ 처리 int numChosen = rand()%v.size(); if(ex[0]!=v.at(numChosen).getKorWord() && ex[1]!=v.at(numChosen).getKorWord() && ex[2]!=v.at(numChosen).getKorWord() && ex[3]!=v.at(numChosen).getKorWord()){ ex[i] = v.at(numChosen).getKorWord(); break; } } } for(int i=0; i<4; i++) cout << "(" << i+1 << ") " << ex[i] << ' '; cout << ":>"; cin >> num; // 숫자만 μž…λ ₯ if(num == -1

March 10, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 9 Solution

Problem: STL의 vector 클래슀λ₯Ό μ΄μš©ν•˜λŠ” κ°„λ‹¨ν•œ ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•΄λ³΄μž. vector 객체λ₯Ό μƒμ„±ν•˜κ³ , ν‚€λ³΄λ“œλ‘œλΆ€ν„° μ •μˆ˜λ₯Ό μž…λ ₯받을 λ•Œλ§ˆλ‹€ μ •μˆ˜λ₯Ό 벑터에 μ‚½μž…ν•˜κ³  μ§€κΈˆκΉŒμ§€ μž…λ ₯된 μˆ˜μ™€ 평균을 좜λ ₯ν•œλŠ ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. 0을 μž…λ ₯ν•˜λ©΄ ν”„λ‘œκ·Έλž¨μ΄ μ’…λ£Œλœλ‹€. Execution Result: Objective & Hints: vector μ»¨ν…Œμ΄λ„ˆ ν™œμš© μ—°μŠ΅ μ •μˆ˜λ§Œ λ‹€λ£¨λŠ” λ²‘ν„°μ΄λ―€λ‘œ vector v;λ₯Ό μ΄μš©ν•˜λ©΄ λœλ‹€. iteratorλ₯Ό μ‚¬μš©ν•  ν•„μš”λŠ” μ—†λ‹€. Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include using namespace std; int main() { vector v; double sum=0; while(true){ int tmp; cout << "μ •μˆ˜λ₯Ό μž…λ ₯ν•˜μ„Έμš”(0을 μž…λ ₯ν•˜λ©΄ μ’…λ£Œ)>>"; cin >> tmp; if(!tmp) break; //μž…λ ₯ν•œ μ •μˆ˜κ°€ 0이면 μ’…λ£Œ v.push_back(tmp); // μž…λ ₯ν•œ μ •μˆ˜κ°€ 0이 μ•„λ‹ˆλ©΄ v에 μ‚½μž… for(int i=0; i<v.size(); i++) // vector v의 λͺ¨λ“  μ›μ†Œ 좜λ ₯ cout << v.at(i) << ' '; cout << endl; sum += tmp; cout << "평균 = " << sum/v.size() << endl; } } Explanation: ...

March 10, 2020 Β· 1 min Β· Sobamemil