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

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

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

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

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

C++ Programming Ch.10 Exercise 8 Solution

Problem: 문제 7을 ν‘ΈλŠ” λ‹€λ₯Έ 방법을 μ†Œκ°œν•œλ‹€. bigger() ν•¨μˆ˜μ˜ λ‹€μŒ λΌμΈμ—μ„œ > μ—°μ‚°μž λ•Œλ¬Έμ— 1 if(a > b) return a; T에 Circleκ³Ό 같은 클래슀 νƒ€μž…μ΄ λŒ€μž…λ˜λ©΄, ꡬ체화가 μ‹€νŒ¨ν•˜μ—¬ 컴파일 였λ₯˜κ°€ λ°œμƒν•œλ‹€. 이 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ λ‹€μŒκ³Ό 같은 좔상 클래슀 Comparable을 μ œμ•ˆν•œλ‹€. 1 2 3 4 5 6 class Comparable { public: virtual bool operator > (Comparable& op2) = 0; // 순수 가상 ν•¨μˆ˜ virtual bool operator < (Comparable& op2) = 0; // 순수 가상 ν•¨μˆ˜ virtual bool operator == (Comparable& op2) = 0; // 순수 가상 ν•¨μˆ˜ }; Circle ν΄λž˜μŠ€κ°€ Comparable을 상속받아 순수 가상 ν•¨μˆ˜λ₯Ό λͺ¨λ‘ κ΅¬ν˜„ν•˜λ©΄, μ•žμ˜ bigger() ν…œν”Œλ¦Ώ ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ”λ° 아무 λ¬Έμ œκ°€ μ—†λ‹€. ...

March 9, 2020 Β· 3 min Β· Sobamemil

C++ Programming Ch.10 Exercise 7 Solution

Problem: λ‹€μŒ ν”„λ‘œκ·Έλž¨μ€ 컴파일 였λ₯˜κ°€ λ°œμƒν•œλ‹€. μ†ŒμŠ€μ˜ μ–΄λ””μ—μ„œ μ™œ 컴파일 였λ₯˜κ°€ λ°œμƒν•˜λŠ”κ°€? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include using namespace std; class Circle { int radius; public: Circle(int radius = 1) { this->radius = radius; } int getRadius() { return radius; } }; template T bigger(T a, T b) { // 두 개의 맀개 λ³€μˆ˜λ₯Ό λΉ„κ΅ν•˜μ—¬ 큰 값을 리턴 if (a > b) return a; else return b; } int main() { int a = 20, b = 50, c; c = bigger(a, b); cout << "20κ³Ό 50쀑 큰 값은 " << c << endl; Circle waffle(10), pizza(20), y; y = bigger(waffle, pizza); cout << "waffleκ³Ό pizza 쀑 큰 κ²ƒμ˜ λ°˜μ§€λ¦„μ€ " << y.getRadius() << endl; } Execution Result: ...

March 9, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.10 Exercise 6 Solution

Problem: λ‹€μŒ ν•¨μˆ˜λŠ” 맀개 λ³€μˆ˜λ‘œ μ£Όμ–΄μ§„ int λ°°μ—΄ srcμ—μ„œ λ°°μ—΄ minus에 λ“€μ–΄μžˆλŠ” 같은 μ •μˆ˜λ₯Ό λͺ¨λ‘ μ‚­μ œν•œ μƒˆλ‘œμš΄ int 배열을 λ™μ μœΌλ‘œ ν• λ‹Ήλ°›μ•„ λ¦¬ν„΄ν•œλ‹€. retSizeλŠ” remove() ν•¨μˆ˜μ˜ Execution Resultλ₯Ό λ¦¬ν„΄ν•˜λŠ” λ°°μ—΄μ˜ 크기λ₯Ό μ „λ‹¬λ°›λŠ”λ‹€. 1 int * remove(int src[], int sizeSrc, int minus[], int sizeMinus, int& resSize); ν…œν”Œλ¦Ώμ„ μ΄μš©ν•˜μ—¬ removeλ₯Ό μΌλ°˜ν™”ν•˜λΌ. 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 #include using namespace std; template T* remove(T src[], int sizeSrc, T minus[], int sizeMinus, int & retsize){ int j; T* tmpArray = new T[sizeSrc]; for(int i=0; i<sizeSrc; i++){ for(j=0; j<sizeMinus; j++){ if(src[i] == minus[j]){ // src의 μ›μ†Œμ™€ minus의 μ›μ†Œκ°€ κ°™μœΌλ©΄ j의 값을 ν•˜λ‚˜ λ‚΄λ¦° ν›„ break --j; break; } } if(j==sizeMinus){ // j==sizeMinus λΌλŠ”κ±΄ src와 minus에 같은 μ›μ†Œκ°€ μ—†μ–΄ 쀑간에 break λ˜μ§€ μ•Šμ€ 경우 tmpArray[retsize] = src[i]; // src[i]λ₯Ό 동적 ν• λ‹Ή ν•˜μ—¬ μƒμ„±ν•œ tmpArray에 μ‚½μž… retsize++; // return ν•  size의 값을 ν•˜λ‚˜ 올렀쀌 } } return tmpArray; } int main() { int a[] = { 1,2,3,4,5,6,7,8,9,10 }; int b[] = { 5,6,7,8,9 }; int size=0; int *p = remove(a, 10, b,5, size); for (int i = 0; i < size; ++i) cout << p[i] << ' '; cout << endl; delete[] p; size = 0; char c[] = { 'a','b','i','m','c','d','e',}; char d[] = { 'k','i','m','n','u' }; char *q = remove(c, 7, d, 5,size); for (int i = 0; i < size; ++i) cout << q[i] << ' '; cout << endl; delete[] q; } Explanation: ...

March 9, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.10 Exercise 5 Solution

Problem: λ‹€μŒ ν•¨μˆ˜λŠ” 맀개 λ³€μˆ˜λ‘œ μ£Όμ–΄μ§„ 두 개의 int 배열을 μ—°κ²°ν•œ μƒˆλ‘œμš΄ int 배열을 동적 ν• λ‹Ήλ°›μ•„ λ¦¬ν„΄ν•œλ‹€. 1 int * concat(int a[], int sizea, int b[], int sizeb); concatκ°€ int 배열뿐 μ•„λ‹ˆλΌ λ‹€λ₯Έ νƒ€μž…μ˜ 배열도 μ²˜λ¦¬ν•  수 μžˆλ„λ‘ μΌλ°˜ν™”ν•˜λΌ. 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 #include using namespace std; template T* concat(T a[], int sizea, T b[], int sizeb){ T *rArray = new T[sizea + sizeb]; // return ν•  배열을 동적생성 for(int i=0; i<sizea+sizeb; i++){ if(i<sizea) rArray[i] = a[i]; else rArray[i] = b[i-sizea]; } return rArray; } int main() { int x[] = { 1, 10, 100, 5, 4 }; int y[] = { 7, 6, 10, 9 }; int *a = concat(x, 5, y, 4); int aSize = sizeof(x)/sizeof(x[0]) + sizeof(y)/sizeof(y[0]); // a에 λ“€μ–΄μžˆλŠ” μ›μ†Œμ˜ 개수 for (int i = 0; i<aSize; i++) cout << a[i] << ' '; }

March 9, 2020 Β· 1 min Β· Sobamemil

C++ Programming Ch.10 Exercise 4 Solution

Problem: λ°°μ—΄μ—μ„œ μ›μ†Œλ₯Ό κ²€μƒ‰ν•˜λŠ” search() ν•¨μˆ˜λ₯Ό ν…œν”Œλ¦ΏμœΌλ‘œ μž‘μ„±ν•˜λΌ. search()의 첫 번째 맀개 λ³€μˆ˜λŠ” κ²€μƒ‰ν•˜κ³ μž ν•˜λŠ” μ›μ†Œ 값이고, 두 번째 맀개 λ³€μˆ˜λŠ” 배열이며, μ„Έ 번째 맀개 λ³€μˆ˜λŠ” λ°°μ—΄μ˜ κ°œμˆ˜μ΄λ‹€. search() ν•¨μˆ˜κ°€ 검색에 μ„±κ³΅ν•˜λ©΄ trueλ₯Ό, μ•„λ‹ˆλ©΄ falseλ₯Ό λ¦¬ν„΄ν•œλ‹€. search()의 호좜 μ‚¬λ‘€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 int x[] = {1, 10, 100, 5, 4}; if(search(100, x, 5)) cout << "100이 λ°°μ—΄ x에 ν¬ν•¨λ˜μ–΄ μžˆλ‹€"; // 이 cout μ‹€ν–‰ else cout << "100이 λ°°μ—΄ x에 ν¬ν•¨λ˜μ–΄ μžˆμ§€ μ•Šλ‹€"; Execution Result: ...

March 9, 2020 Β· 1 min Β· Sobamemil