본문 바로가기
문제풀이/명품 C++ Programming

[명품 C++ Programming] 3장 실습 문제

by 민됴리 2020. 12. 14.
반응형

2018년에 3월 5일에 발행된

황기태 저자의

명품 C++ Programming 개정판

3장 실습 문제입니다.

 

저작권을 준수하기 위해서

책에 나와있는 문제를 요약해서 적었습니다.

 

궁금한 점은 댓글로 남겨주세요.

 


 

3-1. Tower 클래스 작성하기

class Tower {
	int height;
public:
	Tower();
	Tower(int h);
	int getHeight();
};

Tower::Tower() {
	height = 1;
}

Tower::Tower(int h) {
	height = h;
}

int Tower::getHeight() {
	return height;
}

 

3-2. Date 클래스 작성하기

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class Date {
	int year;
	int month;
	int day;
public:
	Date(int y, int m, int d);
	Date(string strDate);
	void show();
	int getYear();
	int getMonth();
	int getDay();
};

Date::Date(int y, int m, int d) {
	year = y;
	month = m;
	day = d;
}

Date::Date(string strDate) {
	char* str_buff = new char[100];
	strcpy(str_buff, strDate.c_str());

	year = stoi(strtok(str_buff, "/"));
	month = stoi(strtok(nullptr, "/"));
	day = stoi(strtok(nullptr, "/"));
}

void Date::show() {
	cout << year << "년" << month << "월" << day << "일" << endl;
}

int Date::getYear() {
	return year;
}

int Date::getMonth() {
	return month;
}

int Date::getDay() {
	return day;
}

int main(void) {
	Date birth(2014, 3, 20);
	Date independenceDay("1945/8/15");
	independenceDay.show();
	cout << birth.getYear() << ',' << birth.getMonth() << ',' << birth.getDay() << endl;

	return 0;
}

 

3-3. Account 클래스 작성하기

class Account {
	string name;
	int id;
	int balance;
public:
	Account(string name, int id, int balance);
	int withdraw(int money);	//출금
	void deposit(int money);	//저금
	string getOwner();
	int inquiry();
};

Account::Account(string name, int id, int balance) {
	this->name = name;
	this->id = id;
	this->balance = balance;
}

int Account::withdraw(int money) {
	balance -= money;
	return money;
}

void Account::deposit(int money) {
	balance += money;
}

string Account::getOwner() {
	return name;
}

int Account::inquiry() {
	return balance;
}

 

3-4. CoffeeMachine 클래스 작성하기

class CoffeeMachine {
	int coffee;
	int water;
	int sugar;
public:
	CoffeeMachine(int coffee, int water, int sugar);
	void drinkEspresso();
	void drinkAmericano();
	void drinkSugarCoffee();
	void show();
	void fill();
};

CoffeeMachine::CoffeeMachine(int coffee, int water, int sugar) {
	this->coffee = coffee;
	this->water = water;
	this->sugar = sugar;
}

void CoffeeMachine::drinkEspresso() {
	coffee -= 1;
	water -= 1;
}

void CoffeeMachine::drinkAmericano() {
	coffee -= 1;
	water -= 2;
}

void CoffeeMachine::drinkSugarCoffee() {
	coffee -= 1;
	water -= 2;
	sugar -= 1;
}

void CoffeeMachine::fill() {
	coffee = 10;
	water = 10;
	sugar = 10;
}

void CoffeeMachine::show() {
	cout << "커피 머신 상태, 커피: " << coffee << " \t물: " << water << "\t설탕: " << sugar << endl;
}

 

3-5. Random 클래스 작성하기

class Random {
	int start;
	int end;
public:
	Random();
	int next();
	int nextInRange(int start, int end);
};

Random::Random() {
	srand((unsigned int)time(NULL));
	start = 0;
	end = RAND_MAX;
}

int Random::next() {
	return rand() % (RAND_MAX + 1);
}

int Random::nextInRange(int start, int end) {
	return rand() % (end - start + 1) + start;
}

 

3-6. EvenRandom 클래스 작성하고 프로그램 완성하기

class EvenRandom {
	int start;
	int end;
public:
	EvenRandom();
	int next();
	int nextInRange(int start, int end);
};

EvenRandom::EvenRandom() {
	srand((unsigned int)time(NULL));
	start = 0;
	end = RAND_MAX;
}

int EvenRandom::next() {
	while (true) {
		int even = rand() % (RAND_MAX + 1);
		if (even % 2 == 0) {
			return even;
		}
	}
}

 

3-7. SelectableRandom 클래스 작성하고 프로그램 완성하기

class SelectableRandom {
	int start;
	int end;
public:
	SelectableRandom();
	int next(int even_or_odd);
	int nextInRange(int start, int end, int even_or_odd);
};

SelectableRandom::SelectableRandom() {
	srand((unsigned int)time(NULL));
	start = 0;
	end = RAND_MAX;
}

int SelectableRandom::next(int even_or_odd) {
	int result;
	while (true) {
		result = rand() % (RAND_MAX + 1);
		if (result % 2 == even_or_odd) {
			return result;
		}
	}
}

int SelectableRandom::nextInRange(int start, int end, int even_or_odd) {
	int result;
	while (true) {
		result = rand() % (end - start + 1) + start;
		if (result % 2 == even_or_odd) {
			return result;
		}
	}
}

 

3-8. Integer 클래스 작성하기

class Integer {
	int num;
public:
	Integer(int num) {
		this->num = num;
	}
	Integer(string num) {
		this->num = stoi(num);
	}
	void set(int num) {
		this->num = num;
	}
	int get() {
		return num;
	}
	int isEven() {
		return true;
	}
};

 

3-9. Oval 클래스 작성하기

class Oval {
	int width;
	int height;
public:
	Oval();
	Oval(int width, int height);
	~Oval();
	int getWidth();
	int getHeight();
	void set(int w, int h);
	void show();
};

Oval::Oval() {
	width = 1;
	height = 1;
}

Oval::~Oval() {
	cout << "Oval 소멸: width = " << width << ", height = " << height << endl;
}

Oval::Oval(int width, int height) {
	this->width = width;
	this->height = height;
}

int Oval::getWidth() {
	return width;
}

int Oval::getHeight() {
	return height;
}

void Oval::set(int w, int h) {
	width = w;
	height = h;
}

void Oval::show() {
	cout << "width = " << width << ", height = " << height << endl;
}

 

3-10.

(1) 모든 코드를 하나의 cpp 파일에 작성하기

#include <iostream>
using namespace std;

class Add {
	int a;
	int b;
public:
	void setValue(int x, int y);
	int calculate();
};

void Add::setValue(int x, int y) {
	a = x; 
	b = y; 
}

int Add::calculate() { 
	return a + b; 
}

class Sub {
	int a;
	int b;
public:
	void setValue(int x, int y);
	int calculate();
};

void Sub::setValue(int x, int y) {
	a = x;
	b = y;
}

int Sub::calculate() {
	return a - b;
}

class Mul {
	int a;
	int b;
public:
	void setValue(int x, int y);
	int calculate();
};

void Mul::setValue(int x, int y) {
	a = x;
	b = y;
}

int Mul::calculate() {
	return a * b;
}

class Div {
	int a;
	int b;
public:
	void setValue(int x, int y);
	int calculate();
};

void Div::setValue(int x, int y) {
	a = x;
	b = y;
}

int Div::calculate() {
	return a / b;
}

int main(void) {

	Add a;
	Sub s;
	Mul m;
	Div d;
	
	int num1, num2;
	char oper;

	while (true) {
		cout << "두 정수와 연산자를 입력하세요>>";
		cin >> num1 >> num2 >> oper;
		switch (oper) {
		case '+':
			a.setValue(num1, num2);
			cout << a.calculate() << endl;
			break;
		case '-':
			s.setValue(num1, num2);
			cout << s.calculate() << endl;
			break;
		case '*':
			m.setValue(num1, num2);
			cout << m.calculate() << endl;
			break;
		case '/':
			d.setValue(num1, num2);
			cout << d.calculate() << endl;
			break;
		}
	}

	return 0;
}

 

(2) 헤더 파일과 cpp 파일로 나누어 작성하기

//Add.h
#ifndef ADD_H
#define ADD_H

class Add {
	int a;
	int b;
public:
	void setValue(int x, int y);
	int calculate();
};

#endif
//Add.cpp
#include "Add.h"

void Add::setValue(int x, int y) {
	a = x;
	b = y;
}

int Add::calculate() {
	return a + b;
}
//Sub.h
#ifndef SUB_H
#define SUB_H

class Sub {
	int a;
	int b;
public:
	void setValue(int x, int y);
	int calculate();
};

#endif
//Sub.cpp
#include "Sub.h"

void Sub::setValue(int x, int y) {
	a = x;
	b = y;
}

int Sub::calculate() {
	return a - b;
}
//Mul.h
#ifndef MUL_H
#define MUL_H

class Mul {
	int a;
	int b;
public:
	void setValue(int x, int y);
	int calculate();
};

#endif
//Mul.cpp
#include "Mul.h"

void Mul::setValue(int x, int y) {
	a = x;
	b = y;
}

int Mul::calculate() {
	return a * b;
}
//Div,h
#ifndef DEV_H
#define DEV_H

class Div {
	int a;
	int b;
public:
	void setValue(int x, int y);
	int calculate();
};

#endif
//Div.cpp
#include "Div.h"

void Div::setValue(int x, int y) {
	a = x;
	b = y;
}

int Div::calculate() {
	return a / b;
}
//main.cpp
#include <iostream>
using namespace std;

#include "Add.h"
#include "Sub.h"
#include "Mul.h"
#include "Div.h"

int main(void) {

	Add a;
	Sub s;
	Mul m;
	Div d;

	int num1, num2;
	char oper;

	while (true) {
		cout << "두 정수와 연산자를 입력하세요>>";
		cin >> num1 >> num2 >> oper;
		switch (oper) {
		case '+':
			a.setValue(num1, num2);
			cout << a.calculate() << endl;
			break;
		case '-':
			s.setValue(num1, num2);
			cout << s.calculate() << endl;
			break;
		case '*':
			m.setValue(num1, num2);
			cout << m.calculate() << endl;
			break;
		case '/':
			d.setValue(num1, num2);
			cout << d.calculate() << endl;
			break;
		}
	}

	return 0;
}

 

 

3-11. 주어진 코드를 헤더파일과 cpp 파일로 나누어서 작성하기

//Box.h
#ifndef BOX_H
#define BOX_H

class Box {
	int width, height;
	char fill;
public:
	Box(int w, int h);
	void setFill(char f);
	void setSize(int w, int h);
	void draw();
};

#endif
//Box.cpp
#include <iostream>
using namespace std;

#include "Box.h"

Box::Box(int w, int h) {
	setSize(w, h);
	fill = '*';
}

void Box::setFill(char f) {
	fill = f;
}

void Box::setSize(int w, int h) {
	width = w;
	height = h;
}

void Box::draw() {
	for (int n = 0; n < height; n++) {
		for (int m = 0; m < width; m++)
			cout << fill;
		cout << endl;
	}
}
//main.cpp
#include <iostream>
using namespace std;

#include "Box.h"

int main(void) {
	Box b(10, 2);
	b.draw();
	cout << endl;
	b.setSize(7, 4);
	b.setFill('^');
	b.draw();

	return 0;
}

 

3-12. 헤더 파일과 cpp 파일을 분리하여 프로그램 작성하기

//Ram.h
#ifndef RAM_H
#define RAM_H

class Ram {
	char mem[100 * 1024];
	int size;
public:
	Ram();
	~Ram();
	char read(int address);
	void write(int address, char value);
};

#endif
//Ram.cpp
#include <iostream>
using namespace std;

#include "Ram.h"

Ram::Ram() {
	for (int i = 0; i < 100 * 1024; i++) {
		mem[i] = 0;
	}
	size = 100 * 1024;
}

Ram::~Ram() {
	cout << "메모리 제거됨" << endl;
}

char Ram::read(int address) {
	return mem[address];
}

void Ram::write(int address, char value) {
	mem[address] = value;
}
//main.cpp
#include <iostream>
using namespace std;

#include "Ram.h"

int main(void) {
	Ram ram;
	ram.write(100, 20);
	ram.write(101, 30);
	char res = ram.read(100) + ram.read(101);
	ram.write(102, res);
	cout << "102 번지의 값 = " << (int)ram.read(102) << endl;
	return 0;
}

 

반응형