대학교 수업/C++ 프로그래밍

[C++] 명품 c++ 프로그래밍 5장

Jongung 2022. 4. 20. 23:44

1번

#include <iostream>
using namespace std;

class Circle {
	int radius;
public:
	Circle() :Circle(1) {}
	Circle(int r) { radius = r; }
	void show() { cout << "반지름 : " << this->radius << endl; }
	int getRadius() { return radius; }
};

void swap(Circle& a, Circle& b) {
	Circle tmp;
	tmp = a;
	a = b;
	b = tmp;
}

int main() {
	Circle a(1), b(2);
	a.show();
	b.show();

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

1번

 

2번

#include <iostream>
using namespace std;

void half(double& n) {
	n /= 2;
}

int main() {
	double n = 20;
	half(n);
	cout << n;
}

2번

 

3번

#include <iostream>
using namespace std;

void combine(string& text1, string& text2, string& text3) {
	text3 = text1 + " " + text2;
}


int main() {
	string text1("I love you"), text2("very much");
	string text3;
	combine(text1, text2, text3);
	cout << text3;
}

3번

 

4번

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

bool bigger(int a, int b, int& big) {
	if (a == b) {
		return true;
	}
	else {
		if (a > b) big = a;
		else big = b;
		return false;
	}
}

int main() {
	int a, b, big;

	cin >> a >> b;
	if (bigger(a, b, big)) {
		cout << "두 수는 같습니다.";
	}
	else {
		cout << a << "와" << b << "중 큰 수는" << big << "입니다." << endl;
	}
}

4번

 

5번

#include <iostream>
using namespace std;

class Circle {
	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();
}

5번

 

 

6번

#include <iostream>
using namespace std;

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];
		}
	}
	return a[0];
}

int main(){
	char s[] = "Mike";
	bool b = false;
	char& loc = find(s, 'M', b);
	if(b == false){
		cout << "M을 발견할 수 없다" << endl;
	}
	loc = 'm';
	cout << s << endl;
}

6번

 

 

7번

#include <iostream>
using namespace std;

class MyIntStack{
	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){
		return false;
	}
	else{
		tos++;
		p[tos] = n;
		return true;
	}
}
bool MyIntStack::pop(int &n){
	if(tos < 0){
		return false;
	}
	else{
		n = p[tos];
		tos--;
		return true;
	}
}

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" << endl;
	}
}

7번

 

8번

#include <iostream>
using namespace std;

class MyIntStack{
	int *p;
	int size;
	int tos;
public:
	MyIntStack():MyIntStack(1){};
	MyIntStack(int size);
	MyIntStack(MyIntStack& s);
	~MyIntStack();
	bool push(int n);
	bool pop(int &n);
};
MyIntStack::MyIntStack(int n){
	this->p = new int[n];
	this->size = n;
	this->tos = -1;
}
MyIntStack::MyIntStack(MyIntStack& s){
	int length = s.size;
	this->p = new int[length];
	this->size = s.size;
	this->tos = s.tos;
	for(int i = 0; i <= tos; i++){
		this->p[i] = s.p[i];
	}
}
MyIntStack::~MyIntStack() {
	delete[] p;
}
bool MyIntStack::push(int n){
	if(tos >= 9) return false;
	tos++;
	p[tos] = n;
	return true;
}
bool MyIntStack::pop(int& n){
	if(tos < 0) return false;
	n = p[tos];
	tos--;
	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;
}

8번

 

9번

#include <iostream>
using namespace std;

class Accumlator{
	int value;
public:
	Accumlator(int value);
	Accumlator& add(int n);
	int get();
};
Accumlator::Accumlator(int value){
	this->value = value;
}
Accumlator& Accumlator::add(int n){
	this->value += n;
	return *this;
}

int Accumlator::get(){
	return this->value;
}

int main(){
	Accumlator acc(10);
	acc.add(5).add(6).add(7);
	cout << acc.get();
}

9번

 

10번

#include <iostream>
using namespace std;

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 text);
};
Buffer& append(Buffer& buf, string text){
		buf.add(text);
		return buf;
}

int main(){
	Buffer buf("Hello");
	Buffer& temp = append(buf, "Guys");
	temp.print();
	buf.print();
}

10번

 

11번

#include <iostream>
using namespace std;

class Book{
	string title;
	int price;
public:
	Book(string title, int price);
	Book(Book &b);
	~Book();
	void set(string title, int price);
	void show() {cout << title << ' ' << price << "원" << endl; }
};
Book::Book(string title, int price){
	this->title = title;
	this->price = price;
}
Book::~Book(){

}
Book::Book(Book &b){
	this->title = b.title;
	this->price = b.price;
}
void Book::set(string title, int price){
	this->title = title;
	this->price = price;
}

int main(){
	Book cpp("명품C++", 10000);
	Book java = cpp;
	java.set("명품자바", 12000);
	cpp.show();
	java.show();
}

11번

 

12번

#include <iostream>
using namespace std;

class Dept{
	int size;
	int* scores;
public:
	Dept(int size){
		this->size = size;
		scores = new int[size];
	}
	Dept(Dept& dept);
	~Dept();
	int getSize(){return size;}
	void read();
	bool isOver60(int index);
};
void Dept::read(){
	for(int i = 0; i < size; i++){
		cin >> this->scores[i];
	}
}
bool Dept::isOver60(int index){
	if(scores[index] >= 60){
		return true;
	}
	return false;
}
Dept::~Dept(){
	
}
Dept::Dept(Dept& dept){
	this->size = dept.size;
	this->scores = dept.scores;
}

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 << "명";
}

12번