프로그래밍 공부
명품 C++ Programming Chapter 04 실습 문제 본문
정답을 공개 하지 않는 문제들만 블로그에 올렸습니다
2번 문제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
// 2번 문제
int main() {
int* arr = new int[5];
double solve = 0.0;
cout << "정수 5개 입력>>";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
solve += arr[i];
}
cout << "평균 " << solve / 5;
return 0;
}
|
cs |
출력결과
3번 문제
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
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
// 3번 문제
int main() {
//ex : Are you happy? I am so happy.
string str; int cnt = 0;
cout << "문자열 입력>> ";
getline(cin, str, '\n');
// 3-1번
// for (int i = 0; i < str.length(); i++) {
// if (str.at(i) == 'a') cnt++; // or if(str[i] == 'a') cnt++;
// }
// 3-2번
int index = 0;
while (1) {
if (str.find('a',index)!= string::npos) {
index = str.find('a', index) + 1;
cnt++;
}
else {
break;
}
}
cout << "문자 a는 " << cnt << "개 있습니다.";
return 0;
}
|
cs |
출력결과
4번 문제
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
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
// 4번 문제
class Sample {
int *p;
int size;
public:
Sample(int n) { // 생성자
size = n; p = new int[n];
}
void read(); // 동적 할당받은 정수 배열 p에 사용자로부터 정수를 입력 받음.
void write(); // 정수 배열을 화면에 출력.
int big(); // 정수 배열에서 가장 큰 수 리턴.
~Sample(); // 소멸자.
};
void Sample::read() {
for (int i = 0; i < this->size; i++) {
cin >> this->p[i];
}
}
void Sample::write() {
for (int i = 0; i < this->size; i++) {
cout << this->p[i] << ' ';
}
cout << endl;
}
int Sample::big() {
int max = 0;
for (int i = 0; i < this->size; i++) {
if (max < this->p[i]) {
max = this->p[i];
}
}
return max;
}
Sample::~Sample() {
}
int main() {
// ex : 100 4 -2 9 55 300 44 38 99 -500
Sample s(10); // 10개 정수 배열을 가진 Sample 객체 생성.
s.read(); // 키보드에서 정수 배열 읽기.
s.write(); // 정수 배열 출력.
cout << "가장 큰 수는 " << s.big() << endl; // 가장 큰 수 출력.
}
|
cs |
출력결과
5번 문제
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
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
// 5번 문제
int main() {
// ex : Falling in love with you. , hello world , exit
srand((unsigned)time(NULL));
cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" << endl;
while (1) {
string str, re;
cout << ">>";
getline(cin, str, '\n');
if (str == "exit") {
break;
}
int n = rand() % str.length(); int A = rand() % 100 + 1; int a = rand() % 100 + 1;
re = A > a ? rand() % (90 + 1 - 65) + 65 : rand() % (122 + 1 - 97) + 97;
if (str[n] != ' ') {
str.replace(n, 1, re);
}
else {
str.replace(n + 1, 1, re);
}
cout << str << endl;
}
return 0;
}
|
cs |
출력결과
7번 문제
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
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
//7번 문제
class Circle {
int radius;
public:
void setRadius(int radius);
double getArea();
};
void Circle::setRadius(int r) {
this->radius = r;
}
double Circle::getArea() {
return this->radius * this->radius * 3.14;
}
int main() {
Circle arr[3];
int cnt = 0;
for (int i = 0; i < 3; i++) {
int n;
cout << "원 " << i + 1 << "의 반지름 >> ";
cin >> n;
arr[i].setRadius(n);
if (100 < arr[i].getArea()) cnt++;
}
cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다";
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
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
//8번 문제
class Circle {
int radius;
public:
void setRadius(int radius);
double getArea();
};
void Circle::setRadius(int r) {
this->radius = r;
}
double Circle::getArea() {
return this->radius * this->radius * 3.14;
}
int main() {
int num;
cout << "원의 개수 >> ";
cin >> num;
Circle* arr = new Circle[num];
int cnt = 0;
for (int i = 0; i < num; i++) {
int n;
cout << "원 " << i + 1 << "의 반지름 >> ";
cin >> n;
arr[i].setRadius(n);
if (100 < arr[i].getArea()) cnt++;
}
cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다";
return 0;
}
|
cs |
출력결과
9번 문제
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
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
//9번 문제
class Person {
string name;
string tel;
public:
Person();
string getName() { return name; }
string getTel() { return tel; }
void set(string name, string tel);
};
Person::Person() { };
void Person::set(string name, string tel) {
this->name = name;
this->tel = tel;
}
int main() {
//ex 스폰지밥 010-0000-0000 뚱이 011-1111-1111 징징이 012-2222-2222
Person arr[3];
cout << "이름과 전화 번호를 입력해 주세요" << endl;
for(int i = 0; i < 3; i++){
string str, num;
cout << "사람 " << i+1 << ">> ";
cin >> str >> num;
arr[i].set(str, num);
}
cout << "모든 사람의 이름은 ";
for (auto i : arr) {
cout << i.getName() << ' ';
}
string search;
cout << endl;
cout << "전화번호 검색합니다. 이름을 입력하세요>>";
cin >> search;
for (auto i : arr) {
if (search == i.getName()) {
cout << "전화 번호는 " << i.getTel();
}
}
return 0;
}
|
cs |
출력결과
11번 문제
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
//11번 문제
#define MAX 10
#define coffee 0
#define water 1
#define sugar 2
class Container { // 커피 자판기 작동
int size; // 현재 저장 량, 최대 저장량은 10
public:
Container() { size = 10; }
void fill(); // 최대량(10)으로 채우기
void consume(); // 1 만큼 소모하기
int getSize(); // 현재 크기 리턴
};
void Container::fill() {
this->size = MAX;
}
void Container::consume() {
this->size -= 1;
}
int Container::getSize() {
return size;
}
class CoffeeVendingMachine { // 커피자판기를 표현하는 클래스
Container tong[3]; // tong[0]는 커피, tong[1]을 물, tong[2]는 설탕통을 나타냄
void fill(); // 3개의 통을 모두 10으로 채움
void selectEspresso(); // 에스프레소를 선택한 경우, 커피 1, 물 1 소모
void selectAmericano(); // 아메리카노를 선택한 경우, 커피 1, 물 2 소모
void selectSugarCoffee(); // 설탕커피를 선택한 경우, 커피 1, 물 2 소모, 섵탕 1 소모
void show(); // 현재 커피, 물 ,설탕의 잔량 출력
public:
void run(); // 커피 자판기 작동
};
void CoffeeVendingMachine::fill() {
for (int i = 0; i < 3; i++) {
tong[i].fill();
}
}
void CoffeeVendingMachine::selectEspresso() {
if (tong[coffee].getSize() >= 1 && tong[water].getSize() >= 1) {
tong[coffee].consume(); tong[water].consume();
cout << "에스프레소 드세요" << endl;
}
}
void CoffeeVendingMachine::selectAmericano() {
if (tong[coffee].getSize() >= 1 && tong[water].getSize() >= 2 ) {
tong[coffee].consume(); tong[water].consume(); tong[water].consume();
cout << "아메리카노 드세요" << endl;
}
cout << "아메리카노 드세요" << endl;
}
void CoffeeVendingMachine::selectSugarCoffee() {
if (tong[coffee].getSize() >= 1 && tong[water].getSize() >= 2 && tong[sugar].getSize() >= 1) {
tong[coffee].consume(); tong[water].consume(); tong[water].consume(); tong[sugar].consume();
cout << "설탕커피 드세요" << endl;
}
else {
cout << "원료가 부족합니다." << endl;
}
}
void CoffeeVendingMachine::show() {
cout << "커피 " << tong[coffee].getSize() << ", 물 " << tong[water].getSize() << ", 설탕" << tong[sugar].getSize() << endl;
}
void CoffeeVendingMachine::run() {
CoffeeVendingMachine Machine;
Machine.fill();
cout << "***** 커피자판기를 작동합니다. *****" << endl;
while (1) {
int op;
cout << "메뉴를 눌러주새요(1:에스프레소, 2:아메리카노, 3:설탕커피, 4:잔량보기, 5:채우기)>> ";
cin >> op;
if (op == 1) {
Machine.selectEspresso();
}
else if (op == 2) {
Machine.selectAmericano();
}
else if (op == 3) {
Machine.selectSugarCoffee();
}
else if (op == 4) {
Machine.show();
}
else if (op == 5) {
Machine.fill();
Machine.show();
}
}
}
int main() {
CoffeeVendingMachine machine;
machine.run();
}
|
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
//12번 문제
class Circle {
int radius;
string name;
public:
void setCircle(string name, int radius);
double getArea();
string getName();
};
void Circle::setCircle(string name, int radius) {
this->name = name; this->radius = radius;
}
double Circle::getArea() {
return this->radius * this->radius * 3.14;
}
string Circle::getName() {
return this->name;
}
class CircleManager {
Circle *p;
int size;
public:
CircleManager(int size);
~CircleManager();
void searchByName();
void searchByArea();
};
CircleManager::CircleManager(int size) {
this->size = size;
p = new Circle[this->size];
for (int i = 0; i < size; i++) {
string str; int num;
cout << "원 " << i + 1 << "의 이름과 반지름 >> ";
cin >> str >> num;
p[i].setCircle(str, num);
cout << p[i].getArea() << " " << p[i].getName() << endl;
}
}
CircleManager::~CircleManager() {
delete[] p;
}
void CircleManager::searchByName() {
string str;
cout << "검색하고자 하는 원의 이름 >>";
cin >> str;
for (int i = 0; i < size; i++) {
if (str == p[i].getName()) {
cout << p[i].getName() << "의 면적은 " << p[i].getArea() << endl;
}
}
}
void CircleManager::searchByArea() {
double n;
cout << "최소 면적을 정수로 입력하세요 >> ";
cin >> n;
cout << n << "보다 큰 원을 검색합니다." << endl;
for (int i = 0; i < this->size; i++) {
if (n < p[i].getArea()) {
cout << p[i].getName() << "의 면적은 " << p[i].getArea() << ',';
}
}
}
int main() {
int n;
cout << "원의 개수 >> ";
cin >> n;
CircleManager circle(n);
circle.searchByName();
circle.searchByArea();
return 0;
}
|
cs |
출력결과
14번 문제
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#include <iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
//12번 문제
class Player {
string name;
public:
void setName(string name);
string printName();
};
void Player::setName(string name) {
this->name = name;
}
string Player::printName() {
return name;
}
class GamblingGame {
int random_num[3];
Player user1, user2;
public:
GamblingGame();
void getName();
int* random_number();
};
GamblingGame::GamblingGame() {
cout << "***** 갬블링 게임을 시작합니다. *****" << endl;
getName();
bool turn = true;
while (1) {
if (turn) {
turn = false;
cout << user1.printName() << ":<Enter>" << endl;
}
else {
turn = true;
cout << user2.printName() << ":<Enter>" << endl;
}
if (cin.get() == '\n') {
random_number();
cout << " ";
for (int i = 0; i < 3; i++) {
cout << random_num[i] << " ";
}
if (random_num[0] == random_num[1] && random_num[0] == random_num[2] && random_num[1] == random_num[2]) {
if (!turn) {
cout << user1.printName() << "님 승리!!";
break;
}
else {
cout << user2.printName() << "님 승리!!";
break;
}
}
else {
cout << "아쉽군요!" << endl;
}
}
}
}
void GamblingGame::getName() {
string user;
cout << "첫번째 선수 이름>>";
cin >> user; user1.setName(user);
cout << "두번째 선수 이름>>";
cin >> user; user2.setName(user);
}
int* GamblingGame::random_number() {
srand((unsigned)time(NULL));
for (int i = 0; i < 3; i++) {
random_num[i] = rand() % 3;
}
return random_num;
}
int main() {
GamblingGame Game_Start;
}
|
cs |
출력결과
'Programming > 명품 C++ Programming' 카테고리의 다른 글
명품 C++ Programming Chapter 05 실습문제 (0) | 2021.02.25 |
---|---|
명품 C++ Programming CHAPTER 03 OpenChallenge (0) | 2021.02.22 |
명품 C++ Programming Open Challenge 4장 한글 끝말잇기 게임 (1) | 2019.05.06 |
Comments