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

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

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

C++ Programming Ch.7 Exercise 1 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

C++ Programming Ch.6 Exercise 9 Solution

Problem: κ²Œμ‹œνŒ ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•΄λ³΄μž. λ©€ν‹°νƒœμŠ€ν‚Ήμ˜ 경우 μ—¬λŸ¬ μ‚¬μš©μžλ“€μ΄ κ²Œμ‹œνŒμ— 글을 올리기 λ•Œλ¬Έμ— κ²Œμ‹œνŒ κ°μ²΄λŠ” 전체 ν•˜λ‚˜λ§Œ μžˆμ–΄μ•Ό ν•œλ‹€. κ·ΈλŸ¬λ―€λ‘œ κ²Œμ‹œνŒ 객체의 멀버듀은 static으둜 μž‘μ„±ν•œλ‹€. λ‹€μŒμ€ κ²Œμ‹œνŒ κΈ°λŠ₯을 ν•˜λŠ” Board 클래슀λ₯Ό ν™œμš©ν•˜λŠ” main() μ½”λ“œμ΄λ‹€. Execution Resultλ₯Ό μ°Έκ³ ν•˜μ—¬ Board 클래슀λ₯Ό λ§Œλ“€κ³  전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. static μ—°μŠ΅μ΄ λͺ©μ μ΄κΈ° λ•Œλ¬Έμ— κ²Œμ‹œνŒ κΈ°λŠ₯을 글을 μ˜¬λ¦¬λŠ” κΈ°λŠ₯κ³Ό κ²Œμ‹œκΈ€μ„ λͺ¨λ‘ 좜λ ₯ν•˜λŠ” κΈ°λŠ₯으둜 μ œν•œν•˜κ³  main()도 λ‹¨μˆœν™”ν•˜μ˜€λ‹€. 1 2 3 4 5 6 7 8 int main() { // Board myBoard; // 객체 생성은 컴파일 였λ₯˜μž…λ‹ˆλ‹€. Board::add("μ€‘κ°„κ³ μ‚¬λŠ” 감독 μ—†λŠ” 자율 μ‹œν—˜μž…λ‹ˆλ‹€."); Board::add("μ½”λ”© λΌμš΄μ§€ 많이 μ΄μš©ν•΄ μ£Όμ„Έμš”."); Board::print(); Board::add("μ§„μ†Œλ¦° 학생이 κ²½μ§„λŒ€νšŒ μž…μƒν•˜μ˜€μŠ΅λ‹ˆλ‹€. μΆ•ν•˜ν•΄μ£Όμ„Έμš”"); Board::print(); } Execution Result: ...

March 6, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.6 Exercise 8 Solution

Problem: 디버깅에 ν•„μš”ν•œ 정보λ₯Ό μ €μž₯ν•˜λŠ” Trace 클래슀λ₯Ό λ§Œλ“€μ–΄λ³΄μž. μ €μžμ˜ κ²½ν—˜μ— μ˜ν•˜λ©΄, λ©€ν‹°νƒœμŠ€ν¬ ν”„λ‘œκ·Έλž¨μ„ κ°œλ°œν•˜κ±°λ‚˜ νŠΉλ³„ν•œ ν™˜κ²½μ—μ„œ μž‘μ—…ν•  λ•Œ, Visual Studio의 디버거와 같은 μ†ŒμŠ€ 레벨 디버거λ₯Ό μ‚¬μš©ν•˜μ§€ λͺ»ν•˜λŠ” κ²½μš°κ°€ λ”λŸ¬ μžˆμ—ˆκ³ , μ΄λ•Œ μ‹€ν–‰ 도쀑 정보λ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•΄ Trace 클래슀λ₯Ό λ§Œλ“€μ–΄ μ‚¬μš©ν•˜μ˜€λ‹€. Trace 클래슀λ₯Ό ν™œμš©ν•˜λŠ” λ‹€μŒ ν”„λ‘œκ·Έλž¨κ³Ό κ²°κ³Όλ₯Ό μ°Έκ³ ν•˜μ—¬ Trace 클래슀λ₯Ό μž‘μ„±ν•˜κ³  전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. 디버깅 μ •λ³΄λŠ” 100개둜 μ œν•œν•œλ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 void f() { int a, b, c; cout << "두 개의 μ •μˆ˜λ₯Ό μž…λ ₯ν•˜μ„Έμš”>>"; cin >> a >> b; Trace::put("f()", "μ •μˆ˜λ₯Ό μž…λ ₯ λ°›μ•˜μŒ"); // 디버깅 정보 μ €μž₯ c = a + b; Trace::put("f()", "ν•© 계산"); // 디버깅 정보 μ €μž₯ cout << "합은 " << c << endl; } int main() { Trace::put("main()", "ν”„λ‘œκ·Έλž¨μ„ μ‹œμž‘ν•©λ‹ˆλ‹€"); // 디버깅 정보 μ €μž₯ f(); Trace::put("main()", "μ’…λ£Œ"); // put()의 첫 번째 맀개 λ³€μˆ˜λŠ” νƒœκ·Έμ΄κ³  // 두 번째 맀개 λ³€μˆ˜λŠ” 디버깅 정보이닀. Trace::print("f()"); // "f()" νƒœκ·Έλ₯Ό κ°€μ§„ 디버깅 정보λ₯Ό λͺ¨λ‘ 좜λ ₯ν•œλ‹€. Trace::print(); // λͺ¨λ“  디버깅 정보λ₯Ό 좜λ ₯ν•œλ‹€. } Execution Result: ...

March 6, 2020 Β· 3 min Β· Sobamemil

C++ Programming Ch.6 Exercise 7 Solution

Problem: λ‹€μŒκ³Ό 같은 static 멀버λ₯Ό κ°€μ§„ Random 클래슀λ₯Ό μ™„μ„±ν•˜λΌ(Open Challenge 힌트 μ°Έκ³ ). 그리고 Random 클래슀λ₯Ό μ΄μš©ν•˜μ—¬ λ‹€μŒκ³Ό 같이 λžœλ€ν•œ 값을 좜λ ₯ν•˜λŠ” main() ν•¨μˆ˜λ„ μž‘μ„±ν•˜λΌ. main()μ—μ„œ Random 클래슀의 seed() ν•¨μˆ˜λ₯Ό ν™œμš©ν•˜λΌ. Execution Result: Objective & Hints: static 멀버λ₯Ό κ°€μ§„ 클래슀 λ§Œλ“€κΈ° 및 μ‚¬μš© μ—°μŠ΅ μ •μˆ˜λ₯Ό λžœλ€ν•˜κ²Œ μƒμ„±ν•˜λŠ” μ½”λ“œλŠ” λ‹€μŒμ„ μ΄μš©ν•˜λ©΄ λ©λ‹ˆλ‹€. 1 2 3 srand((unsigned)time(0)); // 항상 λ‹€λ₯Έ 랜덀수λ₯Ό λ°©μƒμ‹œν‚€κΈ° μœ„ν•œ seed μ„€μ • int n = rand(); // 0μ—μ„œ RAND_MAX(32767) μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜κ°€ n에 λ°œμƒ n = n % 100; // n은 0μ—μ„œ 99 μ‚¬μ΄μ˜ μ •μˆ˜ Code: ...

March 6, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.6 Exercise 6 Solution

Problem: λ™μΌν•œ 크기의 배열을 λ³€ν™˜ν•˜λŠ” λ‹€μŒ 2개의 static 멀버 ν•¨μˆ˜λ₯Ό κ°€μ§„ ArrayUtiliry2 클래슀λ₯Ό λ§Œλ“€κ³ , 이 클래슀λ₯Ό μ΄μš©ν•˜μ—¬ μ•„λž˜ 결과와 같이 좜λ ₯λ˜λ„λ‘ ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. 1 2 3 4 5 6 // s1κ³Ό s2λ₯Ό μ—°κ²°ν•œ μƒˆλ‘œμš΄ 배열을 동적 μƒμ„±ν•˜κ³  포인터 리턴 static int* concat(int s1[], int s2[], int size); // s1μ—μ„œ s2에 μžˆλŠ” 숫자λ₯Ό λͺ¨λ‘ μ‚­μ œν•œ μƒˆλ‘œμš΄ 배열을 동적 μƒμ„±ν•˜μ—¬ 리턴 // λ¦¬ν„΄ν•˜λŠ” λ°°μ—΄μ˜ ν¬κΈ°λŠ” retSize에 전달. retSizeκ°€ 0인 경우 NULL 리턴 static int* remove(int s1[], int s2[], int size, int& retSize); Execution Result: ...

March 6, 2020 Β· 3 min Β· Sobamemil

C++ Programming Ch.6 Exercise 5 Solution

Problem: λ™μΌν•œ 크기둜 배열을 λ³€ν™˜ν•˜λŠ” λ‹€μŒ 2개의 static 멀버 ν•¨μˆ˜λ₯Ό κ°€μ§„ ArrayUtility 클래슀λ₯Ό λ§Œλ“€μ–΄λΌ. 1 2 3 4 static void intToDouble(int source[], double dest[], int size); // int[]을 double[]둜 λ³€ν™˜ static void doubleToInt(double source[], int dest[], int size); // double[]을 int[]둜 λ³€ν™˜ ArrayUtilityλ₯Ό ν™œμš©ν•˜λŠ” main()은 λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 13 int main() { int x[] = {1,2,3,4,5}; double y[5]; double z[] = {9.9,8.8,7.7,6.6,5.6}; ArrayUtility::intToDouble(x, y, 5); // x[] -> y[] for(int i=0; i<5; i++) cout << y[i] << ' '; cout << endl; ArrayUtility::doubleToInt(z, x, 5); // z[] -> x[] for(int i=0; i<5; i++) cout << x[i] << ' '; cout << endl; } Execution Result: ...

March 6, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.6 Exercise 4 Solution

Problem: λ‹€μŒ ν΄λž˜μŠ€μ— μ€‘λ³΅λœ μƒμ„±μžλ₯Ό λ””ν΄νŠΈ 맀개 λ³€μˆ˜λ₯Ό κ°€μ§„ ν•˜λ‚˜μ˜ μƒμ„±μžλ‘œ μž‘μ„±ν•˜κ³  ν…ŒμŠ€νŠΈ ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class MyVector{ int *mem; int size; public: MyVector(); MyVector(int n, int val); ~MyVector() { delete [] mem; } }; MyVector::MyVector() { mem = new int [100]; size = 100; for(int i=0; i<size; i++) mem[i] = 0; } MyVector::MyVector(int n, int val) { mem = new int [n]; size = n; for(int i=0; i<size; i++) mem[i] = val; } Execution Result: ...

March 6, 2020 Β· 2 min Β· Sobamemil