박민영

명품 C++ 프로그래밍 3장 연습문제

Created March 8, 2022

3-1

#include <iostream>
using namespace std;

class Tower
{
public:
	Tower();
	Tower(int input);
	int getHeight();
private:
	int meter;
};

int main()
{
	Tower myTower;
	Tower seoulTower(100);
	cout << "높이는 " << myTower.getHeight() << "미터" << endl;
	cout << "높이는 " << seoulTower.getHeight() << "미터" << endl;
}
Tower::Tower()
{
	meter = 1;
}
Tower::Tower(int input)
{
	meter = input;
}
int Tower::getHeight()
{
	return meter;
}

3-2

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

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

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

Date::Date(int year, int month, int day)
{
	y = year;
	m = month;
	d = day;
}
Date::Date(string s)
{
	int ind;
	y = stoi(s);
	ind = s.find('/');
	m = stoi(s.substr(ind + 1));

	ind = s.find('/', ind + 1);
	d = stoi(s.substr(ind + 1));

}
int Date::getYear()
{
	return y;
}
int Date::getMonth()
{
	return m;
}
int Date::getDay()
{
	return d;
}
void Date::show()
{
	cout << y << "년" << m << "월" << d << "일" << endl;
}

3-3

#include <iostream>
using namespace std;

class Account
{
public:
	Account(string name, int id, int balance);
	void deposit(int input);
	string getOwner();
	int inquiry();
	int withdraw(int withdrawal);
private:
	int total=0;
	string n;
	int output;
};

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

Account::Account(string name, int id, int balance)
{
	n = name;
	total += balance;
}

void Account::deposit(int input)
{
	total+= input;
}

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

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

int Account::withdraw(int withdrawal)
{
	output=withdrawal;
	total -= output;
	return output;
}

3-4

#include <iostream>
using namespace std;

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

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

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

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

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

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

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

3-5

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

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

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::Random()


int Random::next()
{
	srand((unsigned)time(0));
	int n = rand();
	return n;
}

int Random::nextInRange(int start, int end)
{
	srand((unsigned)time(0));
	int n = rand() % (end - start + 1) + start;
	return n;
}

3-6

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

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

int EvenRandom::next()
{
	srand((unsigned)time(0));
	int n;
	do
	{
		n = rand();
	} while (n % 2 == 1);
	return n;
}


int EvenRandom::nextInRange(int start, int end)
{
	srand((unsigned)time(0));
	int n;
	do
	{
		n = rand() % (end - start + 1) + start;
	} while (n % 2 == 1);
	return n;
}

int main()
{
	EvenRandom r;
	cout << "--에서 " << 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;
	return 0;
}

3-7

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class SelectableRandom {
	int seed = 0;
public:
	int nextEven();
	int nextOdd();
	int nextEvenInRange(int start, int end);
	int nextOddInRange(int start, int end);
};

int SelectableRandom::nextEven() {
	srand((unsigned int)time(0));
	int n;
	do {
		n = rand();
	} while (n % 2 == 1);
	return n;
}

int SelectableRandom::nextOdd() {
	srand((unsigned int)time(0));
	int n;
	do {
		n = rand();
	} while (n % 2 == 0);
	return n;
}

int SelectableRandom::nextEvenInRange(int start, int end) {
	srand((unsigned int)time(0));
	int n;
	do {
		n = rand() % (end - start + 1) + start;
	} while (n % 2 == 1);
	return n;
}

int SelectableRandom::nextOddInRange(int start, int end) {
	srand((unsigned int)time(0));
	int n;
	do {
		n = rand() % (end - start + 1) + start;
	} while (n % 2 == 0);
	return n;
}
int main() {
	SelectableRandom r;
	cout << "-- 0에서 " << RAND_MAX << "까지의 정수 10개--" << endl;
	for (int i = 0; i < 10; i++) {
		int n = r.nextEven();
		cout << n << ' ';
	}
	cout << endl << endl << "-- 2에서 9까지의 랜덤 홀수 정수 10개 --" << endl;
	for (int i = 0; i < 10; i++) {
		int n = r.nextOddInRange(2, 9);
		cout << n << ' ';
	}
	cout << endl;
	return 0;
}

3-8

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

class Integer
{
public:
	int r;
	Integer(int num) { r = num; };
	int get() { return r; };
	void set(int num) { r = num; };
	Integer(string s) { r = stoi(s); };
	bool isEven() {
		if (r % 2 == 0)
			return true;
		else
			return false;
	};
};

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

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

	return 0;
}

3-9

#include <iostream>
using namespace std;

class Oval
{
	int width, height;
public:
	Oval(int a, int b) { width = a; height = b; };
	Oval() { width = 1; height = 1; };
	~Oval() { cout <<"Oval 소멸 : width = "<< width << ", height = "<< height << endl; }
	int getWidth() { return width; };
	int getHeight() { return height; };
	void set(int w, int h) { width = w; height = h; };
	void show() { cout << "width: " << width << "," << "height: " << height; };
};

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

3-10 (1)main

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

int main()
{
	Add a;
	Sub s;
	Mul m;
	Div d;



	while (true)
	{
		cout << "두 정수와 연산자를 입력하세요>>";
		int x, y;
		char op;
		cin >> x >> y >> op;
		if (op == '*')
			cout << m.calculate() << endl;
		else if (op == '/')
			cout << d.calculate() << endl;
		else if (op == '+')
			cout << a.calculate() << endl;
		else if (op == '-')
			cout << s.calculate() << endl;
		else
		{
		}

	}

	return 0;
}

(2)

#pragma once
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <iostream>
using namespace std;

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

(3)

#include <iostream>
using namespace std;

#include "Calculate.h"



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

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

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

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

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

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

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

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

3-11 (1)

#include <iostream>
using namespace std;

#include "Calculate.h"



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

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

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

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

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

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

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

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

(2)

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

(3)

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

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

3-12 (1)

#pragma once
#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

(2)

#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 << "메모리 제거됨\n";
}

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

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

(3)

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

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