다라다라V
article thumbnail
728x90
반응형
본 글은 "황기태"님의 [명품 C++ Programming]의 연습 문제 답을 공유하고자 작성되었으며, 필자가 직접 문제를 풀며 작성한 것이기에 오류가 있을 수 있습니다. 댓글로 알려주시면 반영하도록 하겠습니다.

시리즈 보기
[C++] 명품 C++ Programming 1장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 1장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 2장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 2장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 3장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 4장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 4장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 5장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 6장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 7장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 8장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 8장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 9장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 9장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 10장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 10장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 11장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 11장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 12장 연습 문제 풀이 (이론 문제)
[C++] 명품 C++ Programming 12장 연습 문제 풀이 (실습 문제)
[C++] 명품 C++ Programming 13장 연습 문제 풀이 (이론 문제)

 

9.1 - 9.2

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

 

1. 💡 

 

<java />
int main() { WonToDollor wd(1010); wd.run(); }
<java />
class WonToDollor : public Converter { public: WonToDollor(int ratio) : Converter(ratio) {} double convert(double src) { return src / ratio; }; string getSourceString() { return "원"; } string getDestString() { return "달러"; } };

 

2. 💡 

<java />
int main() { KmToMile toMile(1.609344); toMile.run(); }
<java />
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"; } };

 

 

 

9.3 - 9.4

<java />
class LoopAdder { string name; // 루프 이름 int x, y, sum; // x에서 y까지의 합은 sum void read(); // x, y 값을 읽어 들이는 함수 void write(); // sum을 출력하는 함수 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(); // x, y를 읽는다. sum = calculate(); // 루프를 돌면서 계산한다. write(); // 결과 sum을 출력한다. }

 

 

3. 💡 

<java />
#include <iostream> #include <string> using namespace std; class LoopAdder { string name; // 루프 이름 int x, y, sum; // x에서 y까지의 합은 sum void read(); // x, y 값을 읽어 들이는 함수 void write(); // sum을 출력하는 함수 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(); // x, y를 읽는다. sum = calculate(); // 루프를 돌면서 계산한다. write(); // 결과 sum을 출력한다. } class ForLoopAdder : public LoopAdder { int sum = 0; public: ForLoopAdder(string name) : LoopAdder(name) {} int calculate() { for (int i = getX(); i <= getY(); i++) { sum += i; } return sum; } }; int main() { ForLoopAdder forLoop("For Loop"); forLoop.run(); }

 

4. 💡 

<java />
#include <iostream> #include <string> using namespace std; class LoopAdder { string name; // 루프 이름 int x, y, sum; // x에서 y까지의 합은 sum void read(); // x, y 값을 읽어 들이는 함수 void write(); // sum을 출력하는 함수 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(); // x, y를 읽는다. sum = calculate(); // 루프를 돌면서 계산한다. write(); // 결과 sum을 출력한다. } class WhileLoopAdder :public LoopAdder { int sum = 0; public: WhileLoopAdder(string name) : LoopAdder(name) {} int calculate() { int plus = getX(); while (plus <= getY()) { sum += plus; plus++; } return sum; } }; class DoWhileLoopAdder : public LoopAdder { int sum = 0; public: DoWhileLoopAdder(string name) : LoopAdder(name) {} int calculate() { int ret = 0; int plus = getX(); do { ret += plus; plus++; } while (plus <= getY()); return ret; } }; int main() { WhileLoopAdder whileLoop("while Loop"); DoWhileLoopAdder doWhileLoop("Do while Loop"); whileLoop.run(); doWhileLoop.run(); }

 

5. 💡 

<java />
class AbstractGate { protected: bool x, y; public: void set(bool x, bool y) { this->x = x; this->y = y; } virtual bool operation() = 0; }; 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; }
<java />
#include <iostream> #include <string> 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; }

 

6. 💡 

<java />
#include <iostream> #include <string> using namespace std; class AbstractStack { public: virtual bool push(int n) = 0; // 스택에 n을 푸시 virtual bool pop(int& n) = 0; // 스택에서 팝한 정수를 n에 저장 virtual int size() = 0; // 스택에 저장된 정수의 개수 리턴 }; class IntStack : public AbstractStack { int stack[5] = { 0 }; int top = -1; public: bool push(int n) { if (size() + 1 >= 5) { cout << "스택이 가득 찼습니다." << endl; return false; } stack[++top] = n; return true; } bool pop(int& n) { if (size() < 0) { cout << "스택이 비어있습니다." << 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; for (int i = 0; i < 5; i++) { intStack.push(i * 10); } intStack.show(); int n; intStack.pop(n); cout << n << " 이 나왔습니다." << endl; intStack.show(); }

 

9.7 - 9.8

<java />
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; } // dummy 값 리턴 string getname() { return name; } };

 

7. 💡 

 

<java />
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]; }

 

8. 💡 

<java />
#include <iostream> #include <string> 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 w, int h) : Shape(name, w, h) {} double getArea() { return 3.14 * width * height; } }; class Rect : public Shape { public: Rect(string name, int w, int h) : Shape(name, w, h) {} double getArea() { return width * height; } }; class Triangular : public Shape { public: Triangular(string name, int w, int h) : Shape(name, w, h) {} 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]; }

 

9. 💡 

<java />
#include <iostream> #include <string> using namespace std; class Printer { string model; string manufacturer; int printedCount; int availableCount; protected: Printer(string model, string manufacturer, int availableCount) { this->model = model; this->manufacturer = manufacturer; this->availableCount = availableCount; } bool canPrint(int pages) { if (availableCount - pages >= 0) return true; else return false; } virtual void print(int pages) = 0; virtual void show() = 0; // print와 show 함수가 virtual이 되면서 // Printer 클래스의 private 멤버 변수 접근을 위한 함수들이 필요함 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; } }; class InkJetPrinter : public Printer { int availableInk; public: InkJetPrinter(string model, string manufacturer, int availableCount, int availableInk) : Printer(model, manufacturer, availableCount) { this->availableInk = availableInk; } int getAvailableInk() { return availableInk; } void setAvailableInk(int availableInk) { this->availableInk = availableInk; } bool canInkPrint(int pages) { if (availableInk - pages >= 0) return true; else return false; } void print(int pages) { if (canPrint(pages)) { if (canInkPrint(pages)) { setPrintedCount(getPrintedCount() + pages); setAvailableCount(getAvailableCount() - pages); setAvailableInk(getAvailableInk() - pages); cout << "프린트하였습니다" << endl; } else { cout << "잉크가 부족하여 프린트할 수 없습니다." << endl; } } else cout << "용지가 부족하여 프린트할 수 없습니다." << endl; } void show() { cout << getModel() << ", " << getManufacturer() << ", 남은 종이 " << getAvailableCount() << "장, 남은 잉크 " << getAvailableInk() << endl; } }; class LaserPrinter : public Printer { int availableToner; public: LaserPrinter(string model, string manufacturer, int availableCount, int availableToner) : Printer(model, manufacturer, availableCount) { this->availableToner = availableToner; } int getAvailableToner() { return availableToner; } void setAvailableToner(int availableToner) { this->availableToner = availableToner; } bool canLaserPrint(int pages) { if (availableToner - pages >= 0) return true; else return false; } void print(int pages) { if (canPrint(pages)) { if (canLaserPrint(pages)) { setPrintedCount(getPrintedCount() + pages); setAvailableCount(getAvailableCount() - pages); setAvailableToner(getAvailableToner() - 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 type, pages; char cont; while (true) { cout << endl << "프린터(1:잉크젯, 2:레이저)와 매수 입력>>"; cin >> type >> pages; switch (type) { case 1: ink->print(pages); break; case 2: laser->print(pages); break; default: break; } ink->show(); laser->show(); cout << "게속 프린트 하시겠습니까(y/n)>>"; cin >> cont; if (cont == 'n') break; else continue; } return 0; }

 

10. 💡 

// 잠시 미룸

 

 

 

 

728x90
반응형
profile

다라다라V

@DaraDaraV

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!