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

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

C++ Programming Ch.4 Exercise 2 Solution

Problem: 정수 공간 5개를 배열로 동적 할당받고, 정수를 5개 입력받아 평균을 구하고 출력한 뒤 배열을 소멸시키도록 main() 함수를 작성하라. Objective & Hints: 배열의 동적 할당 및 반환 Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include using namespace std; int main() { int *p = new int[5]; double sum = 0; cout << "정수 5개 입력>>"; for(int i=0; i<5; i++) { cin >> p[i]; sum += p[i]; } cout << "평균 " << sum/5; delete [] p; } Explanation: ...

March 4, 2020 · 1 min · Sobamemil

C++ Programming Ch.4 Exercise 1 Solution

Problem: 다음은 색의 3요소인 red, green, blue로 색을 추상화한 Color 클래스를 선언하고 활용하는 코드이다. 빈칸을 채워라. red, green, blue는 0~255의 값만 가진다. 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 #include using namespace std; class Color { int red, green, blue; public: Color() {red = green = blue = 0;} Color(int r, int g, int b) {red = r; green = g; blue = b;} void setColor(int r, int g, int b) {red = r; green = g; blue = b;} void show() {cout << red << ' ' << green << ' ' << blue << endl;} }; int main() { Color screenColor(255,0,0); // 빨간색의 screenColor 객체 생성 Color *p; // Color 타입의 포인터 변수 p 선언 _____ // (1) p가 screenColor의 주소를 가지도록 코드 작성 _____ // (2) p와 show()를 이용하여 screenColor 색 출력 _____ // (3) Color의 일차원 배열 colors 선언. 원소는 3개 _____ // (4) p가 colors 배열을 가리키도록 코드 작성 // (5) p와 setColor()를 이용하여 colors[0], colors[1], colors[2]가 // 각각 빨강, 초록, 파랑색을 가지도록 코드 작성 _____ _____ _____ // (6) p와 show()를 이용하여 colors 배열의 모든 객체의 색 출력. for 문 이용 _____ _____ _____ } Objective & Hints: ...

March 4, 2020 · 3 min · Sobamemil

C++ Programming Ch.3 Exercise 12 Solution

Problem: 컴퓨터의 주기억장치를 모델링하는 클래스의 Ram을 구현하려고 한다. Ram 클래스는 데이터가 기록될 메모리 공간과 크기 정보를 가지고, 주어진 주소에 데이터를 기록하고(write), 주어진 주소로부터 데이터를 읽어 온다(read). Ram 클래스는 다음과 같이 선언된다. 1 2 3 4 5 6 7 8 9 class Ram { char mem[100 * 1024]; // 100KB 메모리. 한 번지는 한 바이트이므로 char 타입 사용 int size; public: Ram(); // mem 배열을 0으로 초기화하고 size를 100*1024로 초기화 ~Ram(); // "메모리 제거됨" 문자열 출력 char read(int address); // address 주소의 메모리 바이트 리턴 void write(int address, char value); // address 주소에 한 바이트로 value 저장 }; 다음 main() 함수는 100 번지에 20을 저장하고, 101 번지에 30을 저장한 후, 100 번지와 101 번지의 값을 읽고 더하여 102 번지에 저장하는 코드이다. ...

March 3, 2020 · 3 min · Sobamemil

C++ Programming Ch.3 Exercise 11 Solution

Problem: 다음 코드에서 Box 클래스의 선언부와 구현부를 Box.h, Box.cpp 파일로 분리하고 main() 함수 부분을 main.cpp로 분리하여 전체 프로그램을 완성하라. 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 #include using namespace std; class Box { int width, height; char fill; public: Box(int w, int h) { setSize(w, h); fill = '*';} void setFill(char f) {fill = f;} void setSize(int w, int h) { width = w; height = h;} void draw(); }; void Box::draw() { for (int n = 0; n < height; n++) { for (int m = 0; m < width; m++) cout << fill; cout << endl; } } int main() { Box b(10, 2); b.draw(); // 박스를 그린다. cout << endl; b.setSize(7, 4); // 박스의 크기를 변경한다. b.setFill('^'); // 박스의 내부를 채울 문자를 '^'로 변경한다. b.draw(); // 박스를 그린다. } Objective & Hints: ...

March 3, 2020 · 2 min · Sobamemil

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개를 랜덤하게 발생시키는 프로그램을 작성하라. 2020/03/03 - [C++/명품 C++ programming] - 명품 C++ programming Exercise Problem 3장 5번 [명품 C++ programming Exercise Problem 3장 5번 Problem: 랜덤 수를 발생시키는 Random 클래스를 만들자. Random 클래스를 이용하여 랜덤 한 정수를 10개 출력하는 사례는 다음과 같다. Random 클래스가 생성자, next(), nextInRange()의 3개의 멤버 함수를 가지도.. sobamemil.tistory.com](https://sobamemil.tistory.com/47) ...

March 3, 2020 · 2 min · Sobamemil