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

mian.cpp

#include <iostream>
#include "Tower.h"
using namespace std;

int main() {
	Tower myTower;
	Tower seoulTower(100);
	cout << "높이는 " << myTower.getHeight() << "미터" << endl;
	cout << "높이는 " << seoulTower.getHeight() << "미터 " << endl;
}

 

 

Tower.h

#ifndef TOWER_H
#define TOWER_H
class Tower {
	int height;
public:
	Tower();
	Tower(int n);
	int getHeight();
};
#endif

 

Tower.cpp

#include <iostream>
#include "Tower.h"
using namespace std;

int main() {
	Tower myTower;
	Tower seoulTower(100);
	cout << "높이는 " << myTower.getHeight() << "미터" << endl;
	cout << "높이는 " << seoulTower.getHeight() << "미터 " << endl;
}


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

main.cpp

#include <iostream>
#include "Date.h"
using namespace std;

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

 

Date.h

#ifndef DATE_H
#define DATE_H
#include <string>
using namespace std;

class Date {
	int year, month, day;
public:
	Date(int y, int m, int d);
	Date(string date); // string 때문에 include <string>이랑 using namespace std 사용
	int getYear();
	int getMonth();
	int getDay();
	void show();
};
#endif

 

Date.cpp

#include <iostream>
#include <string>
#include "Date.h"

using namespace std;

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

Date::Date(string date) {
	int index;
	
	year = stoi(date);
	
	index = date.find('/');
	month = stoi(date.substr(index + 1));

	index = date.find('/');
	day = stoi(date.substr(index + 1));
}

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

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

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

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

3.

main.cpp

#include <iostream>
#include "Account.h"
using namespace std;

int main() {
	Account a("kiate", 1, 5000);
	a.deposit(50000);
	cout << a.getOwner() << "의 잔액은 " << a.inquiry() << endl;
	int moeny = a.withdraw(20000);
	cout << a.getOwner() << "의 잔액은 " << a.inquiry() << endl;
}

 

Account.h

#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <string>
using namespace std;

class Account {
	string name;
	int id;
	int balance;
public:
	Account(string n, int i, int b);

	void deposit(int money);
	string getOwner();
	int withdraw(int money);
	int inquiry();
};
#endif

 

Account.cpp

#include <string>
#include "Account.h"

Account::Account(string n, int i, int b) {
	name = n;
	id = i;
	balance = b;
}

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

int Account::withdraw(int money) {
	if (balance >= money) {
		balance -= money;
		return money;
	}
	else {
		return 0;
	}
}

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

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

4.

main.cpp

#include <iostream>
#include "CoffeeMachine.h"
using namespace std;

int main() {
	CoffeeMachine java(5, 10, 3);
	java.drinkEspresso();
	java.show();
	java.drinkAmericano();
	java.show();
	java.drinkSugarCoffee();
	java.show();
	java.fill();
	java.show();
}

 

CoffeeMachine.h

#include <iostream>
#include "CoffeeMachine.h"
using namespace std;

int main() {
	CoffeeMachine java(int c, int w, int s);
	java.drinkEspresso();
	java.show();
	java.drinkAmericano();
	java.show();
	java.drinkSugarCoffee();
	java.show();
	java.fill();
	java.show();
}

 

CoffeeMacine.cpp

#include <iostream>
#include "CoffeeMachine.h"
using namespace std;

CoffeeMachine::CoffeeMachine(int c, int w, int s) {
	coffee = c;
	water = w;
	sugar = s;
}

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

void CoffeeMachine::drinkEspresso() {
	if (coffee < 1 || water < 1) {
		cout << "재료 부족" << endl;
		return;
	}
	coffee--;
	water--;
}
;
void CoffeeMachine::drinkAmericano() {
	if (coffee < 1 || water < 2) {
		cout << "재료 부족" << endl;
		return;
	}
	coffee--;
	water -= 2;
}
void CoffeeMachine::drinkSugarCoffee() {
	if (coffee < 1 || water < 2 || sugar < 1) {
		cout << "재료 부족" << endl;
		return;
	}
	coffee--;
	water -= 2;
	sugar--;
}
void CoffeeMachine::fill() {
	coffee = water = sugar = 10;
}

5.

 

main.cpp

#include <iostream>
#include "Random.h"
using namespace std;

int main(){
	Random r;
	cout << "-- 0에서 " << RAND_MAX << "까지의 랜덤 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++)
	{
		int n = r.next();
		cout << n << ' ';
	}
	cout << endl << endl << "-- 2에서 " << "4까지의 랜덤 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++)
	{
		int n = r.nextInRange(2, 4);
		cout << n << ' ';
	}
	cout << endl;
}

 

Random.h

#include <cstdlib>
#include <ctime>
#include "Random.h"

Random::Random() {
	srand((unsigned)time(NULL));
}

int Random::next() {
	return rand();
}

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

 

Random.cpp

#include <iostream>
#include "Random.h"
using namespace std;

int main(){
	Random r;
	cout << "-- 0에서 " << RAND_MAX << "까지의 랜덤 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++)
	{
		int n = r.next();
		cout << n << ' ';
	}
	cout << endl << endl << "-- 2에서 " << "4까지의 랜덤 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++)
	{
		int n = r.nextInRange(2, 4);
		cout << n << ' ';
	}
	cout << endl;
}

6. 

 

main.cpp

#include <iostream>
#include "EvenRandom.h"
using namespace std;

int main()
{
	EvenRandom r;
	cout << "-- 0에서 " << RAND_MAX << "까지의 랜덤 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++)
	{
		int n = r.next();
		cout << n << ' ';
	}
	cout << endl << endl << "-- 2에서 " << "4까지의 랜덤 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++)
	{
		int n = r.nextInRange(2, 10);
		cout << n << ' ';
	}
	cout << endl;
}

 

EvenRandom.h

#ifndef EVENRANDOM_H
#define EVENRANDOM_H
class EvenRandom {
public:
	EvenRandom();
	int next();
	int nextInRange(int a, int b);
};
#endif

 

EvenRandom.cpp

#include <iostream>
#include "EvenRandom.h"
using namespace std;

int main()
{
	EvenRandom r;
	cout << "-- 0에서 " << RAND_MAX << "까지의 랜덤 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++)
	{
		int n = r.next();
		cout << n << ' ';
	}
	cout << endl << endl << "-- 2에서 " << "4까지의 랜덤 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++)
	{
		int n = r.nextInRange(2, 10);
		cout << n << ' ';
	}
	cout << endl;
}

7.

 

main.cpp

#include <iostream>
#include "SelectableRandom.h"
using namespace std;

int main()
{
	SelectableRandom r;
	cout << "-- 0에서 " << RAND_MAX << "까지의 짝수 랜덤 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++)
	{
		int n = r.next();
		cout << n << ' ';
	}
	cout << endl << endl << "-- 2에서 " << "4까지의 랜덤 홀수 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++)
	{
		int n = r.nextInRange(2, 10);
		cout << n << ' ';
	}
	cout << endl;
}

 

SelectableRandom.h

#ifndef SELECTABLERANDOM_H
#define SELECTABLERANDOM_H
class SelectableRandom {
public:
	SelectableRandom();
	int next();
	int nextInRange(int a, int b);
};
#endif

 

SelectableRandom.cpp

#include <cstdlib>
#include <ctime>
#include "SelectableRandom.h"


SelectableRandom::SelectableRandom() {
	srand((unsigned)time(NULL));
}

int SelectableRandom::next() {
	int n;
	while (true) {
		n = rand();
		if (n % 2 == 0) return n;
	}
}

int SelectableRandom::nextInRange(int a, int b) {
	int n;
	while (true) {
		n =  a + rand() % (b - a + 1);
		if (n % 2 == 1) return n;
	}
}

8.  매개변수 타입이 다른 생성자 

 

main.cpp

#include <iostream>
#include "Integer.h"
using namespace std;

int main()
{
	Integer n(30);
	cout << n.get() << ' ';
	n.set(50);
	cout << n.get() << ' ';

	Integer m("300");
	cout << m.get() << ' ';
	cout << m.isEven();

	cout << endl;
}

 

Integer.h

#ifndef INTEGER_H
#define INTEGER_H
#include <string>
using namespace std;

class Integer {
	int integer;
public:
	Integer(int i) { integer = i; }
	Integer(string i) { integer = stoi(i); }

	int get() { return integer; }
	bool isEven() { return integer % 2 == 0; }
	void set(int i) { integer = i; }
};

#endif

9. 

main.cpp

#include <iostream>
#include "Oval.h"
using namespace std;

int main()
{
	Oval a, b(3, 4);
	a.set(10, 20);
	a.show();
	cout << b.getWidth() << "," << b.getHeight() << endl;
}

 

Oval.h

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

#endif

 

Oval.cpp

#include <iostream>
#include "Oval.h"
using namespace std;

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

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

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

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

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

void Oval::show() {
	cout << "타원의 너비:" << width << ", 타원의 높이: " << height << endl;
}

10.

main.cpp

#include <iostream>
#include "Cal.h"
using namespace std;

int main(){
	Add a;
	Sub s;
	Mul m;
	Div d;
	int num1, num2;
	char op;

	while (true) {
		cout << "두 정수와 연산자를 입력하세요>>";
		cin >> num1 >> num2 >> op;
		switch (op) {
		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;
		}
	}
}

 

Cal.h

#ifndef Cal_H
#define Cal_h
class Add {
	int a, b;
public:
	void setValue(int x, int y);
	int calculate();
};

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

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

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

#endif

 

Cal.cpp

#include "Cal.h"

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

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

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

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


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

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

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

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

11.

main.cpp

#include <iostream>
#include "Box.h"
using namespace std;

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

 

Box.h

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

#endif

 

Box.cpp

#include <iostream>;
#include "Box.h"
using namespace std;

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

 

12.

main.cpp

#include <iostream>
#include "Ram.h"
using namespace std;

int main(){
	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;
}

 

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>
#include "Ram.h"
using namespace std;

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

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

void Ram::write(int address, char value) {
	mem[address] = value;
}

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

+ Recent posts