프로그래밍 공부
명품 C++ Programming Chapter 05 실습문제 본문
답지가 없는 문제들만 블로그에 올립니다
1번 문제
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
|
#include <iostream>
#include <string>
using namespace std;
// 1번 문제
class Circle {
int a;
public:
Circle() {
this->a = 0;
}
Circle(int n) {
this->a = n;
}
void print() {
cout << this->a << ' ';
}
};
void swap(Circle& a, Circle& b) {
Circle tmp;
tmp = a;
a = b;
b = tmp;
}
int main() {
Circle cir_1(2),cir_2(5);
cir_1.print(); cir_2.print();
cout << "\nSwap" << endl;
swap(cir_1, cir_2);
cir_1.print(); cir_2.print();
}
|
cs |
출력결과
3번 문제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
#include <string>
using namespace std;
// 3번 문제
void combine(string str1, string str2, string& str3) {
str3 = str1 + " "; str3 += str2;
}
int main() {
string text1("I love you"), text2("very much");
string text3("");
combine(text1, text2, text3);
cout << text3 << endl;
return 0;
}
|
cs |
출력결과
5번 문제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <iostream>
#include <string>
using namespace std;
// 5번 문제
class Circle {
int radius;
public:
Circle(int r) { radius = r; }
int getRadius() { return radius; }
int setRadius(int r) { return radius = r; }
void show() { cout << "반지름이 " << radius << "인 원" << endl; }
};
void increaseBy(Circle& a, Circle& b) {
int r = a.getRadius() + b.getRadius();
a.setRadius(r);
}
int main() {
Circle x(10), y(5);
increaseBy(x, y);
x.show();
}
|
cs |
출력결과
6번 문제
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
|
#include <iostream>
#include <string>
using namespace std;
//6번
char& find(char a[], char c, bool& success) {
for (int i = 0; i < sizeof(a) / sizeof(char); i++) {
if (a[i] == c) {
success = true;
return a[i];
}
}
}
int main() {
char s[] = "Mike";
bool b = false;
char& loc = find(s, 'M', b);
if (b == false) {
cout << "M을 발견할 수 없다" << endl;
return 0;
}
loc = 'm';
cout << s << endl;
return 0;
}
|
cs |
출력결과
8번 문제
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 <iostream>
#include <string>
using namespace std;
// 8번 문제
class MyIntStack {
int *p;
int size;
int top;
public:
MyIntStack() {};
MyIntStack(int size) {
this->p = new int[size];
this->size = size;
this->top = 0;
}
MyIntStack(const MyIntStack& s) {
this->p = new int[s.size];
for (int i = 0; i < s.top; i++) {
this->p[i] = s.p[i];
cout << this->p[i] << ' ';
}
cout << endl;
this->size = s.size;
this->top = s.top;
}
~MyIntStack() {
delete[] p;
}
bool push(int n) {
if (size == top) {
return false;
}
else {
p[top++] = n;
return true;
}
}
bool pop(int &n) {
if (0 == top) {
return false;
}
else {
n = p[--top];
return true;
}
}
};
int main() {
MyIntStack a(10);
a.push(10);
a.push(20);
MyIntStack b = a;
b.push(30);
int n;
a.pop(n);
cout << "스택 a에서 팝한 값" << n << endl;
b.pop(n);
cout << "스택 b에서 팝한 값" << n << endl;
return 0;
}
|
cs |
출력결과
8번 문제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include <iostream>
#include <string>
using namespace std;
// 10번 문제
class Buffer {
string text;
public:
Buffer(string text) { this->text = text; }
void add(string next) { text += next; }
void print() { cout << text << endl; }
};
Buffer& append(Buffer& buf, string str) {
buf.add(str);
return buf;
}
int main() {
Buffer buf("Hello");
Buffer& temp = append(buf, "Guys");
temp.print();
buf.print();
}
|
cs |
출력결과
12번 문제
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
|
#include <iostream>
#include <string>
using namespace std;
// 12
class Dept {
int size; // scores 배열의 크기
int* scores; // 동적 할당 받을 정수 배열의 주소
public:
Dept(int size) {
this->size = size;
scores = new int[size];
}
Dept(const Dept& dept) {
this->size = dept.size;
this->scores = new int[dept.size];
for (int i = 0; i < dept.size; i++) {
this->scores[i] = dept.scores[i];
}
}
~Dept() {
delete[] scores;
}
int getSize() { return size; }
void read() {
cout << "10개 점수 입력>> ";
for (int i = 0; i < this->size; i++) {
cin >> this->scores[i];
}
}
bool isOver60(int index) {
if (this->scores[index] > 60) {
return true;
}
else {
return false;
}
}
};
// 10 20 30 40 50 60 70 80 90 100
int countPass(Dept dept) { // 12-3 정답 함수 인자 부분을 (Dept dept) -> (Dept& dept)
int count = 0;
for (int i = 0; i < dept.getSize(); i++) {
if (dept.isOver60(i)) count++;
}
return count;
}
int main() {
Dept com(10);
com.read();
int n = countPass(com); // 12-2 정답 매개변수로 넘겨줄때 복사를 해서 넘겨줌
cout << "60점 이상은 " << n << "명";
return 0;
}
|
cs |
출력결과
'Programming > 명품 C++ Programming' 카테고리의 다른 글
명품 C++ Programming Chapter 04 실습 문제 (0) | 2021.02.24 |
---|---|
명품 C++ Programming CHAPTER 03 OpenChallenge (0) | 2021.02.22 |
명품 C++ Programming Open Challenge 4장 한글 끝말잇기 게임 (1) | 2019.05.06 |
Comments