다라다라V
article thumbnail
728x90
반응형
본 글은 "황기태"님의 [명품 C++ Programming]의 연습 문제 답을 공유하고자 작성되었으며, 필자가 직접 문제를 풀며 작성한 것이기에 오류가 있을 수 있습니다. 댓글로 알려주시면 반영하도록 하겠습니다.

시리즈 보기
[C++] 명품 C++ Programming 1장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 1장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 2장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 2장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 3장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 4장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 4장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 5장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 6장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 7장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 8장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 8장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 9장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 9장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 10장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 10장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 11장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 11장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 12장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 12장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 13장 연습 문제 풀이 (이론 문제)

 

💡 

[정답]

#include <iostream>
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);
	Color* p;
	p = &screenColor;// (1)
	p->show();		 // (2)
	Color colors[3]; // (3)
	p = colors;		 // (4)

	// (5)
	p->setColor(255, 0, 0);
	(p + 1)->setColor(0, 255, 0);
	(p + 2)->setColor(0, 0, 255);

	// (6)
	for (int i = 0; i < 3; i++) {
		p->show();
		p++;
	}
}

 

💡 

[정답]

#include <iostream>
using namespace std;

int main() {
	int* arr = new int[5];
	double average = 0;

	cout << "정수 5개 입력>> ";
	for (int i = 0; i < 5; i++) {
		cin >> arr[i];
		average += arr[i];
	}

	cout << "평균 " << average / 5 << endl;
}

 

💡 

(1)

[정답]

#include <iostream>
#include <string>
using namespace std;

int main() {
	string str;
	int cnt = 0;

	cout << "문자열 입력>> ";
	getline(cin, str);
	for (int i = 0; i < str.length(); i++) {
		if (str[i] == 'a') cnt++;
	}

	cout << "문자 a는 " << cnt << "개 있습니다." << endl;
}

 

(2)

[정답]

#include <iostream>
#include <string>
using namespace std;

int main() {
	string str;
	int cnt = 0;
	int index = 0;

	cout << "문자열 입력>> ";
	getline(cin, str);

	while (true) {
		index = str.find('a', index + 1);
		if (index == -1) break;
		else cnt++;
	}

	cout << "문자 a는 " << cnt << "개 있습니다." << endl;
}

 

💡 

[정답]

#include <iostream>
#include <string>

using namespace std;

class Sample {
	int* p;
	int size;
public:
	Sample(int n) { size = n; p = new int[n]; }
	void read();
	void write();
	int big();
	~Sample();
};

// 동적 할당받은 정수 배열 p에 사용자로부터 정수를 입력받음
void Sample::read() {
	for (int i = 0; i < size; i++)
		cin >> p[i];
}
//정수 배열을 화면에 출력
void Sample::write() {
	for (int i = 0; i < size; i++)
		cout << p[i] << ' ';
	cout << endl;
}
//정수 배열에서 가장 큰 수 리턴
int Sample::big() {
	int max  = p[0];
	for (int i = 1; i < size; i++)
		if (p[i] > max) max = p[i];
	return max;
}
// 소멸자
Sample::~Sample() {
	delete[] p;
}

int main() {
	Sample s(10);
	s.read();
	s.write();
	cout << "가장 큰 수는 " << s.big() << endl;
}

 

💡 

랜덤 정수 발생을 위해 두 코드가 필요합니다. <cstdlib>와 <ctime> 헤더 파일을 include 해야합니다.

[정답]

 

 

💡 

[정답]

#include <iostream>
#include <string>
using namespace std;

int main() {
	string str;

	cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" << endl;
	while (true) {
		cout << ">>";
		getline(cin, str);
		if (str.compare("exit") == 0) break;

		for (int i = str.length() - 1; i >= 0; i--) {
			cout << str[i];
		}
		cout << endl;
	}
}

 

💡 

[정답]

#include <iostream>
using namespace std;

class Circle {
	int radius;
public:
	void setRadius(int radius) { this->radius = radius; }
	double getArea() { return 3.14 * radius * radius; }
};

int main() {
	Circle circles[3];
	int cnt = 0, r;

	for (int i = 0; i < 3; i++) {
		cout << "원 " << i + 1 << "의 반지름 >> ";
		cin >> r;
		circles[i].setRadius(r);

		if (circles[i].getArea() > 100) {
			cnt++;
		}
	}
	cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다" << endl;
}

 

💡 

[정답]

#include <iostream>
using namespace std;


class Circle {
	int radius;
public:
	void setRadius(int radius) { this->radius = radius; }
	double getArea() { return 3.14 * radius * radius; }
};


int main() {
	int cnt = 0, size;

	cout << "원의 개수 >> ";
	cin >> size;
	Circle* circles = new Circle[size];

	for (int i = 0; i < size; i++) {
		int r;
		cout << "원 " << i + 1 << "의 반지름 >> ";
		cin >> r;
		circles[i].setRadius(r);

		if (circles[i].getArea() > 100) {
			cnt++;
		}
	}
	cout << "면적이 100보다 큰 원은 " << cnt << "개 입니다" << endl;
}

 

💡 

[정답]

#include <iostream>
#include <string>
using namespace std;

class Person {
	string name;
	string phone;
public:
	Person() { name = ""; phone = ""; };
	string getName() { return name; }
	string getPhone() { return phone; }
	void set(string name, string phone) {
		this->name = name;
		this->phone = phone;
	}
};

int main() {
	Person person[3];
	string name, phone;
	cout << "이름과 전화 번호를 입력해 주세요" << endl;
	for (int i = 0; i < 3; i++) {
		cout << "사람 " << i + 1 << ">> ";
		cin >> name >> phone;
		person[i].set(name, phone);
	}

	cout << "모든 사람의 이름은 ";
	for (int i = 0; i < 3; i++)
		cout << person[i].getName() << ' ';
	cout << endl;

	cout << "전화번호 검색합니다. 이름을 입력하세요>>";
	string search;
	cin >> search;
	for (int i = 0; i < 3; i++) {
		if (search.compare(person[i].getName()) == 0) {
			cout << "전화 번호는 " << person[i].getPhone() << endl;
			break;
		}
	}
}

 

💡 

[정답]

#include <iostream>
#include <string>
using namespace std;

class Person {
	string name;
	string phone;
public:
	Person() { name = ""; phone = ""; };
	void set(string name, string phone) {
		this->name = name;
		this->phone = phone;
	}
	string getName() { return name; }
	string getPhone() { return phone; }
	void setName(string name) { this->name = name; }
	void setPhone(string phone) { this->phone = phone; }
};

class Family {
	Person* p;
	string name;
	int size;
public:
	Family(string name, int size) {
		p = new Person[size];
		this->name = name;
		this->size = size;
	}
	void show() {
		cout << name << "가족은 다음과 같이 " << size << "명 입니다." << endl;
		for (int i = 0; i < size; i++) {
			cout << p[i].getName() << '\t';
		}
		cout << endl;
	}
	void setName(string name, int index) { p[index].setName(name); }
	~Family() { delete[] p; };
};

int main() {
	Family* simpson = new Family("Simpson", 3);
	simpson->setName("Mr.Simpson", 0);
	simpson->setName("Mrs.Simpson", 1);
	simpson->setName("Bart Simpson", 2);
	simpson->show();
	delete simpson;
}

 

💡 

[정답]

#include <iostream>
#include <string>
using namespace std;

// 이 클래스가 멤버로 들어가 문제에서 제공한 순서와 다르게 작성하였습니다.
class Container {
	int size;
public:
	Container() { size = 10; }
	void fill() { size = 10; }
	void consume() { size -= 1; };
	int getSize() { return size; };
};

class CoffeeVendingMachine {
	Container tong[3]; // tong[0]은 커피, tong[1]은 물, tong[2]은 설탕
	void fill() {
		for (int i = 0; i < 3; i++)
			tong[i].fill();
	}
	void selectEspresso() {
		tong[0].consume();
	}
	void selectAmericano() {
		tong[0].consume();
		tong[1].consume();
	}
	void selectSugarCoffee() {
		tong[0].consume();
		tong[1].consume();
		tong[2].consume();
	}
	void show() {
		cout << "커피 " << tong[0].getSize() << ", ";
		cout << "물 " << tong[1].getSize() << ", ";
		cout << "설탕 " << tong[2].getSize() << endl;
	}
public:
	void run() {
		cout << "**** 커피자판기를 작동합니다. *****" << endl;

		int select;
		while (true) {
			cout << "메뉴를 눌러주세요(1:에스프레소,2:아메리카노,3:설탕커피,4:잔량보기,5:채우기)>>";
			cin >> select;
			switch (select) {
			case 1:
				selectEspresso();
				show();
				break;
			case 2:
				selectAmericano();
				show();
				break;
			case 3:
				selectSugarCoffee();
				show();
				break;
			case 4:
				show();
				break;
			case 5:
				fill();
				show();
				break;
			}
		}
	}
};

int main() {
	CoffeeVendingMachine cvm;
	cvm.run();
}

 

💡 

[정답]

#include <iostream>
#include <string>
using namespace std;

class Circle {
	string name;
	int raidus;
public:
	void setCircle(string name, int radius) {
		this->name = name;
		this->raidus = radius;
	}
	double getArea() { return raidus * raidus * 3.14; }
	string getName() { return name; }
};

class CircleManager {
	Circle* p;
	int size;
public:
	CircleManager(int size) { 
		p = new Circle[size];
		this->size = size;

		string name;
		int r;
		for (int i = 0; i < size; i++) {
			cout << "원 " << i + 1 << "의 이름과 반지름 >> ";
			cin >> name >> r;
			cin.get();
			p[i].setCircle(name, r);
		}
	}
	~CircleManager() { delete[] p; }
	void searchByName() {
		string name;
		cout << "검색하고자 하는 원의 이름 >> ";
		getline(cin, name);

		for (int i = 0; i < size; i++) {
			if (name.compare(p[i].getName()) == 0) {
				cout << name << "의 면적은 " << p[i].getArea() << endl;
				break;
			}
		}
	}
	void searchByArea() {
		int area;
		cout << "최소 면적을 정수로 입력하세요 >> ";
		cin >> area;
		cout << area << "보다 큰 원을 검색합니다." << endl;

		for (int i = 0; i < size; i++) {
			if (p[i].getArea() > area) 
				cout << p[i].getName() << "의 면적은 " << p[i].getArea() << ",";
		}
		cout << endl;
	}
};

int main() {
	int number;
	cout << "원의 개수 >> ";
	cin >> number;

	CircleManager c(number);

	c.searchByName();
	c.searchByArea();
}

 

 

 

 

반응형
profile

다라다라V

@DaraDaraV

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!