πŸ’» Dev & CSC++ Programming

C++ Programming Ch.4 Exercise 13 Solution

Problem: 영문자둜 κ΅¬μ„±λœ ν…μŠ€νŠΈμ— λŒ€ν•΄ 각 μ•ŒνŒŒλ²³μ— ν•΄λ‹Ήν•˜λŠ” λ¬Έμžκ°€ λͺ‡ κ°œμΈμ§€ 좜λ ₯ν•˜λŠ” νžˆμŠ€ν† κ·Έλž¨ 클래슀 Histogram을 λ§Œλ“€μ–΄λ³΄μž. λŒ€λ¬ΈμžλŠ” λͺ¨λ‘ μ†Œλ¬Έμžλ‘œ λ³€ν™˜ν•˜μ—¬ μ²˜λ¦¬ν•œλ‹€. Histogram 클래슀λ₯Ό ν™œμš©ν•˜λŠ” 사둀와 Execution ResultλŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 Histogram elvisHisto("Wise men say, only fools rush in But I can't help, "); elvisHisto.put("falling in love with you"); elvisHisto.putc('-'); elvisHisto.put("Elvis Presley"); elvisHisto.print(); 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 #include #include using namespace std; class Histogram { string sent; public: Histogram(string sent); void put(string sent); void putc(char c); void print(); }; Histogram::Histogram(string text){ sent = text; cout << sent << endl; } void Histogram::put(string text){ sent += text; cout << text; } void Histogram::putc(char c){ sent += c; cout << c; } void Histogram::print(){ int alpha[26] = { 0 }; int num = 0; for (int i = 0; i < sent.length(); i++) { if(isalpha(sent[i])){ char c = tolower(sent[i]); alpha[c - 'a']++; num++; } } cout << endl << endl; cout << "총 μ•ŒνŒŒλ²³ 수 " << num; cout << endl << endl; for (int i = 0; i < 26; ++i) { char c = 'a' + i; cout << c << " (" << alpha[i] << ")\t: "; for (int j = 0; j < alpha[i]; ++j) { cout << "*"; } cout << endl; } } int main() { Histogram elvisHisto("Wise men say, only fools rush in But I can't help, "); elvisHisto.put("falling in love with you"); elvisHisto.putc('-'); elvisHisto.put("Elvis Presley"); elvisHisto.print(); } Explanation: ...

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

C++ Programming Ch.4 Exercise 12 Solution

Problem: λ‹€μŒμ€ 이름과 λ°˜μ§€λ¦„μ„ μ†μ„±μœΌλ‘œ κ°€μ§„ Circle ν΄λž˜μŠ€μ™€ 이듀을 λ°°μ—΄λ‘œ κ΄€λ¦¬ν•˜λŠ” CircleManager ν΄λž˜μŠ€μ΄λ‹€. 1 2 3 4 5 6 7 8 class Circle{ int radius; // μ›μ˜ λ°˜μ§€λ¦„ κ°’ string name; // μ›μ˜ 이름 public: void setCircle(string name, int radius); // 이름과 λ°˜μ§€λ¦„ μ„€μ • double getArea(); string getName(); }; 1 2 3 4 5 6 7 8 9 class CircleManager { Circle *p; // Circle 배열에 λŒ€ν•œ 포인터 int size; // λ°°μ—΄μ˜ 크기 public: CircleManager(int size); // size 크기의 배열을 동적 생성. μ‚¬μš©μžλ‘œλΆ€ν„° μž…λ ₯ μ™„λ£Œ ~CircleManager(); void searchByName(); // μ‚¬μš©μžλ‘œλΆ€ν„° μ›μ˜ 이름을 μž…λ ₯λ°›μ•„ 면적 좜λ ₯ void searchByArea(); // μ‚¬μš©μžλ‘œλΆ€ν„° 면적을 μž…λ ₯λ°›μ•„ 면적보닀 큰 μ›μ˜ 이름 좜λ ₯ }; ν‚€λ³΄λ“œμ—μ„œ μ›μ˜ 개수λ₯Ό μž…λ ₯λ°›κ³ , κ·Έ 개수만큼 μ›μ˜ 이름과 λ°˜μ§€λ¦„μ„ μž…λ ₯λ°›κ³ , λ‹€μŒκ³Ό 같이 μ‹€ν–‰λ˜λ„λ‘ main() ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜λΌ. ...

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

C++ Programming Ch.4 Exercise 11 Solution

Problem: λ‹€μŒμ€ μ»€ν”ΌμžνŒκΈ°λ‘œ μž‘λ™ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“€κΈ° μœ„ν•΄ ν•„μš”ν•œ 두 ν΄λž˜μŠ€μ΄λ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class CoffeeVendingMachine { // μ»€ν”ΌμžνŒκΈ°λ₯Ό ν‘œν˜„ν•˜λŠ” 클래슀 Container tong[3]; // tong[0]λŠ” 컀피, tong[1]은 λ¬Ό, tong[2]λŠ” 섀탕톡을 λ‚˜νƒ€λƒ„ void fill(); // 3개의 톡을 λͺ¨λ‘ 10으둜 채움 void selectEspresso(); // μ—μŠ€ν”„λ ˆμ†Œλ₯Ό μ„ νƒν•œ 경우, 컀피 1, λ¬Ό 1 μ†Œλͺ¨ void selectAmericano(); // 아메리카노λ₯Ό μ„ νƒν•œ 경우, 컀피 1, λ¬Ό 2 μ†Œλͺ¨ void selectSugarCoffee(); // 섀탕컀피λ₯Ό μ„ νƒν•œ 경우, 컀피 1, λ¬Ό 2, 섀탕 1 μ†Œλͺ¨ void show(); // ν˜„μž¬ 컀피, λ¬Ό, μ„€νƒ•μ˜ μž”λŸ‰ 좜λ ₯ public: void run(); // 컀피 자판기 μž‘λ™ }; class Container { // 톡 ν•˜λ‚˜λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 클래슀 int size; // ν˜„μž¬ μ €μž₯ λŸ‰, μ΅œλŒ€ μ €μž₯λŸ‰μ€ 10 public: Container() {size = 10;} void fill(); // μ΅œλŒ€λŸ‰(10)으둜 μ±„μš°κΈ° void consume(); // 1 만큼 μ†Œλͺ¨ν•˜κΈ° int getSize(); // ν˜„μž¬ 크기 리턴 }; λ‹€μŒκ³Ό 같이 μ‹€ν–‰λ˜λ„λ‘ main() ν•¨μˆ˜μ™€ CoffeeVendingMachine, Containerλ₯Ό μ™„μ„±ν•˜λΌ. ...

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

C++ Programming Ch.4 Exercise 10 Solution

Problem: λ‹€μŒμ—μ„œ Person은 μ‚¬λžŒμ„, FamilyλŠ” 가쑱을 μΆ”μƒν™”ν•œ ν΄λž˜μŠ€λ‘œμ„œ μ™„μ„±λ˜μ§€ μ•Šμ€ ν΄λž˜μŠ€μ΄λ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Person { string name; public: Person(string name) { this->name = name;} string getName() { return name;} }; class Family { Person *p; // Person λ°°μ—΄ 포인터 int size; // Person λ°°μ—΄μ˜ 크기. κ°€μ‘± ꡬ성원 수 public: Family(string name, int size); // size 개수만큼 Person λ°°μ—΄ 동적 생성 void show(); // λͺ¨λ“  κ°€μ‘± ꡬ성원 좜λ ₯ ~Family(); }; λ‹€μŒ main()이 μž‘λ™ν•˜λ„λ‘ Personκ³Ό Family ν΄λž˜μŠ€μ— ν•„μš”ν•œ 멀버듀을 μΆ”κ°€ν•˜κ³  μ½”λ“œλ₯Ό μ™„μ„±ν•˜λΌ. ...

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

C++ Programming Ch.4 Exercise 9 Solution

Problem: λ‹€μŒκ³Ό 같은 Person ν΄λž˜μŠ€κ°€ μžˆλ‹€. Person ν΄λž˜μŠ€μ™€ main() ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ—¬, 3개의 Person 객체λ₯Ό κ°€μ§€λŠ” 배열을 μ„ μ–Έν•˜κ³ , λ‹€μŒκ³Ό 같이 ν‚€λ³΄λ“œμ—μ„œ 이름과 μ „ν™”λ²ˆν˜Έλ₯Ό μž…λ ₯λ°›μ•„ 좜λ ₯ν•˜κ³  κ²€μƒ‰ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 class Person{ string name; string tel; public: Person(); string getName() { return name; } string getTel() { return tel; } void set(string name, string tel); }; Objective & Hints: 객체 λ°°μ—΄κ³Ό string μ‘μš© μ—°μŠ΅ ...

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

C++ Programming Ch.4 Exercise 7 Solution

Problem: λ‹€μŒκ³Ό 같이 원을 μΆ”μƒν™”ν•œ Circle ν΄λž˜μŠ€κ°€ μžˆλ‹€. Circle ν΄λž˜μŠ€μ™€ main() ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜κ³  3개의 Circle 객체λ₯Ό κ°€μ§„ 배열을 μ„ μ–Έν•˜κ³ , λ°˜μ§€λ¦„ 값을 μž…λ ₯λ°›κ³  면적이 100보닀 큰 μ›μ˜ 개수λ₯Ό 좜λ ₯ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. Circle ν΄λž˜μŠ€λ„ μ™„μ„±ν•˜λΌ. 1 2 3 4 5 6 class Circle { int radius; // μ›μ˜ λ°˜μ§€λ¦„ κ°’ public: void setRadius(int radius); // λ°˜μ§€λ¦„μ„ μ„€μ •ν•œλ‹€. double getArea(); // 면적을 λ¦¬ν„΄ν•œλ‹€. }; Objective & Hints: 객체 λ°°μ—΄ 닀루기 μ—°μŠ΅ ...

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

C++ Programming Ch.4 Exercise 6 Solution

Problem: string 클래슀λ₯Ό μ΄μš©ν•˜μ—¬ μ‚¬μš©μžκ°€ μž…λ ₯ν•œ 영문 ν•œ 쀄을 λ¬Έμžμ—΄λ‘œ μž…λ ₯λ°›κ³  거꾸둜 좜λ ₯ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. Objective & Hints: string 클래슀둜 λ¬Έμžμ—΄ 닀루기 Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include using namespace std; int main() { string str; cout << "μ•„λž˜ ν•œ 쀄을 μž…λ ₯ν•˜μ„Έμš”.(exitλ₯Ό μž…λ ₯ν•˜λ©΄ μ’…λ£Œν•©λ‹ˆλ‹€)"; while(true){ cout << endl << ">>"; getline(cin,str); if(str == "exit") break; for(int i = str.length()-1; i>=0; i--) cout << str[i]; } } Explanation: ...

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

C++ Programming Ch.4 Exercise 5 Solution

Problem: string 클래슀λ₯Ό μ΄μš©ν•˜μ—¬ μ‚¬μš©μžκ°€ μž…λ ₯ν•œ 영문 ν•œ 쀄을 μž…λ ₯λ°›κ³  κΈ€μž ν•˜λ‚˜λ§Œ λžœλ€ν•˜κ²Œ μˆ˜μ •ν•˜μ—¬ 좜λ ₯ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. Objective & Hints: string 클래슀둜 λ¬Έμžμ—΄ 닀루기 랜덀 μ •μˆ˜λ₯Ό λ°œμƒμ‹œν‚€κΈ° μœ„ν•΄ λ‹€μŒ 두 라인의 μ½”λ“œκ°€ ν•„μš”ν•˜λ©°, 와 헀더 νŒŒμΌμ„ include ν•΄μ•Ό ν•œλ‹€. 1 2 srand((unsinged)time(0)); // μ‹œμž‘ν•  λ•Œλ§ˆλ‹€, λ‹€λ₯Έ 랜덀수λ₯Ό λ°œμƒμ‹œν‚€κΈ° μœ„ν•œ seed μ„€μ • int n = rand(); // 0μ—μ„œ RAND_MAX(32767) μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜ λ°œμƒ Execution Result: 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 #include #include #include #include using namespace std; int main() { string sent; int n; cout << "μ•„λž˜ ν•œ 쀄을 μž…λ ₯ν•˜μ„Έμš”.(exitλ₯Ό μž…λ ₯ν•˜λ©΄ μ’…λ£Œν•©λ‹ˆλ‹€)" ; while(true){ srand((unsigned)time(0)); cout << "\n>>"; getline(cin,sent); if(sent == "exit") break; int length = sent.length(); while(true){ n = rand()%length; if(sent[n]!=' ') break; } int a = rand()%25+95; // μž„μ˜μ˜ 문자 ν•˜λ‚˜ 선택 sent[n] = (char)a; cout << sent; } } Explanation: ...

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

C++ Programming Ch.4 Exercise 4 Solution

Problem: λ‹€μŒκ³Ό 같은 Sample ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 class Sample{ int *p; int size; public: Sample(int n) { // μƒμ„±μž size = n; p = new int [n]; // n개 μ •μˆ˜ λ°°μ—΄μ˜ 동적 생성 } void read(); // 동적 할당받은 μ •μˆ˜ λ°°μ—΄ p에 μ‚¬μš©μžλ‘œλΆ€ν„° μ •μˆ˜λ₯Ό μž…λ ₯ λ°›μŒ void write(); // μ •μˆ˜ 배열을 화면에 좜λ ₯ int big(); // μ •μˆ˜ λ°°μ—΄μ—μ„œ κ°€μž₯ 큰 수 리턴 ~Sample(); // μ†Œλ©Έμž }; λ‹€μŒ main() ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜λ„λ‘ Sample 클래슀λ₯Ό μ™„μ„±ν•˜λΌ. ...

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

C++ Programming Ch.4 Exercise 3 Solution

Problem: string 클래슀λ₯Ό μ΄μš©ν•˜μ—¬ λΉˆμΉΈμ„ ν¬ν•¨ν•˜λŠ” λ¬Έμžμ—΄μ„ μž…λ ₯λ°›κ³  λ¬Έμžμ—΄μ—μ„œ 'a'κ°€ λͺ‡κ°œ μžˆλŠ”μ§€ 좜λ ₯ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•΄λ³΄μž. Objective & Hints: getline(), string 클래슀 ν™œμš© Execution Result: Code: (1) λ¬Έμžμ—΄μ—μ„œ 'a'λ₯Ό μ°ΎκΈ° μœ„ν•΄ string 클래슀의 멀버 at()λ‚˜ []λ₯Ό μ΄μš©ν•˜μ—¬ μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include #include using namespace std; int main() { string str; cout << "λ¬Έμžμ—΄ μž…λ ₯>>"; getline(cin,str); int length = str.length(); int num = 0; for(int i=0; i<length; i++){ if(str[i] == 'a') num++; } cout << "문자 aλŠ” " << num << "개 μžˆμŠ΅λ‹ˆλ‹€." ; } (2) λ¬Έμžμ—΄μ—μ„œ 'a'λ₯Ό μ°ΎκΈ° μœ„ν•΄ string 클래슀의 find() 멀버 ν•¨μˆ˜λ₯Ό μ΄μš©ν•˜μ—¬ μž‘μ„±ν•˜λΌ. text.find('a', index);λŠ” text λ¬Έμžμ—΄μ˜ index μœ„μΉ˜λΆ€ν„° 'a'λ₯Ό μ°Ύμ•„ λ¬Έμžμ—΄ λ‚΄ 인덱슀λ₯Ό λ¦¬ν„΄ν•œλ‹€. ...

March 4, 2020 Β· 2 min Β· Sobamemil