πŸ’» Dev & CSC++ Programming

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

C++ Programming Ch.7 Exercise 12 Solution

Problem: μ •μˆ˜ 배열을 항상 증가 순으둜 μœ μ§€ν•˜λŠ” SortedArray 클래슀λ₯Ό μž‘μ„±ν•˜λ €κ³  ν•œλ‹€. μ•„λž˜μ˜ main() ν•¨μˆ˜κ°€ μž‘λ™ν•  만큼만 SortedArray 클래슀λ₯Ό μž‘μ„±ν•˜κ³  +와 = μ—°μ‚°μžλ„ μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 10 11 12 13 class SortedArray{ int size; // ν˜„μž¬ λ°°μ—΄μ˜ 크기 int *p; // μ •μˆ˜ 배열에 λŒ€ν•œ 포인터 void sort(); // μ •μˆ˜ 배열을 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ public: SortedArray(); // pλŠ” NULL둜 sizeλŠ” 0으둜 μ΄ˆκΈ°ν™” SortedArray(SortedArray& src); // 볡사 μƒμ„±μž SortedArray(int p[], int size); // μƒμ„±μž. μ •μˆ˜ λ°°μ—΄κ³Ό 크기λ₯Ό μ „λ‹¬λ°›μŒ ~SortedArray(); // μ†Œλ©Έμž SortedArray operator+ (SortedArray& op2); // ν˜„μž¬ 배열에 op2 λ°°μ—΄ μΆ”κ°€ SortedArray& operator= (const SortedArray& op2); // ν˜„μž¬ 배열에 op2 λ°°μ—΄ 볡사 void show(); // λ°°μ—΄μ˜ μ›μ†Œ 좜λ ₯ }; 1 2 3 4 5 6 7 8 9 10 11 12 int main() { int n[] = { 2, 20, 6 }; int m[] = { 10, 7, 8, 30 }; SortedArray a(n, 3), b(m, 4), c; c = a + b; // +, = μ—°μ‚°μž μž‘μ„± ν•„μš” // + μ—°μ‚°μžκ°€ SortedArray 객체λ₯Ό λ¦¬ν„΄ν•˜λ―€λ‘œ 볡사 μƒμ„±μž ν•„μš” a.show(); b.show(); c.show(); } Execution Result: ...

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

C++ Programming Ch.7 Exercise 9 Solution

Problem: 문제 8번의 Circle 객체에 λŒ€ν•΄ λ‹€μŒ 연산이 κ°€λŠ₯ν•˜λ„λ‘ μ—°μ‚°μžλ₯Ό κ΅¬ν˜„ν•˜λΌ. 1 2 3 4 Circle a(5), b(4); b = 1+a; // b의 λ°˜μ§€λ¦„μ„ a의 λ°˜μ§€λ¦„μ— 1을 λ”ν•œ κ²ƒμœΌλ‘œ λ³€κ²½ a.show(); b.show(); 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 #include using namespace std; class Circle{ int radius; public: Circle(int radius=0) { this->radius = radius; } void show() { cout << "radius = " << radius << " 인 원" << endl; } friend Circle operator+ (int x, Circle c); }; Circle operator+ (int x, Circle c){ c.radius += x; return c; } int main() { Circle a(5), b(4); b = 1+a; // b의 λ°˜μ§€λ¦„μ„ a의 λ°˜μ§€λ¦„μ— 1을 λ”ν•œ κ²ƒμœΌλ‘œ λ³€κ²½ a.show(); b.show(); }

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

C++ Programming Ch.7 Exercise 8 Solution

Problem: 원을 μΆ”μƒν™”ν•œ Circle ν΄λž˜μŠ€λŠ” κ°„λ‹¨νžˆ μ•„λž˜μ™€ κ°™λ‹€. 1 2 3 4 5 6 class Circle{ int radius; public: Circle(int radius=0) { this->radius = radius; } void show() { cout << "radius = " << radius << " 인 원" << endl; } }; λ‹€μŒ 연산이 κ°€λŠ₯ν•˜λ„λ‘ μ—°μ‚°μžλ₯Ό ν”„λ Œλ“œ ν•¨μˆ˜λ‘œ μž‘μ„±ν•˜λΌ. 1 2 3 4 5 Circle a(5), b(4); ++a; // λ°˜μ§€λ¦„μ„ 1 증가 μ‹œν‚¨λ‹€. b = a++; // λ°˜μ§€λ¦„μ„ 1 증가 μ‹œν‚¨λ‹€. a.show(); b.show(); Execution Result: ...

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

C++ Programming Ch.7 Exercise 4 Solution

Problem: 1번 ~ 4번 λ¬Έμ œκΉŒμ§€ μ‚¬μš©λ  Book ν΄λž˜μŠ€λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Book{ string title; int price, pages; public: Book(string title="", int price=0, int pages=0){ this->title = title; this->price = price; this->pages = pages; } void show() { cout << title << " " << price << "원 " << pages << " νŽ˜μ΄μ§€" << endl; } string getTitle() { return title; } }; λ‹€μŒ 연산을 톡해 μ±…μ˜ 제λͺ©μ„ 사전 순으둜 λΉ„κ΅ν•˜κ³ μž ν•œλ‹€. ...

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

C++ Programming Ch.7 Exercise 3 Solution

Problem: 1번 ~ 4번 λ¬Έμ œκΉŒμ§€ μ‚¬μš©λ  Book ν΄λž˜μŠ€λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Book{ string title; int price, pages; public: Book(string title="", int price=0, int pages=0){ this->title = title; this->price = price; this->pages = pages; } void show() { cout << title << " " << price << "원 " << pages << " νŽ˜μ΄μ§€" << endl; } string getTitle() { return title; } }; λ‹€μŒ 연산을 톡해 곡짜 책인지λ₯Ό νŒλ³„ν•˜λ„λ‘ ! μ—°μ‚°μžλ₯Ό μž‘μ„±ν•˜λΌ. ...

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

C++ Programming Ch.7 Exercise 2 Solution

Problem: 1번 ~ 4번 λ¬Έμ œκΉŒμ§€ μ‚¬μš©λ  Book ν΄λž˜μŠ€λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Book{ string title; int price, pages; public: Book(string title="", int price=0, int pages=0){ this->title = title; this->price = price; this->pages = pages; } void show() { cout << title << " " << price << "원 " << pages << " νŽ˜μ΄μ§€" << endl; } string getTitle() { return title; } }; Book 객체λ₯Ό ν™œμš©ν•˜λŠ” 사둀이닀. ...

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

λͺ…ν’ˆ C++ programming μ‹€μŠ΅λ¬Έμ œ 2μž₯ 3번

Problem: ν‚€λ³΄λ“œλ‘œλΆ€ν„° 두 개의 μ •μˆ˜λ₯Ό 읽어 큰 수λ₯Ό 화면에 좜λ ₯ν•˜λΌ. Objective & Hints: cin ν™œμš©, ν‚€λ³΄λ“œλ‘œλΆ€ν„° μ •μˆ˜ 읽기 Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include using namespace std; int main(){ int a,b; cout << "두 수λ₯Ό μž…λ ₯ν•˜λΌ>>"; cin >> a >> b; // cin μ—°μ‚°μžλ₯Ό μ΄μš©ν•˜μ—¬ λ‘κ°œμ˜ μ •μˆ˜ μž…λ ₯λ°›κΈ° cout << "큰 수 = "; if(a<b) cout << b; else cout << a; return 0; } Explanation: ...

February 28, 2020 Β· 1 min Β· Sobamemil