본문 바로가기
C++/명품 C++ Programming

[C++/명품 C++ Programming]명품 C++ 9장 연습문제 - 실습 문제

by starfish22 2022. 5. 20.
728x90

1.

#include <iostream>
#include <string>
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 {
    virtual double convert(double src) { return src / ratio; }
    virtual string getSourceString() { return "원"; }
    virtual string getDestString() { return "달러"; }
public:
    WonToDollar(int won) : Converter(won) {}
};

int main() {
    WonToDollar wd(1010);
    wd.run();
}

2.

#include <iostream>
#include <string>
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 {
    virtual double convert(double src) { return src / ratio; }
    virtual string getSourceString() { return "Km"; }
    virtual string getDestString() { return "Mile"; }
public:
    KmToMile(double km) : Converter(km) {}
};

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

3.

#include <iostream>
#include <string>
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 {
    virtual int calculate() {
        int sum = 0;
        for (int i = getX(); i <= getY(); i++) sum += i;
        return sum;
    }
public:
    ForLoopAdder(string name) : LoopAdder(name) {}
};

int main() {
    ForLoopAdder forLoop("For Loop");
    forLoop.run();
}

4.

#include <iostream>
#include <string>
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 WhileLoopAdder : public LoopAdder {
    virtual int calculate() {
        int i = getX(), sum = 0;
        while (i <= getY()) {
            sum += i;
            i++;
        }
        return sum;
    }
public:
    WhileLoopAdder(string name) : LoopAdder(name) {}
};

class DoWhileLoopAdder : public LoopAdder {
    virtual int calculate() {
        int i = getX(), sum = 0;
        do {
            sum += i;
            i++;
        } while (i <= getY());
        return sum;
    }
public:
    DoWhileLoopAdder(string name) : LoopAdder(name) {}
};

int main() {
    WhileLoopAdder whileLoop("While Loop");
    DoWhileLoopAdder doWhileLoop("Do whie Loop");

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

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:
    virtual bool operation() { return x & y; }
};

class ORGate : public AbstractGate {
public:
    virtual bool operation() { return x | y; }
};

class XORGate : public AbstractGate {
public:
    virtual 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.

#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;
    int top, maxSize;
public:
    IntStack(int n) {
        stack = new int[n];
        maxSize = n;
        top = 0;
    }
    ~IntStack() { delete[] stack; }
    virtual bool push(int n) {
        if (maxSize > top) {
            stack[top++] = n;
            return true;
        }
        return false;
    }
    virtual bool pop(int &n) {
        if (top > 0) {
            n = stack[--top];
            return true;
        }
        return false;
    }
    virtual int size() { return top; }
};

int main() {
    IntStack stack(5);

    for (int i = 1; i <= 10; i++) {
        if (stack.push(i)) cout << i << " push" << endl;
        else cout << "Full" << endl;
    }

    int n, size = stack.size();
    for (int i = 0; i < size; i++) {
        if (stack.pop(n)) cout << n << " pop" << endl;
        else cout << "Empty" << endl;
    }
}

7.

#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() { return 0; }
    string getName() { return name; }
};

class Oval : public Shape {
public:
    Oval(string n, int w, int h) : Shape(n, w, h) {}
    virtual double getArea() { return width * height * 3.14; }
};

class Rect : public Shape {
public:
    Rect(string n, int w, int h) : Shape(n, w, h) {}
    virtual double getArea() { return width * height; }
};

class Triangular : public Shape {
public:
    Triangular(string n, int w, int h) : Shape(n, w, h) {}
    virtual double getArea() { return width * height / 2; }
};

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.

#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 n, int w, int h) :Shape(n, w, h) {}
	virtual double getArea() { return width * height * 3.14; }
};

class Rect : public Shape {
public:
	Rect(string n, int w, int h) :Shape(n, w, h) {}
	virtual double getArea() { return width * height; }
};

class Triangular : public Shape {
public:
	Triangular(string n, int w, int h) :Shape(n, w, h) {}
	virtual double getArea() { return width * height / 2; }
};

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.

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

class Printer {
protected:
    string model;
    string manufacturer;
    int printedCount;
    int availableCount;
    Printer(string md, string mf, int paper) {
        model = md;
        manufacturer = mf;
        availableCount = paper;
    }
public:
    virtual bool print(int pages) = 0;
    virtual void show() = 0;
};

class InkJetPrinter : public Printer {
    int availableInk;
public:
    InkJetPrinter(string md, string mf, int paper, int ink) : Printer(md, mf, paper) { availableInk = ink; }
    virtual bool print(int pages) {
        if (availableCount < pages) {
            cout << "용지가 부족하여 ";
            return false;
        }
        else if (availableInk < pages) {
            cout << "잉크가 부족하여 ";
            return false;
        }
        availableCount -= pages;
        availableInk -= pages;
        printedCount = pages;
        return true;
    }
    virtual void show() {
        cout << model << ", " << manufacturer << ", 남은 종이 " << availableCount << "장, 남은 잉크 " << availableInk << endl;
    }
    int availableInkCount() { return availableInk; }
};

class LaserPrinter : public Printer {
    int availableToner;
public:
    LaserPrinter(string md, string mf, int paper, int toner) : Printer(md, mf, paper) { availableToner = toner; }
    virtual bool print(int pages) {
        if (availableCount < pages) {
            cout << "용지가 부족하여 ";
            return false;
        }
        else if (availableToner < pages / 2) {
            cout << "토너가 부족하여 ";
            return false;
        }
        availableCount -= pages;
        availableToner -= pages / 2;
        printedCount = pages;
        return true;
    }
    virtual void show() {
        cout << model << ", " << manufacturer << ", 남은 종이 " << availableCount << "장, 남은 토너 " << availableToner << endl;
    }
    int availableTonerCount() { return availableToner; }
};

int main() {
    Printer *printer[2];
    printer[0] = new InkJetPrinter("Officejet V40", "HP", 5, 10);
    printer[1] = new LaserPrinter("SCX-6x45", "삼성전자", 3, 20);

    cout << "현재 작동중인 2 대의 프린터는 아래와 같다" << endl;
    cout << "잉크젯 : ";
    printer[0]->show();
    cout << "레이저 : ";
    printer[1]->show();
    cout << endl;

    string y("y");
    int p, n;
    while (y == "y") {
        cout << "프린터(1:잉크젯, 2:레이저)와 매수 입력>>";
        cin >> p >> n;
        if (printer[p - 1]->print(n)) cout << "프린트하였습니다." << endl;
        else cout << "프린트할 수 없습니다." << endl;
        printer[0]->show();
        printer[1]->show();
        cout << "계속 프린트 하시겠습니까(y/n)>>";
        cin >> y;
        cout << endl;
    }

    delete printer[0];
    delete printer[1];
}

10.

Shape, Line, Rect, Circle 클래스는 예제와 동일

 

main.cpp

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

int main() {
    GraphicEditor *editor = new GraphicEditor();
    delete editor;
}

GraphicEditor.h

#pragma once
#include "Shape.h"

class GraphicEditor {
    Shape *pStart;
    Shape *pLast;
public:
    GraphicEditor();
    ~GraphicEditor();
    void run();
    void show();
    void insertShape(int n);
    void deleteShape(int n);
};

GraphicEitor.cpp

#include <iostream>
#include "GraphicEditor.h"
#include "UI.h"
#include "Shape.h"
#include "Line.h"
#include "Rect.h"
#include "Circle.h"
using namespace std;

GraphicEditor::GraphicEditor() {
    cout << "그래픽 에디터입니다." << endl;
    pStart = nullptr;
    pLast = nullptr;
    run();
}

GraphicEditor::~GraphicEditor() {
    Shape *p = pStart;
    Shape *q;
    while (p != nullptr) {
        q = p->getNext();
        delete p;
        p = q;
    }
}

void GraphicEditor::run() {
    int n;
    while (true) {
        n = UI::menuUI();
        switch (n) {
        case 1:
            insertShape(UI::insertUI());
            break;
        case 2:
            deleteShape(UI::deleteUI());
            break;
        case 3:
            show();
            break;
        default:
            return;
        }
    }
}

void GraphicEditor::show() {
    Shape *p = pStart;
    for (int i = 0; p != nullptr; i++) {
        cout << i << ": ";
        p->paint();
        p = p->getNext();
    }
}

void GraphicEditor::insertShape(int n) {
    Shape *p = nullptr;
    switch (n) {
    case 1:
        p = new Line();
        break;
    case 2:
        p = new Circle();
        break;
    case 3:
        p = new Rect();
        break;
    }

    if (!pStart) {
        pStart = p;
        pLast = pStart;
    }
    else {
        pLast = pLast->add(p);
    }
}

void GraphicEditor::deleteShape(int n) {
    Shape *p = pStart;
    if (n == 0) {
        pStart = p->getNext();
        delete p;
    }
    else {
        Shape *q = p;
        p = p->getNext();
        for (int i = 1; i <= n; i++) {
            if (i == n) {
                q->add(p->getNext());
                delete p;
                break;
            }
            q = p;
            p = p->getNext();
        }
    }
}

UI.h

#pragma once

class UI {
public:
    static int menuUI();
    static int insertUI();
    static int deleteUI();
};

UI.cpp

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

int UI::menuUI() {
    int n;
    cout << "삽입:1, 삭제:2, 모두보기:3, 종료:4 >> ";
    cin >> n;
    return n;
}

int UI::insertUI() {
    int n;
    cout << "선:1, 원:2, 사각형:3 >> ";
    cin >> n;
    return n;
}

int UI::deleteUI() {
    int n;
    cout << "삭제하고자 하는 도형의 인덱스 >> ";
    cin >> n;
    return n;
}
728x90

댓글