C++ Programming Ch.3 Exercise 10 Solution

Problem: λ‹€μˆ˜μ˜ 클래슀λ₯Ό μ„ μ–Έν•˜κ³  ν™œμš©ν•˜λŠ” κ°„λ‹¨ν•œ λ¬Έμ œμ΄λ‹€. λ”ν•˜κΈ°(+), λΉΌκΈ°(-), κ³±ν•˜κΈ°(*), λ‚˜λˆ„κΈ°(/)λ₯Ό μˆ˜ν–‰ν•˜λŠ” 4개의 클래슀λ₯Ό Add, Sub, Mul, Divλ₯Ό λ§Œλ“€κ³ μž ν•œλ‹€. 이듀은 λͺ¨λ‘ κ³΅ν†΅μœΌλ‘œ λ‹€μŒ 멀버λ₯Ό κ°€μ§„λ‹€. ● int νƒ€μž… λ³€μˆ˜ a, b : ν”Όμ—°μ‚°μž ● void setValue(int x, int y) ν•¨μˆ˜ : 맀개 λ³€μˆ˜ x, yλ₯Ό 멀버 a, b에 볡사 ● int calculate() ν•¨μˆ˜ : 연산을 μ‹€ν–‰ν•˜κ³  κ²°κ³Ό 리턴 main() ν•¨μˆ˜λŠ” Add, Sub, Mul, Div 클래슀 νƒ€μž…μ˜ 객체 a, s, m, dλ₯Ό μƒμ„±ν•˜κ³ , μ•„λž˜μ™€ 같이 ν‚€λ³΄λ“œλ‘œλΆ€ν„° 두 개의 μ •μˆ˜μ™€ μ—°μ‚°μžλ₯Ό μž…λ ₯λ°›κ³ , a, s, m, d 객체 μ€‘μ—μ„œ 연산을 μ²˜λ¦¬ν•  객체의 setValue() ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œ ν›„, calculate()λ₯Ό ν˜ΈμΆœν•˜μ—¬ κ²°κ³Όλ₯Ό 화면에 좜λ ₯ν•œλ‹€. ...

March 3, 2020 Β· 4 min Β· Sobamemil

C++ Programming Ch.3 Exercise 9 Solution

Problem: Oval ν΄λž˜μŠ€λŠ” μ£Όμ–΄μ§„ μ‚¬κ°ν˜•μ— λ‚΄μ ‘ν•˜λŠ” 타원을 μΆ”μƒν™”ν•œ ν΄λž˜μŠ€μ΄λ‹€. Oval 클래슀의 λ©€λ²„λŠ” λͺ¨λ‘ λ‹€μŒκ³Ό κ°™λ‹€. Oval 클래슀λ₯Ό 선언뢀와 κ΅¬ν˜„λΆ€λ‘œ λ‚˜λˆ„μ–΄ μž‘μ„±ν•˜λΌ. ● μ •μˆ˜κ°’μ˜ μ‚¬κ°ν˜• λ„ˆλΉ„μ™€ 높이λ₯Ό κ°€μ§€λŠ” width, height λ³€μˆ˜ 멀버 ● λ„ˆλΉ„μ™€ 높이 값을 맀개 λ³€μˆ˜λ‘œ λ°›λŠ” μƒμ„±μž ● λ„ˆλΉ„μ™€ 높이λ₯Ό 1둜 μ΄ˆκΈ°ν™”ν•˜λŠ” 맀개 λ³€μˆ˜ μ—†λŠ” μƒμ„±μž ● width와 heightλ₯Ό 좜λ ₯ν•˜λŠ” μ†Œλ©Έμž ● 타원이 λ„ˆλΉ„λ₯Ό λ¦¬ν„΄ν•˜λŠ” getWidth() ν•¨μˆ˜ 멀버 ● νƒ€μ›μ˜ 높이λ₯Ό λ¦¬ν„΄ν•˜λŠ” getHeight() ν•¨μˆ˜ 멀버 ● νƒ€μ›μ˜ λ„ˆλΉ„μ™€ 높이λ₯Ό λ³€κ²½ν•˜λŠ” set(int w, int h) ν•¨μˆ˜ 멀버 ...

March 3, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.3 Exercise 8 Solution

Problem: int νƒ€μž…μ˜ μ •μˆ˜λ₯Ό κ°μ²΄ν™”ν•œ Integer 클래슀λ₯Ό μž‘μ„±ν•˜λΌ. Integer의 λͺ¨λ“  멀버 ν•¨μˆ˜λ₯Ό μžλ™ 인라인으둜 μž‘μ„±ν•˜λΌ. Integer 클래슀λ₯Ό ν™œμš©ν•˜λŠ” μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include #include using namespace std; int main() { Integer n(30); cout << n.get() << ' '; // 30 좜λ ₯ n.set(50); cout << n.get() << ' '; // 50 좜λ ₯ Integer m("300"); cout << m.get() << ' '; // 300 좜λ ₯ cout << m.isEven(); // true(μ •μˆ˜λ‘œ 1) 좜λ ₯ } Objective & Hints: ...

March 3, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.3 Exercise 7 Solution

Problem: 문제 5λ²ˆμ„ μ°Έκ³ ν•˜μ—¬ μƒμ„±μžλ₯Ό μ΄μš©ν•˜μ—¬ 짝수 ν™€μˆ˜λ₯Ό 선택할 수 μžˆλ„λ‘ SelectableRandom 클래슀λ₯Ό μž‘μ„±ν•˜κ³  짝수 10개, ν™€μˆ˜ 10개λ₯Ό λžœλ€ν•˜κ²Œ λ°œμƒμ‹œν‚€λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. Objective & Hints: 클래슀 μž‘μ„±μ—μ„œ 맀개 λ³€μˆ˜λ₯Ό κ°€μ§„ μƒμ„±μžμ˜ ν™œμš© 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 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 #include #include #include using namespace std; class SelectableRandom{ int num; public: SelectableRandom(int _num); // λ§€κ°œλ³€μˆ˜λ‘œ 짝수 ν˜Ήμ€ ν™€μˆ˜λ₯Ό λ°›λŠ”λ‹€ int next(); int nextInRange(int a, int b); }; SelectableRandom::SelectableRandom(int _num){ num = _num; srand((unsigned)time(0)); } int SelectableRandom::next() { int ran = rand(); if(num%2 == 0){ // 짝수 return if(ran%2 == 0) return ran; else return --ran; } else { if(ran%2 == 0) // ν™€μˆ˜ return return ++ran; else return ran; } } int SelectableRandom::nextInRange(int a, int b){ int ran = rand()%(b-a+1) + a; if(num%2 == 0) { // 짝수 return if(ran%2 == 0) return ran; else return ++ran; } else { // ν™€μˆ˜ return if(ran%2 == 0) return ++ran; else return ran; } } int main() { SelectableRandom r1(0); // 짝수 랜덀 μ •μˆ˜ λ°œμƒ cout << "-- 0μ—μ„œ " << RAND_MAX << "κΉŒμ§€μ˜ 짝수 랜덀 μ •μˆ˜ 10 개--" << endl; for(int i=0; i<10; i++) { int n = r1.next(); cout << n << ' '; } SelectableRandom r2(1); // ν™€μˆ˜ 랜덀 μ •μˆ˜ λ°œμƒ cout << endl << endl << "-- 2μ—μ„œ " << "9 κΉŒμ§€μ˜ 랜덀 ν™€μˆ˜ μ •μˆ˜ 10 개 --" << endl; for(int i=0; i<10; i++) { int n = r2.nextInRange(2,9); cout << n << ' '; } cout << endl; } Explanation: ...

March 3, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.3 Exercise 6 Solution

Problem: 문제 5λ²ˆμ„ μ°Έκ³ ν•˜μ—¬ 짝수 μ •μˆ˜λ§Œ λžœλ€ν•˜κ²Œ λ°œμƒμ‹œν‚€λŠ” EvenRandom 클래슀λ₯Ό μž‘μ„±ν•˜κ³  EvenRandom 클래슀λ₯Ό μ΄μš©ν•˜μ—¬ 10개의 짝수λ₯Ό λžœλ€ν•˜κ²Œ 좜λ ₯ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. 0도 짝수둜 μ²˜λ¦¬ν•œλ‹€. Objective & Hints: 클래슀 μž‘μ„± 및 클래슀 ν™œμš© 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include #include using namespace std; class EvenRandom{ public: EvenRandom(); int next(); int nextInRange(int a, int b); }; EvenRandom::EvenRandom(){ srand((unsigned)time(0)); } int EvenRandom::next() { int ran = rand(); if(ran%2 == 0) return ran; else return --ran; } int EvenRandom::nextInRange(int a, int b){ int ran = rand()%(b-a+1) + a; if(ran%2 == 0) return ran; else return --ran; } int main(){ EvenRandom r; cout << "-- 0μ—μ„œ " << RAND_MAX << "κΉŒμ§€μ˜ 랜덀 μ •μˆ˜ 10 개--" << endl; for(int i=0; i<10; i++) { int n = r.next(); // 0μ—μ„œ RAND_MAX(32767) μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜ cout << n << ' '; } cout << endl << endl << "-- 2μ—μ„œ " << "10 κΉŒμ§€μ˜ 랜덀 μ •μˆ˜ 10 개 --" << endl; for(int i=0; i<10; i++) { int n = r.nextInRange(2,10); // 2μ—μ„œ 10 μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜ cout << n << ' '; } cout << endl; } Explanation: ...

March 3, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.3 Exercise 5 Solution

Problem: 랜덀 수λ₯Ό λ°œμƒμ‹œν‚€λŠ” Random 클래슀λ₯Ό λ§Œλ“€μž. Random 클래슀λ₯Ό μ΄μš©ν•˜μ—¬ 랜덀 ν•œ μ •μˆ˜λ₯Ό 10개 좜λ ₯ν•˜λŠ” μ‚¬λ‘€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. Random ν΄λž˜μŠ€κ°€ μƒμ„±μž, next(), nextInRange()의 3개의 멀버 ν•¨μˆ˜λ₯Ό 가지도둝 μž‘μ„±ν•˜κ³  main() ν•¨μˆ˜μ™€ 합쳐 ν•˜λ‚˜μ˜ cpp νŒŒμΌμ— κ΅¬ν˜„ν•˜λΌ. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main() { Random r; cout << "-- 0μ—μ„œ " << RAND_MAX << "κΉŒμ§€μ˜ 랜덀 μ •μˆ˜ 10 개--" << endl; for(int i=0; i<10; i++) { int n = r.next(); // 0μ—μ„œ RAND_MAX(32767) μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜ cout << n << ' '; } cout << endl << endl << "-- 2μ—μ„œ " << "4 κΉŒμ§€μ˜ 랜덀 μ •μˆ˜ 10 개 --" << endl; for(int i=0; i<10; i++) { int n = r.nextInRange(2,4); // 2μ—μ„œ 4 μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜ cout << n << ' '; } cout << endl; } Objective & Hints: ...

March 3, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.3 Exercise 4 Solution

Problem: CoffeeMachine 클래슀λ₯Ό λ§Œλ“€μ–΄λ³΄μž. main() ν•¨μˆ˜μ™€ Execution Resultκ°€ λ‹€μŒκ³Ό 같도둝 CoffeeMachine 클래슀λ₯Ό μž‘μ„±ν•˜λΌ. μ—μŠ€ν”„λ ˆμ†Œ ν•œ μž”μ—λŠ” 컀피와 물이 각각 1μ”© μ†ŒλΉ„λ˜κ³ , μ•„λ©”λ¦¬μΉ΄λ…Έμ˜ 경우 μ»€ν”ΌλŠ” 1, 물은 2κ°€ μ†ŒλΉ„λ˜κ³ , 섀탕 μ»€ν”ΌλŠ” 컀피 1, λ¬Ό 2, 섀탕 1이 μ†ŒλΉ„λœλ‹€. CoffeeMachine ν΄λž˜μŠ€μ—λŠ” μ–΄λ–€ 멀버 λ³€μˆ˜μ™€ μ–΄λ–€ 멀버 ν•¨μˆ˜κ°€ ν•„μš”ν•œμ§€ 잘 νŒλ‹¨ν•˜μ—¬ μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include using namespace std; int main() { CoffeeMachine java(5, 10, 3); // μ»€ν”ΌλŸ‰:5, λ¬ΌλŸ‰:10, 섀탕:6 으둜 μ΄ˆκΈ°ν™” java.drinkEspresso(); // 컀피 1, λ¬Ό 1 μ†ŒλΉ„ java.show(); // ν˜„μž¬ 컀피 λ¨Έμ‹ μ˜ μƒνƒœ 좜λ ₯ java.drinkAmericano(); // 컀피 1, λ¬Ό 2 μ†ŒλΉ„ java.show(); // ν˜„μž¬ 컀피 λ¨Έμ‹ μ˜ μƒνƒœ 좜λ ₯ java.drinkSugarCoffee(); // 컀피 1, λ¬Ό 2, 섀탕 1 μ†ŒλΉ„ java.show(); // ν˜„μž¬ 컀피 λ¨Έμ‹ μ˜ μƒνƒœ 좜λ ₯ java.fill(); // 컀피 10, λ¬Ό 10, 섀탕 10 으둜 μ±„μš°κΈ° java.show(); // ν˜„μž¬ 컀피 λ¨Έμ‹ μ˜ μƒνƒœ 좜λ ₯ Execution Result: ...

March 3, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.3 Exercise 3 Solution

Problem: μ€ν–‰μ—μ„œ μ‚¬μš©ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜κΈ° μœ„ν•΄, 은행 κ³„μ’Œ ν•˜λ‚˜λ₯Ό ν‘œν˜„ν•˜λŠ” 클래슀 Accountκ°€ ν•„μš”ν•˜λ‹€. κ³„μ’Œ μ •λ³΄λŠ” κ³„μ’Œμ˜ 주인, κ³„μ’Œ 번호, μž”μ•‘μ„ λ‚˜νƒ€λ‚΄λŠ” 3 멀버 λ³€μˆ˜λ‘œ 이루어진닀. main() ν•¨μˆ˜μ˜ μ‹€ν–‰κ³Ό κ²°κ³Όκ°€ λ‹€μŒκ³Ό 같도둝 Account 클래슀λ₯Ό μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 10 #include using namespace std; int main() { Account a("Kitae", 1, 5000); // id 1번, μž”μ•‘ 5000원, 이름이 Kitae인 κ³„μ’Œ μ„± a.deposit(50000); // 50000원 μ €κΈˆ cout << a.getOwner() << "의 μž”μ•‘μ€ " << a.inquiry() << endl; int money = a.withdraw(20000); // 20000원 좜금. withdraw()λŠ” μΆœκΈˆν•œ μ‹€μ œ κΈˆμ•‘ 리턴 cout << a.getOwner() << "의 μž”μ•‘μ€ " << a.inquiry() << endl; } Execution Result: ...

March 2, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.3 Exercise 2 Solution

Problem: λ‚ μ§œλ₯Ό λ‹€λ£¨λŠ” Date 클래슀λ₯Ό μž‘μ„±ν•˜κ³ μž ν•œλ‹€. Dateλ₯Ό μ΄μš©ν•˜λŠ” main()κ³Ό Execution ResultλŠ” λ‹€μŒκ³Ό κ°™λ‹€. 클래슀 Dateλ₯Ό μž‘μ„±ν•˜μ—¬ μ•„λž˜ ν”„λ‘œκ·Έλž¨μ— μΆ”κ°€ν•˜λΌ. 1 2 3 4 5 6 7 8 9 #include using namespace std; int main() { Date virth(2014, 3, 20); Date independenceDay("1945/8/15"); independenceDay.show(); cout << birth.getYear() << ',' << birth.getMonth() << ',' << birth.getDay() << endl; } Execution Result: Objective & Hints: 2개의 μƒμ„±μžμ™€ μ—¬λŸ¬ 멀버λ₯Ό κ°€μ§„ 클래슀 λ§Œλ“€κΈ° ...

March 2, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.3 Exercise 1 Solution

Problem: μ•„λž˜ main()의 Execution Resultκ°€ λ‹€μŒκ³Ό 같도둝 Tower 클래슀λ₯Ό μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 #include using namespace std; int main() { Tower myTower; // 1 λ―Έν„° Tower seoulTower(100); // 100 λ―Έν„° cout << "λ†’μ΄λŠ” " << myTower.getHeight() << "λ―Έν„°" << endl; cout << "λ†’μ΄λŠ” " << seoulTower.getHeight() << "λ―Έν„°" << endl; } Objective & Hints: 2개의 μƒμ„±μžμ™€ μ—¬λŸ¬ 멀버λ₯Ό κ°€μ§„ 클래슀 λ§Œλ“€κΈ° ...

March 2, 2020 Β· 1 min Β· Sobamemil