Jam's story

명품 C++ programming 9장 실습 문제 본문

2021-2학기/C++

명품 C++ programming 9장 실습 문제

애플쩀 2021. 11. 22. 21:19

 

#include <iostream>

using namespace std;

class Converter {
protected:
	double ratio;
	virtual double convert(double src) = 0;
	virtual string getSourceString() = 0;
	virtual string getDestString() = 0;
public:
	Converter(double ratio) { this->ratio = ratio; }
	void run() {
		double src;
		cout << getSourceString() << "을 " << getDestString() << "로 바꿉니다. ";
		cout << getSourceString() << "을 입력하세요>> ";
		cin >> src;
		cout << "변환 결과: " << convert(src) << getDestString() << endl;
	}
};
class WonToDollar : public Converter {
public:
	WonToDollar(double ratio=0.0) : Converter(ratio) {}
	double convert(double src) { return src / ratio; }
	 string getSourceString() { return "원"; }
	 string getDestString() { return "달러"; }
};
int main() {
	WonToDollar wd(1010);
	wd.run();

	return 0;
}

 

1-2

 

#include <iostream>

using namespace std;

class Converter {
protected:
	double ratio;
	virtual double convert(double src) = 0;
	virtual string getSourceString() = 0;
	virtual string getDestString() = 0;
public:
	Converter(double ratio) { this->ratio = ratio; }
	void run() {
		double src;
		cout << getSourceString() << "을 " << getDestString() << "로 바꿉니다. ";
		cout << getSourceString() << "을 입력하세요>> ";
		cin >> src;
		cout << "변환 결과: " << convert(src) << getDestString() << endl;
	}
};

class KmToMile : public Converter{
public:
	KmToMile(double ratio=0.0) : Converter(ratio) { }
	double convert(double src) { return src / ratio; }
	string getSourceString() { return "Km"; }
	string getDestString() { return "Mile"; }
};

int main() {
	KmToMile toMile(1.609344);
	toMile.run();

	return 0;
}

3

#include <iostream>

using namespace std;

class LoopAdder {
	string name;
	int x, y, sum;
	void read();
	void write();
protected:
	LoopAdder(string name = "") { this->name = name; }
	int getX() { return x; }
	int getY() { return y; }
	virtual int calculate() = 0;
public:
	void run();
};

void LoopAdder::read() {
	cout << name << ":" << endl;
	cout << "처음 수에서 두번째 수까지 더합니다. 두 수를 입력하세요 >> ";
	cin >> x >> y;
}
void LoopAdder::write() {
	cout << x << "에서 " << y << "까지의 합 = " << sum << "입니다." << endl;
}
void LoopAdder::run() {
	read();
	sum = calculate();
	write();
}

class ForLoopAdder : public LoopAdder {
public:
	ForLoopAdder(string name = " ") : LoopAdder(name) {}
	int calculate() {
		int sum = 0;
		for (int i = getX(); i <= getY(); i++) {
			sum += i;
		}
		return sum;
	}
};

class WhileLoopAdder : public LoopAdder{
public:
	WhileLoopAdder(string name = " ") :LoopAdder(name) {
		cout << name << ":";
	}
	int calculate() {
		int sum = 0;
		int x = getX();
		while (x <= getY()) {
			
			sum += x;
			x++;
		}
		return sum;
	}
};

class DoWhileLoopAdder : public LoopAdder {
public:
	DoWhileLoopAdder(string name = " ") :LoopAdder(name) {
		cout << name << ":";
	}
	int calculate() {
		int sum = 0;
		int x = getX();
		do {
			sum += x;
			x++;
		} while (x <= getY());

		return sum;
	}
};
int main() {
	WhileLoopAdder whileLoop("while Loop");
	DoWhileLoopAdder doWhileLoop("Do while Loop");

	whileLoop.run();
	doWhileLoop.run();

	return 0;
}

 

4

#include <iostream>

using namespace std;

class LoopAdder {
	string name;
	int x, y, sum;
	void read();
	void write();
protected:
	LoopAdder(string name = "") { this->name = name; }
	int getX() { return x; }
	int getY() { return y; }
	virtual int calculate() = 0;
public:
	void run();
};

void LoopAdder::read() {
	cout << name << ":" << endl;
	cout << "처음 수에서 두번째 수까지 더합니다. 두 수를 입력하세요 >> ";
	cin >> x >> y;
}
void LoopAdder::write() {
	cout << x << "에서 " << y << "까지의 합 = " << sum << "입니다." << endl;
}
void LoopAdder::run() {
	read();
	sum = calculate();
	write();
}

class ForLoopAdder : public LoopAdder {
public:
	ForLoopAdder(string name = " ") : LoopAdder(name) {}
	int calculate() {
		int sum = 0;
		for (int i = getX(); i <= getY(); i++) {
			sum += i;
		}
		return sum;
	}
};

class WhileLoopAdder : public LoopAdder{
public:
	WhileLoopAdder(string name = " ") :LoopAdder(name) {
		cout << name << ":";
	}
	int calculate() {
		int sum = 0;
		int x = getX();
		while (x <= getY()) {
			
			sum += x;
			x++;
		}
		return sum;
	}
};

class DoWhileLoopAdder : public LoopAdder {
public:
	DoWhileLoopAdder(string name = " ") :LoopAdder(name) {
		cout << name << ":";
	}
	int calculate() {
		int sum = 0;
		int x = getX();
		do {
			sum += x;
			x++;
		} while (x <= getY());

		return sum;
	}
};
int main() {
	WhileLoopAdder whileLoop("while Loop");
	DoWhileLoopAdder doWhileLoop("Do while Loop");

	whileLoop.run();
	doWhileLoop.run();

	return 0;
}

 

 

5

#include <iostream>

using namespace std;

class AbstractGate {
protected:
	bool x, y;
public:
	void set(bool x, bool y) { this->x = x; this->y = y; }
	virtual bool operation() = 0;
};

class ANDGate : public AbstractGate {
public:
	bool operation() {
		return  x & y;
	}
};
class ORGate : public AbstractGate {
public:
	bool operation() {
		return  x | y;
	}
};
class XORGate : public AbstractGate {
public:
	bool operation() {
		return  x ^ y;
	}
};
int main() {
	ANDGate andGate;
	ORGate orGate;
	XORGate xorGate;

	andGate.set(true, false);
	orGate.set(true, false);
	xorGate.set(true, false);

	cout.setf(ios::boolalpha);
	cout << andGate.operation() << endl;
	cout << orGate.operation() << endl;
	cout << xorGate.operation() << endl;

	return 0;
}

6

#include <iostream>

using namespace std;

class AbstractStack {
public:
	virtual bool push(int n) = 0;
	virtual bool pop(int& n) = 0;
	virtual int size() = 0;
};
class IntStack : public AbstractStack {
	int stack[10] = { 0 };
	int top = -1;
public:
	bool push(int n) {
		if (size() + 1 >= 10) {
			cout << "stack 이 꽉찼어요 "<<endl;
			return false;
		}
		stack[++top]=n;
		return true;
	}
	bool pop(int& n) {
		if (size() < 0) {
			cout << "stack이 비었음" << endl;
			return false;
		}
		n = stack[top--];
		return true;
	}
	int size() {
		return top;
	}
	void show() {
		cout << "{";
		for (int i = 0; i <= top; i++) {
			cout << stack[i] << ' ';
	}
		cout << "}" << endl;
	}
};

int main() {
	IntStack intStack;

	intStack.push(1);
	intStack.push(2);
	intStack.push(3);
	intStack.push(4);
	intStack.push(5);
	intStack.push(6);

	intStack.show();

	int n;
	intStack.pop(n);
	cout << n << " is popped" << endl;
	intStack.show();

	return 0;
}

7

#include <iostream>
using namespace std;

class Shape {
protected:
	string name;
	int width, height;
public:
	Shape(string n = "", int w = 0, int h = 0) { name = n; width = w; height = h; }
	virtual double getArea() { return 0; }
	string getname() { return name; }
};

class Oval : public Shape {
public:
	Oval(string name, int width, int height) : Shape(name, width, height) {}
	double getArea() { return 3.14 * width * height; }
};
class Rect : public Shape {
public:
	Rect(string name, int width, int height) : Shape(name, width, height) {}
	double getArea() { return width * height; }
};
class Triangular : public Shape {
public:
	Triangular(string name, int width, int height) : Shape(name, width, height) {}
	double getArea() { return 0.5* width * height; }
};
int main() {
	Shape* p[3];
	p[0] = new Oval("빈대떡", 10, 20);
	p[1] = new Rect("찰떡", 30, 40);
	p[2] = new Triangular("토스트", 30, 40);

	for (int i = 0; i < 3; i++) {
		cout << p[i]->getname() << " 넓이는 " << p[i]->getArea() << endl;
	}

	for (int i = 0; i < 3; i++) delete p[i];
	return 0;
}

8

#include <iostream>
using namespace std;

class Shape {
protected:
	string name;
	int width, height;
public:
	Shape(string n = "", int w = 0, int h = 0) { name = n; width = w; height = h; }
	virtual double getArea() = 0;
	string getname() { return name; }
};

class Oval : public Shape {
public:
	Oval(string name, int width, int height) : Shape(name, width, height) {}
	double getArea() { return 3.14 * width * height; }
};
class Rect : public Shape {
public:
	Rect(string name, int width, int height) : Shape(name, width, height) {}
	double getArea() { return width * height; }
};
class Triangular : public Shape {
public:
	Triangular(string name, int width, int height) : Shape(name, width, height) {}
	double getArea() { return 0.5* width * height; }
};
int main() {
	Shape* p[3];
	p[0] = new Oval("빈대떡", 10, 20);
	p[1] = new Rect("찰떡", 30, 40);
	p[2] = new Triangular("토스트", 30, 40);

	for (int i = 0; i < 3; i++) {
		cout << p[i]->getname() << " 넓이는 " << p[i]->getArea() << endl;
	}

	for (int i = 0; i < 3; i++) delete p[i];
	return 0;
}

9

#include <iostream>
using namespace std;

class Printer {
	string model; //모델
	string manufacturer; //제조업체 
	int printedCount, availableCount; //인쇄매수, 인소9ㅐ종이장ㄴ량 
protected:
	Printer(string m = " ", string ma = "", int a = 0) {
		this->model = m;
		this->manufacturer = ma;
		this->availableCount = a;
		this->printedCount = 0;
	}

	//getter, setter 
	string getModel() { return model; }
	string getManufacturer() { return manufacturer; }
	int getPrintedCount() { return printedCount; }
	int getAvailableCount() { return availableCount; }
	void setModel(string model) { this->model = model; }
	void setManufacturer(string manufacturer) { this->manufacturer = manufacturer; }
	void setPrintedCount(int printedCount) { this->printedCount = printedCount; }
	void setAvailableCount(int availableCount) { this->availableCount = availableCount; }

	bool isValidPrint(int pages) {
		if (availableCount >= pages) return true;
		else return false;
	}
	virtual void print(int pages) = 0;
	virtual void show() = 0;
};
class InkJetPrinter : public Printer {
	int availableInk;
public:
	InkJetPrinter(string m = " ", string ma = "", int a = 0, int i = 0) : Printer(m, ma, a) {
		availableInk = i;
	}
	bool isValidInkJetPrint(int pages) {
		if (availableInk>= pages) return true;
		else return false;
	}
	void print(int pages) {
		if (isValidPrint(pages)) {
			if (isValidInkJetPrint(pages)) {
				setPrintedCount(getPrintedCount() + pages);
				setAvailableCount(getAvailableCount() - pages);
				setAvailableInk(getAvailableInk() - pages);
				cout << "프린트하였습니다" << endl;
			}
			else {
				cout << "잉크가 부족하여 프린트할 수 없습니다." << endl;
			}
		}
		else cout << "용지가 부족하여 프린트할 수 없습니다." << endl;
	}
	void show() {
		cout << getModel() << ", " << getManufacturer() << ", 남은 종이" << getAvailableCount() << "남은 토너" << availableInk << endl;

	}
	int getAvailableInk() { return availableInk; }
	void setAvailableInk(int availableInk){
				this->availableInk = availableInk;}
};

class LaserPrinter : public Printer {
	int availableToner;
public :
	LaserPrinter(string m = "", string mo = "", int a = 0, int t = 0) : Printer(m, mo, a) {
		availableToner = t;
	}
	bool isValidPrint(int pages) {
		if (availableToner > pages) {
			return true;
		}
		else return false;
	}
	bool isValidToner(int pages) {
		if (availableToner >= pages) {
			return true;
		}
		else { return false; }
	}
	void print(int pages) {
		if (isValidPrint(pages)) {
			if (isValidToner(pages)) {
				setPrintedCount(getPrintedCount() + pages);
				setAvailableCount(getAvailableCount() - pages);
				cout << "프린트하였습니다." << endl;
			}
			else {
				cout << "잉크가 부족해요" << endl;
			}
		}
		else {
			cout << "용지가 부족해요" << endl;
		}
	}
	void show() {
		cout << getModel() << ", " << getManufacturer() << ", 남은 종이" << getAvailableCount() << "남은 토너" << availableToner << endl;
		
	}
};


int main() {

	InkJetPrinter* ink = new InkJetPrinter("officejet v40", "HP", 5, 10);
	LaserPrinter *laser = new LaserPrinter("SCX-6x45", "삼성전자", 3, 20);

	cout << "현재 작동중인 2대의 프린터는 아래와 같다" << endl;
	cout << "잉크젯";
	ink->show();
	cout << "레이저";
	laser->show();
	
	int printer, paper;
	char ch;
	while (true) {
		cout << endl << "프린터(1:잉크젯, 2:레이저)와 매수 입력>>";
		cin >> printer >> paper;
		switch (printer) {
		case 1:
			ink->print(paper); break;
		case 2:
			laser->print(paper); break;
		default:
			break;
		}
		ink->show();
		laser->show();

		cout << "계속 프린트할거야?" << endl;
		cin >> ch;
		if (ch == 'n')break;
		else continue;
	}
	return 0;
}

 

'2021-2학기 > C++' 카테고리의 다른 글

명품 c++ programming 실습 문제 10장  (0) 2021.11.30
C++ programming 8장 실습문제  (0) 2021.11.18
명품 c++programming 7장 실습문제  (0) 2021.11.13
프렌드와 연산자 중복  (0) 2021.11.12
4장  (0) 2021.10.26
Comments