C++/명품 C++ Programming

[9]명품 C++ Programming -5장[ 함수와 참조, 복사 생성자](2)-[실습문제]

Tomato_Coffee 2022. 6. 6. 15:25

1번

#include<iostream>
using namespace std;
class Circle {
private:
	int radius;
public:
	Circle();
	void setRadius(int r);
	void getRadius();
};
Circle::Circle() {
	this->radius = 1;
}
void Circle::getRadius() {
	cout << "원의 반지름은 R = " << this->radius << "입니다. " << endl;
}
void Circle::setRadius(int r) {
	this->radius = r;
}
void swap(Circle& x, Circle& y) {
	Circle tmp;
	tmp = x;
	x = y;
	y = tmp;
}
int main() {

	Circle a;
	Circle b;
	a.setRadius(30);
	b.setRadius(50);

	swap(a, b);
	a.getRadius();
	b.getRadius();
}

2번

#include<iostream>
using namespace std;
void half(double& k) {
	 k /= 2;
}
int main() {
	double n = 20;
	half(n);
	cout << n;
}

3번

#include<iostream>
#include<string>
using namespace std;
void combine(string &a, string &b, string &c) {
	c = a + " " + b;
}
int main() {
	string text1("I love you"), text2("very much");
	string text3;
	combine(text1, text2, text3);
	cout << text3;
}

4번

#include<iostream>
using namespace std;

bool bigger(int a, int b, int& big);
int main() {
	int x, y;
	int big;
	cin >> x >> y;
	bigger(x, y, big);
	cout << big << endl;
}
bool bigger(int a, int b, int& big) {
	
	big = a > b ? a : b;
	if (a == b)
		return true;
	else
		return false;
}

5번

#include<iostream>
using namespace std;

class Circle {
private:
	int radius;
public:
	Circle(int r) { radius = r; }
	int getRadius() { return radius; }
	void setRadius(int r) { 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();
}

6번

#include<iostream>
#include<cstring>
using namespace std;
char& find(char a[], char c, bool& sucess);
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;
}
char& find(char a[], char c, bool& success) {

	for (int i = 0; i < strlen(a); i++) {
		if (a[i] == c) {
			success = true;
			return a[i];
		}
	}
}

7번

#include<iostream>
using namespace std;
class MyIntStack {
private:
	int p[10];
	int tos;
public:
	MyIntStack();
	bool push(int n);
	bool pop(int& n);
};
MyIntStack::MyIntStack() {
	tos = -1;
}
bool MyIntStack::push(int n) {
	if (tos < 9)
	{
		p[++tos] = n;
		return true;
	}
	else
		return false;
}
bool MyIntStack::pop(int& n) {
	if (tos >= 0) {
		n = p[tos--];
		return true;
	}
	else {
		return false;
	}
}
int main() {
	MyIntStack a;
	for (int i = 0; i < 11; i++) {
		if (a.push(i))cout << i << ' ';
		else cout << endl << i + 1 << " 번째 stack full" << endl;
	}
	int n;
	for (int i = 0; i < 11; i++) {
		if (a.pop(n)) cout << n << ' ';
		else cout << endl << i + 1 << " 번째 stack empty";
	}
	cout << endl;
}

8번

#include<iostream>
using namespace std;
class MyIntStack {
private:
	int *p;
	int size;
	int tos;
public:
	MyIntStack();
	MyIntStack(int size);
	MyIntStack(const MyIntStack& s);// 복사 생성자
	~MyIntStack();
	bool push(int n);
	bool pop(int& n);
};
MyIntStack::MyIntStack() {
	this->tos = -1;
}
MyIntStack::MyIntStack(int size) {
	this->size = size;
	this->tos = -1;
	p = new int[size];
}
MyIntStack::MyIntStack(const MyIntStack& s) {// 복사 생성자
	int len = s.size;
	this->size = len;
	this->tos = s.tos;
	p = new int[len];
	for (int i = 0; i <= this->tos; i++) {
		this->p[i] = s.p[i];
	}
	
}
MyIntStack::~MyIntStack() {
	//cout << "소멸자 메모리 해제" << endl;
	delete[] p;
}
bool MyIntStack::push(int n) {
	if (tos < size-1)
	{
		p[++tos] = n;
		return true;
	}
	else
		return false;
}
bool MyIntStack::pop(int& n) {
	if (tos >= 0) {
		n = p[tos--];
		return true;
	}
	else {
		return false;
	}
}
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;
}

9번

#include<iostream>
using namespace std;
class Accumulator {
private:
	int value;
public:
	Accumulator(int value);
	Accumulator& add(int n);
	int get();
};
Accumulator::Accumulator(int value) {
	this->value = value;
}
Accumulator& Accumulator:: add(int n) {
	this->value += n;
	return *this;
}
int Accumulator::get() {
	return value;
}
int main() {
	Accumulator acc(10);
	acc.add(5).add(6).add(7);
	cout << acc.get();
}

10번

#include<iostream>
using namespace std;
class Buffer {
private:
	string text;
public:
	Buffer(string text) { this->text = text; }
	void add(string next) { text += next; }
	void print() { cout << text << endl; }
};
Buffer& append(Buffer& a, string b) {
	a.add(b);
	return a;
}
int main() {
	Buffer buf("Hello");
	Buffer& temp = append(buf, "Guys");
	temp.print();
	buf.print();
}

11번

#include<iostream>
#include<string>
using namespace std;
class Book {
private:
	string title;
	int price;
public:
	Book(string title, int price);
	~Book();
	void set(string title, int price);
	void show() { cout << title << " " << price << "원" << endl; }
};
Book::Book(string title, int price) {
	this->price = price;
	this->title = title;
}
Book::~Book() {

}
void Book::set(string title, int price) {
	this->price = price;
	this->title = title;
}
int main() {
	Book cpp("명품C++", 10000);
	Book java = cpp;// 복사 생성자
	java.set("명품자바", 12000);
	cpp.show();
	java.show();
}

12번

#include<iostream>
#include<string>
using namespace std;
class Dept {
private:
	int size;
	int* scores;
public:
	Dept(int size) {
		this->size = size;
		scores = new int[size];
	}
	Dept(const Dept& dept);
	~Dept();
	int getSize() { return size; }
	void read();
	bool isOver60(int index);
};
Dept::~Dept() {
	delete[]scores;
}
Dept::Dept(const Dept& dept) {
	int len = dept.size;
	this->size = len;
	this->scores = new int[len];
	for (int i = 0; i < len; i++) {
		this->scores[i] = dept.scores[i];
	}
}
void Dept::read() {
	int n;
	cout << "10개 점수 입력>> ";
	for (int i = 0; i < this->size; i++) {// 이 부분이 내가 잘 까먹는 부분
		cin >> n;
		scores[i] = n;
	}
}
bool Dept::isOver60(int index) {
	if (scores[index] >= 60)
		return true;
	return false;
}
int countPass(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);
	cout << "60점 이상은 " << n << "명";
}