πŸ’» 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 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 11 Solution

Problem: μ±…μ˜ 년도, 책이름, μ €μž 이름을 담은 Book 클래슀λ₯Ό λ§Œλ“€κ³ , vector v;둜 μƒμ„±ν•œ 벑터λ₯Ό μ΄μš©ν•˜μ—¬ 책을 μž…κ³ ν•˜κ³ , μ €μžμ™€ λ…„λ„λ‘œ κ²€μƒ‰ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. 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 #include #include #include using namespace std; class Book{ int year; string b_name; string p_name; public: void set(int year, string b_name, string p_name){ this->year = year; this->b_name = b_name; this->p_name = p_name; } string getP(){ return p_name; } int getY(){ return year; } void show(){ cout << year << "년도, " << b_name << ", " << p_name << endl; } }; int main() { vector v; Book b; int year; string b_name; string p_name; cout << "μž…κ³ ν•  책을 μž…λ ₯ν•˜μ„Έμš”. 년도에 -1을 μž…λ ₯ν•˜λ©΄ μž…κ³ λ₯Ό μ’…λ£Œν•©λ‹ˆλ‹€.\n"; while(true){ cout << "년도>>"; cin >> year; if(year==-1) break; fflush(stdin); cout << "책이름>>"; getline(cin, b_name); cout << "μ €μž>>"; getline(cin, p_name); b.set(year, b_name, p_name); v.push_back(b); } cout << "총 μž…κ³ λœ 책은 " << v.size() << "ꢌ μž…λ‹ˆλ‹€.\n"; cout << "κ²€μƒ‰ν•˜κ³ μž ν•˜λŠ” μ €μž 이름을 μž…λ ₯ν•˜μ„Έμš”>>"; fflush(stdin); getline(cin, p_name); for(int i=0; i<v.size(); i++){ if(v[i].getP() == p_name) v[i].show(); } cout << "κ²€μƒ‰ν•˜κ³ μž ν•˜λŠ” 년도λ₯Ό μž…λ ₯ν•˜μ„Έμš”>>"; cin >> year; for(int i=0; i<v.size(); i++){ if(v[i].getY() == year) v[i].show(); } } Explanation: ...

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

C++ Programming Ch.10 Exercise 10 Solution

Problem: λ‚˜λΌμ˜ μˆ˜λ„ λ§žμΆ”κΈ° κ²Œμž„μ— vectorλ₯Ό ν™œμš©ν•΄λ³΄μž. λ‚˜λΌ 이름(nation)κ³Ό μˆ˜λ„(capital) λ¬Έμžμ—΄λ‘œ κ΅¬μ„±λœ Nation 클래슀λ₯Ό λ§Œλ“€κ³ , vector v;둜 μƒμ„±ν•œ 벑터λ₯Ό μ΄μš©ν•˜μ—¬ λ‚˜λΌ 이름과 μˆ˜λ„ 이름을 μ‚½μž…ν•  μˆ˜λ„ 있고 λžœλ€ν•˜κ²Œ ν€΄μ¦ˆλ₯Ό λ³Ό μˆ˜λ„ μžˆλ‹€. ν”„λ‘œκ·Έλž¨ λ‚΄μ—μ„œ 벑터에 Nation 객체λ₯Ό μ—¬λŸ¬ 개 미리 μ‚½μž…ν•˜μ—¬ ν€΄μ¦ˆλ₯Ό 보도둝 ν•˜λΌ. μ‹€ν–‰ 화면은 λ‹€μŒκ³Ό κ°™μœΌλ©°, μ €μžλŠ” 9개 λ‚˜λΌμ˜ 이름과 μˆ˜λ„λ₯Ό 미리 ν”„λ‘œκ·Έλž¨μ—μ„œ μ‚½μž…ν•˜μ˜€λ‹€. λ¬Έμžμ—΄μ€ string 클래슀λ₯Ό μ΄μš©ν•˜λΌ. Execution Result: Objective & Hints: vector에 객체의 μ‚½μž…, 검색 μ‘μš© μ—°μŠ΅ ...

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