PAPAYA Corporation
명품 C++ Programming_제3장 실습문제
본문 바로가기
프로그래밍 언어

명품 C++ Programming_제3장 실습문제

by PAPAYA Corporation 2021. 4. 21.
반응형

1. main()의 실행 결과가 다음과 같도록 Tower 클래스를 작성하라.

#include <iostream>
using namespace std;

class Tower {
public:
	Tower();
	Tower(int hei);
	int hei;
	int getHei();
};
Tower::Tower()
{
	hei = 1;
}
Tower::Tower(int hei)
{
	this->hei = hei;
}
int Tower::getHei()
{
	return hei;
}
int main() {
	Tower myTower;
	Tower seoulTower(100);
	cout << "높이는 " << myTower.getHei() << "미터" << endl;
	cout << "높이는 " << seoulTower.getHei() << "미터" << endl;
}

 

2. 날짜를 다루는 Date 클래스를 작성하고자 한다.  Date를 이용하는 main()과 실행 결과는 다음과 같다. 클래스 Date를 작성하여 아래 프로그램에 추가하라.

#include <iostream>
#include <string>
using namespace std;
class Da {
public:
	int y;
	int m;
	int d;
	Da(int y, int m, int d);
	Da(string da);
	void show();
	int getY();
	int getM();
	int getD();
};
Da::Da(int y, int m, int d)
{
	this->y = y;
	this->m = m;
	this->d = d;
}
Da::Da(string da)
{
	int ind;
	this->y = stoi(da);
	ind = da.find('/');
	this->m = stoi(da.substr(ind + 1));
	ind = da.find('/', ind + 1);
	this->d = stoi(da.substr(ind + 1));
}
void Da::show()
{
	cout << y << "년" << m << "월" << d << "일" << endl;
}
int Da::getY()
{
	return y;
}
int Da::getM()
{
	return m;
}
int Da::getD()
{
	return d;
}
int main() {
	Da birth(2014, 3, 20);
	Da independenceDay("1945/8/15");
	independenceDay.show();
	cout << birth.getY() << '.' << birth.getM() << '.' << birth.getD() << endl;
}

*3. 은행에서 사용하는 프로그램을 작성하기 위해, 은행 계좌 하나를 표현하는 클래스 Account가 필요하다. 계좌 정보는 계좌의 주인, 계좌 번호, 잔액을 나타내는 3 멤버 변수로 이루어진다. main() 함수의 실행 결과가 다음과 같도록 Account 클래스를 작성하라.

#include <iostream>
#include <string>
using namespace std;
class Acco {
public:
	string na;
	int id;
	int bal;
	Acco(string na, int id, int bal);
	void depo(int mon);
	int withd(int mon);
	int inqui();
	string getOw();
};
Acco::Acco(string na, int id, int bal) {
	this->na = na;
	this->id = id;
	this->bal = bal;
}
void Acco::depo(int mon) {
	bal += mon;
}
int Acco::withd(int mon) {
	bal -= mon;
	return bal;
}
int Acco::inqui() {
	return bal;
}
string Acco::getOw() {
	return na;
}
int main() {
	Acco a("kitae", 1, 5000);
	a.depo(50000);
	cout << a.getOw() << "의 잔액은 " << a.inqui() << endl;
	int mon = a.withd(20000);
	cout << a.getOw() << "의 잔액은 " << a.inqui() << endl;
}

4. CoffeeMachine 클래스를 만들어보자. main() 함수와 실행 결과가 다음과 같도록 CoffeMachine 클래스를 작성하라. 에스프레소 한 잔에는 커피와 물이 각각 1씩 소비되고, 아메리카노의 경우 커피는 1, 물은 2가 소비되고, 설탕 커피는 커피 1, 물 2, 설탕 1이 소비된다. CoffeeMachine 클래스에는 어떤 멤버 변수와 어떤 멤버 함수가 필요한지 잘 판단하여 작성하라.

#include <iostream>
#include <string>
using namespace std;
class CoffMac {
	int coff;
	int wat;
	int sug;
public:
	CoffMac(int coff, int wat, int sug);
	void drinkEsp();
	void drinkAmer();
	void drinkSug();
	void show();
	void fill();
};
CoffMac::CoffMac(int coff, int wat, int sug) {
	this->coff = coff;
	this->wat = wat;
	this->sug = sug;
}
void CoffMac::drinkEsp() {
	coff = coff - 1;
	wat = wat - 1;
}
void CoffMac::drinkAmer() {
	coff = coff - 1;
	wat = wat - 2;
}
void CoffMac::drinkSug() {
	coff = coff - 1;
	wat = wat - 2;
	sug = sug - 1;
}
void CoffMac::show() {
	cout << "상태, 카피:" << coff << "\n워터:" << wat << "\n슈가:" << sug << endl;
}
void CoffMac::fill() {
	coff = 10;
	wat = 10;
	sug = 10;
}
int main() {
	CoffMac j(5, 10, 3);
	j.drinkEsp();
	j.show();
	j.drinkAmer();
	j.show();
	j.drinkSug();
	j.show();
	j.fill();
	j.show();
}

5.

5. 랜덤 수를 발생시키는 Random 클래스를 만들자. Random 클래스를 이용하여 랜덤한 정수를 10개 출력하는 사례는 다음과 같다. Random 클래스가 생성자, next(), nextInRange()의 3개의 멤버 함수를 가지도록 작성하고 main() 함수와 합쳐 하나의 cpp 파일에 구현하라.

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

class Rand {
	int Rand;
public:
	int Rand1();
	int Rand2(int fir, int sec);
};
int Rand::Rand1() {
	int nor = rand();
	return nor;
}
int Rand::Rand2(int fir, int sec) {
	int nor = Rand1() % (sec - fir + 1) + fir;
	return nor;
}
int main() {
	Rand r;
	cout << "-- 0dptj " << RAND_MAX << "까지의 랜덤 정수 10 개--" << endl;
	for (int i = 0; i < 10; i++) {
		int nor = r.Rand1();
		cout << nor << ' ';
	}
	cout << endl << endl << "-- 2에서 " << "4 까지의 랜덤 정수 10개--" << endl;
	for (int i = 0; i < 10; i++) {
		int nor = r.Rand2(2, 4);
		cout << nor << ' ';
	}
}

6. 문제 5번을 참고하여 짝수 정수만 랜덤하게 발생시키는 EvenRandom 클래스를 작성하고 EvenRandom 클래스를 이용하여 10개의 짝수를 랜덤하게 출력하는 프로그램을 완성하라. 0도 짝수로 처리한다.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Rand {
	int Rando = 0;
public:
	int Rand1();
	int Rand2(int fir, int sec);
};
int Rand::Rand1() {
	int nor;
	do {
		nor = rand();
	} while (nor % 2 == 1);
	return nor;
}
int Rand::Rand2(int fir, int sec) {
	int nor;
	do {
		nor = rand() % (sec - fir + 1) + fir;
	} while (nor % 2 == 1);
	return nor;
}

int main() {
	Rand r;
	cout << "-- 0에서 " << RAND_MAX << "까지의 정수 10개--" << endl;
	for (int i = 0; i < 10; i++) {
		int nor = r.Rand1();
		cout << nor << ' ';
	}
	cout << endl << endl << "-- 2에서 10까지의 랜덤 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++) {
		int nor = r.Rand2(2, 10);
		cout << nor << ' ';
	}
	cout << endl;
}

7. 문제 5번을 참고하여 생성자를 이용하여 짝수 홀수를 선택할 수 있도록 SelectableRandom 클래스를 작성하고 짝수 10개, 홀수 10개를 랜덤하게 발생시키는 프로그램을 작성하라.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class SelectableRandom {
	int Nor = 0;
public:
	int nE();
	int nO();
	int nERange(int fir, int sec);
	int nORange(int fir, int sec);
};
int SelectableRandom::nE() {
	int nNor;
	do {
		nNor = rand();
	} while (nNor % 2 != 0);
	return nNor;
}
int SelectableRandom::nO() {
	int nNor;
	do {
		nNor = rand();
	} while (nNor % 2 != 1);
	return nNor;
}
int SelectableRandom::nERange(int fir, int sec) {
	int nNor;
	do {
		nNor = rand() % (sec - fir + 1) + fir;
	} while (nNor % 2 != 0);
	return nNor;
}
int SelectableRandom::nORange(int fir, int sec) {
	int nNor;
	do {
		nNor = rand() % (sec - fir + 1) + fir;
	} while (nNor % 2 != 1);
	return nNor;
}
int main() {
	SelectableRandom r;
	cout << "-- 0에서 " << RAND_MAX << "까지의 짝수 랜덤 정수 10개--" << endl;
	for (int i = 0; i < 10; i++) {
		int nNor = r.nE();
		cout << nNor << ' ';
	}
	cout << endl << endl << "-- 2에서 9까지의 랜덤 홀수 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++) {
		int nNor = r.nORange(2, 9);
		cout << nNor << ' ';
	}
	cout << endl;
}

*8. int 타입의 정수를 객체화한 Integer 클래스를 작성하라. Integer의 모든 멤버 함수를 자동 인라인으로 작성하라. Integer 클래스를 활용하는 코드는 다음과 같다.

 

#include <iostream>
#include <string>
using namespace std;
class Integer {
public:
	int num;
	Integer(int num) {
		this->num = num;
	}
	Integer(string str) {
		this->num = stoi(str);
	}
	void set(int num) {
		this->num = num;
	}
	int get() {
		return num;
	}
	bool isEven() {
		if (num % 2 == 0) return true;
		else return false;
	}
};
int main() {
	Integer num(30);
	cout << num.get() << ' ';
	num.set(50);
	cout << num.get() << ' ';
	Integer m("300");
	cout << m.get() << ' ';
	cout << m.isEven();
}

9. Oval 클래스는 주어진 사각형에 내접하는 타원을 추상화한 클래스이다. Oval 클래스의 멤버는 모두 다음과 같다. Oval 클래스를 선언부와 구현부로 나누어 작성하라.

#include <iostream>
#include <string>
using namespace std;
class Oval {
private:
	int wid;
	int hei;
public:
	Oval() {
		wid = hei = 1;
	}
	Oval(int wid, int hei) {
		this->wid = wid;
		this->hei = hei;
	}
	~Oval() {
		cout << "소멸 : width = " << wid << ", height = " << hei;
	}
	void show() {
		cout << "width = " << wid << ", height = " << hei;
	}
	void set(int wid, int hei) {
		this->wid = wid;
		this->hei = hei;
	}
	int getWid() {
		return wid;
	}
	int getHei() {
		return hei;
	}
};
int main() {
	Oval x, y(3, 4);
	x.set(10, 20);
	x.show();
	cout << y.getWid() << "," << y.getHei();
}

10. (1) 클래스의 선언부와 구현부를 분리하고, 모든 코드를 Calculator.cpp 파일에 작성하라.

#include <iostream>
#include <string>
using namespace std;
class A {
	int x;
	int y;
public:
	void setValue(int x, int y) {
		x = x; y = y;
	}
	int calculate() {
		return x + y;
	}
};
class S {
	int x;
	int y;
public:
	void setValue(int x, int y) {
		x = x; y = y;
	}
	int calculate() {
		return x - y;
	}
};

class M {
	int x;
	int y;
public:
	void setValue(int x, int y) {
		x = x; y = y;
	}
	int calculate() {
		return x * y;
	}
};
class D {
	int x;
	int y;
public:
	void setValue(int x, int y) {
		x = x; y = y;
	}
	int calculate() {
		return x / y;
	}
};
int main() {
	A a;
	S s;
	M m;
	D d;
	while (true) {
		cout << "입력하세요>>";
		int x, y;
		char op;
		cin >> x >> y >> op;

		switch (op) {
		case '+':
			a.setValue(x, y);
			cout << a.calculate() << endl;
			break;
		case '-':
			s.setValue(x, y);
			cout << s.calculate() << endl;
			break;
		case '*':
			m.setValue(x, y);
			cout << m.calculate() << endl;
			break;
		case '/':
			d.setValue(x, y);
			cout << d.calculate() << endl;
			break;
		default:
			break;
		}
	}
}

     (2) 클래스의 선언부와 구현부를 헤더 파일과 cpp 파일로 나누어 프로그램을 작성하라.

#ifndef CALCULATOR_H
#define CALCULATOR_H
class A {
	int x, y;
public:
	void setValue(int x, int y);
	int calculate();
};
class S {
	int x, y;
public:
	void setValue(int x, int y);
	int calculate();
};
class M {
	int x, y;
public:
	void setValue(int x, int y);
	int calculate();
};
class D {
	int x, y;
public:
	void setValue(int x, int y);
	int calculate();
};
#endif

    

#include <iostream>
#include <string>
#include "calculator.h"
using namespace std;
void A::setValue(int x, int y) {
	this->x = x;
	this->y = y;
}
int A::calculate() {
	return x + y;
}
void S::setValue(int x, int y) {
	this->x = x;
	this->y = y;
}
int S::calculate() {
	return x - y;
}
void M::setValue(int x, int y) {
	this->x = x;
	this->y = y;
}
int M::calculate() {
	return x * y;
}
void D::setValue(int x, int y) {
	this->x = x;
	this->y = y;
}
int D::calculate() {
	return x / y;
}
int main() {
	A a;
	S s;
	M m;
	D d;
	while (true) {
		cout << "입력하세요>>";
		int x, y;
		char op;
		cin >> x >> y >> op;
		switch (op) {
		case '+':
			a.setValue(x, y);
			cout << a.calculate() << endl;
			break;
		case '-':
			s.setValue(x, y);
			cout << s.calculate() << endl;
			break;
		case '*':
			m.setValue(x, y);
			cout << m.calculate() << endl;
			break;
		case '/':
			d.setValue(x, y);
			cout << d.calculate() << endl;
			break;
		default:
			break;
		}
	}
}

 

11. 다음 코드에서 Box 클래스의 선언부와 구현부를 Box.h, Box.cpp 파일로 분리하고 main() 함수 부분을 main.cpp로 분리하여 전체 프로그램을 완성하라.

 

#ifndef BOX_H
#define BOX_H
class Box {
	int wid, hei;
	char fill;
public:
	Box(int w, int h);
	void setFill(char f);
	void setSize(int w, int h);
	void draw();
};
#endif

 

#include <iostream>
#include "Box.h"
using namespace std;
Box::Box(int w, int h) {
	setSize(w, h);
	fill = '*';
}
void Box::draw() {
	for (int i = 0; i < hei; i++) {
		for (int j = 0; j < wid; j++) {
			cout << fill;
		}
		cout << endl;
	}
}
void Box::setFill(char f) {
	fill = f;
}
void Box::setSize(int w, int h) {
	wid = w;
	hei = h;
}


 

#include "Box.h"
int main() {
	Box box(10, 2);
	box.draw();
	cout << endl;
	box.setSize(7, 4);
	box.setFill('^');
	box.draw();
}

 

12. 컴퓨터의 주기억장치를 모델링하는 클래스 Ram을 구현하려고 한다. Ram 클래스는 데이터가 기록될 메모리 공간과 크기 정보를 가지고, 주어진 주소에 데이터를 기록하고(write), 주어진 주소로부터 데이터를 읽어온다(read). Ram 클래스는 다음과 같이 선언된다. 실행결과를 참고하여 Ram.h, Ram.cpp, main.cpp로 헤더 파일과 cpp 파일을 분리하여 프로그램을 완성하라.

 

#ifndef RAM_H
#define RAM_H
class Ram {
	char memory[100 * 1024];
	int size;
public:
	Ram();
	~Ram();
	char re(int addre);
	void wri(int addre, char val);
};
#endif

 

#include <iostream>
#include <string>
#include "Ram.h"
using namespace std;
Ram::Ram() {
	for (int i = 0; i < 100 * 1024; i++) {
		memory[i] = 0;
	}
	size = 100 * 1024;
}
Ram::~Ram() {
	cout << "터미네이티드" << endl;
}
char Ram::re(int addre) {
	return memory[addre];
}
void Ram::wri(int addre, char val) {
	mem[addre] = val;
}

 

#include "Ram.h"
int main() {
	Ram ram;
	ram.wri(100, 20);
	ram.wri(101, 30);
	char res = ram.re(100) + ram.re(101);
	ram.write(102, res);
	cout << "값 = " << (int)ram.re(102) << endl;
}

 

반응형

댓글


Please Enable JavaScript!
Mohon Aktifkan Javascript![ Enable JavaScript ]