C++ Programming Ch.8 Exercise 7 Solution

Problem: μ•„λž˜μ™€ 같은 BaseMemory 클래슀λ₯Ό μƒμ†λ°›λŠ” ROM(Read Only Memory), RAM 클래슀λ₯Ό μž‘μ„±ν•˜λΌ. BaseMemory에 ν•„μš”ν•œ μ½”λ“œλ₯Ό μˆ˜μ • μΆ”κ°€ν•˜μ—¬ 적절히 μ™„μ„±ν•˜λΌ. 1 2 3 4 5 class BaseMemory{ char *mem; protected: BaseMemory(int size) { mem = new char [size]; } }; ROM은 읽기 μ „μš© λ©”λͺ¨λ¦¬μ΄λ―€λ‘œ μž‘λ™ 쀑에 값을 μ“Έ μˆ˜κ°€ μ—†κΈ° λ•Œλ¬Έμ—, 곡μž₯μ—μ„œ 생산할 λ•Œ μƒμ‚°μžκ°€ μš”μ²­ν•œ λ°μ΄ν„°λ‘œ μ΄ˆκΈ°ν™” ν•˜λŠ”λ° 이 μž‘μ—…μ„ κ΅½λŠ”λ‹€(burn)라고 ν•œλ‹€. κ·ΈλŸ¬λ―€λ‘œ ROM은 λ°˜λ“œμ‹œ μƒμ„±μžμ—μ„œ burn μž‘μ—…μ΄ μΌμ–΄λ‚˜μ•Ό ν•œλ‹€. ...

March 9, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.8 Exercise 6 Solution

Problem: 문제 5~6에 μ μš©λ˜λŠ” BaseArray ν΄λž˜μŠ€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 class BaseArray { int capacity; // λ°°μ—΄μ˜ 크기 int *mem; // μ •μˆ˜ 배열을 λ§Œλ“€κΈ° μœ„ν•œ λ©”λͺ¨λ¦¬μ˜ 포인터 protected: // μƒμ„±μžκ°€ protected BaseArray(int capacity=100){ this->capacity = capacity; mem = new int [capacity]; } ~BaseArray() { delete [] mem; } void put(int index, int val) { mem[index] = val; } int get(int index) { return mem[index]; } int getCapacity() { return capacity; } }; Write a μŠ€νƒμœΌλ‘œ μž‘λ™ν•˜λŠ” MyStack class that inherits from the BaseArray class. ...

March 9, 2020 Β· 3 min Β· Sobamemil

C++ Programming Ch.8 Exercise 5 Solution

Problem: 문제 5~6에 μ μš©λ˜λŠ” BaseArray ν΄λž˜μŠ€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 class BaseArray { int capacity; // λ°°μ—΄μ˜ 크기 int *mem; // μ •μˆ˜ 배열을 λ§Œλ“€κΈ° μœ„ν•œ λ©”λͺ¨λ¦¬μ˜ 포인터 protected: // μƒμ„±μžκ°€ protected BaseArray(int capacity=100){ this->capacity = capacity; mem = new int [capacity]; } ~BaseArray() { delete [] mem; } void put(int index, int val) { mem[index] = val; } int get(int index) { return mem[index]; } int getCapacity() { return capacity; } }; BaseArrayλ₯Ό 상속받아 큐처럼 μž‘λ™ν•˜λŠ” MyQueue 클래슀λ₯Ό μž‘μ„±ν•˜λΌ. ...

March 9, 2020 Β· 3 min Β· Sobamemil

C++ Programming Ch.8 Exercise 4 Solution

Problem: 문제 3~4에 μ μš©λ˜λŠ” 2차원 μƒμ˜ ν•œ 점을 ν‘œν˜„ν•˜λŠ” Point ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 9 class Point { int x,y; public: point(int x, int y) { this->x = x; this->y = y; } int getX(){ return x; } int getY(){ return y; } protected: void move(int x, int y) { this->x = x; this->y = y; } }; λ‹€μŒ main() ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜λ„λ‘ Point 클래슀λ₯Ό μƒμ†λ°›λŠ” ColorPoint 클래슀λ₯Ό μž‘μ„±ν•˜κ³ , 전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. ...

March 9, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.8 Exercise 3 Solution

Problem: 문제 3~4에 μ μš©λ˜λŠ” 2차원 μƒμ˜ ν•œ 점을 ν‘œν˜„ν•˜λŠ” Point ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 9 class Point { int x,y; public: point(int x, int y) { this->x = x; this->y = y; } int getX(){ return x; } int getY(){ return y; } protected: void move(int x, int y) { this->x = x; this->y = y; } }; λ‹€μŒ main() ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜λ„λ‘ Point 클래슀λ₯Ό 상속받은 ColorPoint 클래슀λ₯Ό μž‘μ„±ν•˜κ³ , 전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. ...

March 9, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.8 Exercise 2 Solution

Problem: 문제 1~2에 μ μš©λ˜λŠ” 원을 μΆ”μƒν™”ν•œ Circle ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 class Circle{ int radius; public: Circle(int radius=0) { this->radius = radius; } int getRadius() { return radius; } void setRadius(int radius) { this->radius = radius; } double getArea() { return 3.14*radius*radius; } }; λ‹€μŒκ³Ό 같이 배열을 μ„ μ–Έν•˜μ—¬ λ‹€μŒ Execution Resultκ°€ λ‚˜μ˜€λ„λ‘ Circle을 상속받은 NamedCircle ν΄λž˜μŠ€μ™€ main() ν•¨μˆ˜ λ“± ν•„μš”ν•œ ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜λΌ. ...

March 9, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.8 Exercise 1 Solution

Problem: 문제 1~2에 μ μš©λ˜λŠ” 원을 μΆ”μƒν™”ν•œ Circle ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 class Circle{ int radius; public: Circle(int radius=0) { this->radius = radius; } int getRadius() { return radius; } void setRadius(int radius) { this->radius = radius; } double getArea() { return 3.14*radius*radius; } }; λ‹€μŒ μ½”λ“œκ°€ μ‹€ν–‰λ˜λ„λ‘ Circle을 상속받은 NamedCircle 클래슀λ₯Ό μž‘μ„±ν•˜κ³  전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. 1 2 NamedCircle waffle(3, "waffle"); // λ°˜μ§€λ¦„μ΄ 3이고 이름이 waffle인 원 waffle.show(); Execution Result: ...

March 9, 2020 Β· 2 min Β· Sobamemil

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

C++ Programming Ch.7 Exercise 11 Solution

Problem: μŠ€νƒ 클래슀 Stack을 λ§Œλ“€κ³  ν‘Έμ‹œ(push)용으둜 << μ—°μ‚°μžλ₯Ό, 팝(pop)을 μœ„ν•΄ >> μ—°μ‚°μžλ₯Ό, λΉ„μ–΄ μžˆλŠ” μŠ€νƒμΈμ§€λ₯Ό μ•ŒκΈ° μœ„ν•΄ ! μ—°μ‚°μžλ₯Ό μž‘μ„±ν•˜λΌ. λ‹€μŒ μ½”λ“œλ₯Ό main()으둜 μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 Stack stack; stack << 3 << 5 << 10; // 3,5,10 μˆœμ„œλŒ€λ‘œ push while(true){ if(!stack) break; //stack empty int x; stack >> x; //stack의 top에 μžˆλŠ” μ •μˆ˜ pop cout << x << ' '; } cout << endl; Execution Result: ...

March 6, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.7 Exercise 10 Solution

Problem: 톡계λ₯Ό λ‚΄λŠ” Statistics 클래슀λ₯Ό λ§Œλ“€λ €κ³  ν•œλ‹€. λ°μ΄ν„°λŠ” Statistics 클래슀 내뢀에 int 배열을 λ™μ μœΌλ‘œ ν• λ‹Ήλ°›μ•„ μœ μ§€ν•œλ‹€. λ‹€μŒκ³Ό 같은 연산이 잘 이루어지도둝 Statistics ν΄λž˜μŠ€μ™€ !, >>, <<, ~ μ—°μ‚°μž ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int main() { Statistics stat; if(!stat) cout << "ν˜„μž¬ 톡계 데이타가 μ—†μŠ΅λ‹ˆλ‹€." << endl; int x[5]; cout << "5 개의 μ •μˆ˜λ₯Ό μž…λ ₯ν•˜λΌ>>"; for(int i=0; i<5; i++) cin >> x[i]; // x[i]에 μ •μˆ˜ μž…λ ₯ for(int i=0; i<5; i++) stat << x[i]; // x[i] 값을 톡계 객체에 μ‚½μž…ν•œλ‹€. stat << 100 << 200; // 100, 200을 톡계 객체에 μ‚½μž…ν•œλ‹€. ~stat; // 톡계 데이터λ₯Ό λͺ¨λ‘ 좜λ ₯ν•œλ‹€. int avg; stat >> avg; // 톡계 κ°μ²΄λ‘œλΆ€ν„° 평균을 λ°›λŠ”λ‹€. cout << "avg=" << avg << endl; // 평균을 좜λ ₯ν•œλ‹€. } Execution Result: ...

March 6, 2020 Β· 2 min Β· Sobamemil