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

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

by starfish22 2022. 2. 8.
728x90

1.

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

class Circle {
protected:
    int radius;
public:
    Circle(int radius = 0) { this->radius = radius; }
    int getRadius() { return radius; }
    void setRadius(int radius) { this->radius = radius; }
    double getArea() { return 3.14 * radius * radius; }
};

class NamedCircle : public Circle {
    string name;
public:
    NamedCircle(int radius, string name) : Circle(radius) { this->name = name; }
    void show() {
        cout << "반지름이 " << radius << "인 " << name << endl;
    }
};

int main() {
    NamedCircle waffle(3, "waffle");
    waffle.show();
}

2.

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

class Circle {
protected:
    int radius;
public:
    Circle(int radius = 0) { this->radius = radius; }
    int getRadius() { return radius; }
    void setRadius(int radius) { this->radius = radius; }
    double getArea() { return 3.14 * radius * radius; }
};

class NamedCircle : public Circle {
    string name;
public:
    NamedCircle(int radius = 0, string name = "") : Circle(radius) { this->name = name; }
    string getName() { return name; }
    void setName(string name) { this->name = name; }
};

int main() {
    NamedCircle pizza[5];
    cout << "5 개의 정수 반지름과 원의 이름을 입력하세요" << endl;
    int r;
    string name;
    for (int i = 0; i < 5; i++) {
        cout << i + 1 << ">> ";
        cin >> r >> name;
        pizza[i].setRadius(r);
        pizza[i].setName(name);
    }

    int big = 0;
    for (int i = 1; i < 5; i++) {
        if (pizza[big].getArea() < pizza[i].getArea()) big = i;
    }
    cout << "가장 면적이 큰 피자는 " << pizza[big].getName() << "입니다" << endl;
}

3.

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

class Point {
    int x, y;
public:
    Point(int x, int y) { this->x = x; this->y = y; }
    int getX() { return x; }
    int getY() { return y; }
protected:
    void move(int x, int y) { this->x = x; this->y = y; }
};

class ColorPoint : public Point {
    string color;
public:
    ColorPoint(int x, int y, string color) : Point(x, y) { this->color = color; }
    void setPoint(int x, int y) { move(x, y); }
    void setColor(string color) { this->color = color; }
    void show() {
        cout << color << "색으로 (" << getX() << ',' << getY() << ")에 위치한 점입니다." << endl;
    }
};

int main() {
    ColorPoint cp(5, 5, "RED");
    cp.setPoint(10, 20);
    cp.setColor("BLUE");
    cp.show();
}

4.

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

class Point {
    int x, y;
public:
    Point(int x, int y) { this->x = x; this->y = y; }
    int getX() { return x; }
    int getY() { return y; }
protected:
    void move(int x, int y) { this->x = x; this->y = y; }
};

class ColorPoint : public Point {
    string color;
public:
    ColorPoint(int x = 0, int y = 0, string color = "BLACK") : Point(x, y) { this->color = color; }
    void setPoint(int x, int y) { move(x, y); }
    void setColor(string color) { this->color = color; }
    void show() {
        cout << color << "색으로 (" << getX() << ',' << getY() << ")에 위치한 점입니다." << endl;
    }
};

int main() {
    ColorPoint zeroPoint;
    zeroPoint.show();

    ColorPoint cp(5, 5);
    cp.setPoint(10, 20);
    cp.setColor("BLUE");
    cp.show();
}

5.

#include <iostream>
using namespace std;

class BaseArray {
private:
    int capacity;
    int *mem;
protected:
    BaseArray(int capacity = 100) {
        this->capacity = capacity;
        mem = new int[capacity];
    }
    ~BaseArray() { delete[] mem; }
    void put(int index, int val) { mem[index] = val; }
    int get(int index) { return mem[index]; }
    int getCapacity() { return capacity; }
};

class MyQueue : public BaseArray {
    int front, rear, leng;
public:
    MyQueue(int n) : BaseArray(n) { front = 0; rear = 0; leng = 0; }
    void enqueue(int n) {
        put(rear++, n);
        if (getCapacity() == rear) rear = 0;
        leng++;
    }
    int capacity() { return getCapacity(); }
    int length() { return leng; }
    int dequeue() {
        int tmp = get(front++);
        if (getCapacity() == front) front = 0;
        leng--;
        return tmp;
    }
};

int main() {
    MyQueue mQ(100);
    int n;
    cout << "큐에 삽입할 5개의 정수를 입력하라>> ";
    for (int i = 0; i < 5; i++) {
        cin >> n;
        mQ.enqueue(n);
    }
    cout << "큐의 용량:" << mQ.capacity() << ", 큐의 크기:" << mQ.length() << endl;
    cout << "큐의 원소 순서대로 제거 후 출력>>";
    while (mQ.length() != 0) {
        cout << mQ.dequeue() << ' ';
    }
    cout << endl << "큐 현재크기:" << mQ.length() << endl;
}

6.

#include <iostream>
using namespace std;

class BaseArray {
private:
    int capacity;
    int *mem;
protected:
    BaseArray(int capacity = 100) {
        this->capacity = capacity; mem = new int[capacity];
    }
    ~BaseArray() { delete[] mem; }
    void put(int index, int val) { mem[index] = val; }
    int get(int index) { return mem[index]; }
    int getCapacity() { return capacity; }
};

class MyStack : public BaseArray {
    int leng;
public:
    MyStack(int capacity) : BaseArray(capacity) { leng = 0; }
    void push(int n) { put(leng++, n); }
    int capacity() { return getCapacity(); }
    int length() { return leng; }
    int pop() {
        leng--;
        return get(leng);
    }
};

int main() {
    MyStack mStack(100);
    int n;
    cout << "스택에 삽입할 5개의 정수를 입력하라>> ";
    for (int i = 0; i < 5; i++) {
        cin >> n;
        mStack.push(n);
    }
    cout << "스택용량:" << mStack.capacity() << ", 스택크기:" << mStack.length() << endl;
    cout << "스택의 모든 원소를 팝하여 출력한다>> ";
    while (mStack.length() != 0) {
        cout << mStack.pop() << ' ';
    }
    cout << endl << "스택의 현재 크기 : " << mStack.length() << endl;
}

7.

#include <iostream>
using namespace std;

class BaseMemory {
    char *mem;
protected:
    BaseMemory(int size) { mem = new char[size]; }
    ~BaseMemory() { delete[] mem; }
    void put(int index, char ch) { mem[index] = ch; }
    char get(int index) { return mem[index]; }
};

class ROM : public BaseMemory {
    int leng;
public:
    ROM(int size, char arr[], int leng) : BaseMemory(size) {
        this->leng = leng;
        for (int i = 0; i < leng; i++) {
            put(i, arr[i]);
        }
    }
    char read(int index) { return get(index); }
};

class RAM : public BaseMemory {
public:
    RAM(int size) : BaseMemory(size) {}
    void write(int index, char ch) { put(index, ch); }
    char read(int index) { return get(index); }
};

int main() {
    char x[5] = {'h', 'e', 'l', 'l', 'o'};
    ROM biosROM(1024 * 10, x, 5);
    RAM mainMemory(1024 * 1024);

    for (int i = 0; i < 5; i++) mainMemory.write(i, biosROM.read(i));
    for (int i = 0; i < 5; i++) cout << mainMemory.read(i);
}

8.

#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;
        printedCount = 0;
    }
    bool print(int pages) {
        if (pages <= availableCount) {
            availableCount -= pages;
            printedCount = pages;
            cout << "프린트하였습니다." << endl;
            return true;
        }
        else {
            cout << "용지가 부족하여 프린트할 수 없습니다." << endl;
            return false;
        }
    }
    string getModel() { return model; }
    string getManufacturer() { return manufacturer; }
    int getAvailableCount() { return availableCount; }
};

class InkJet : public Printer {
    int availableInk;
public:
    InkJet(string model, string manufacturer, int availableCount, int availableInk) : Printer(model, manufacturer, availableCount)
    {
        this->availableInk = availableInk;
    }
    void printInkJet(int pages) {
        if (pages <= availableInk)
        {
            if (print(pages)) availableInk -= pages;
        }
        else cout << "잉크가 부족하여 프린트할 수 없습니다." << endl;
    }
    void show() {
        cout << getModel() << ", " << getManufacturer() << ", 남은 종이 " << getAvailableCount() << "장, 남은 잉크 " << availableInk << endl;
    }
};

class Laser : public Printer {
    int availableToner;
public:
    Laser(string model, string manufacturer, int availableCount, int availableToner) : Printer(model, manufacturer, availableCount)
    {
        this->availableToner = availableToner;
    }
    void printLaser(int pages) {
        if (pages <= availableToner)
        {
            if (print(pages)) availableToner -= pages;
        }
        else cout << "토너가 부족하여 프린트할 수 없습니다." << endl;
    }
    void show() {
        cout << getModel() << ", " << getManufacturer() << ", 남은 종이 " << getAvailableCount() << "장, 남은 토너 " << availableToner << endl;
    }
};

int main() {
    InkJet inkjet("Officejet V40", "HP", 5, 10);
    Laser laser("SCX-6x45", "삼성전자", 3, 20);

    cout << "현재 작동중인 2 대의 프린터는 아래와 같다" << endl;
    cout << "잉크젯 : ";
    inkjet.show();
    cout << "레이저 : ";
    laser.show();

    int print, count;
    string yes = "y";
    while (yes == "y")
    {
        cout << endl << "프린터(1:잉크젯, 2:레이저)와 매수 입력>>";
        cin >> print >> count;
        if (print == 1) inkjet.printInkJet(count);
        else laser.printLaser(count);
        inkjet.show();
        laser.show();

        cout << "계속 프린트 하시겠습니까(y/n)>>";
        cin >> yes;
    }
}

9.

main.cpp

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

extern AirlineBook *air;

int main() {
    air = new AirlineBook();
    Console::start();
    delete air;
}
airline.h
#pragma once
#include <string>
using namespace std;

class Schedule;
class Seat;
class Console;

class AirlineBook {
    Schedule *scd;
public:
    AirlineBook();
    ~AirlineBook();
    void showSchedule(int index);
    void setAirline(int index, int num, string name);
    void deleteAirline(int index, int num, string name);
};

class Schedule {
    Seat *seat;
    string time;
public:
    Schedule();
    ~Schedule();
    void setTime(string time);
    void show();
    void setSchedule(int num, string name);
    void deleteSchedule(int num, string name);
};

class Seat {
    string seat;
public:
    Seat();
    string getSeat();
    void setSeat(string name);
};

class Console {
public:
    static void start();
    static int menu();
    static void reservation();
    static void cancle();
    static void show();
    friend void AirlineBook::showSchedule(int index);
};

airline.cpp

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

AirlineBook *air;

AirlineBook::AirlineBook() {
    scd = new Schedule[3];
    scd[0].setTime("07시");
    scd[1].setTime("12시");
    scd[2].setTime("17시");
}

AirlineBook::~AirlineBook() {
    delete[] scd;
}

void AirlineBook::showSchedule(int index) {
    scd[index].show();
}

void AirlineBook::setAirline(int index, int num, string name) {
    scd[index].setSchedule(num, name);
}

void AirlineBook::deleteAirline(int index, int num, string name) {
    scd[index].deleteSchedule(num, name);
}

Schedule::Schedule() {
    seat = new Seat[8];
}

Schedule::~Schedule() {
    delete[] seat;
}

void Schedule::setTime(string time) {
    this->time = time;
}

void Schedule::show() {
    cout << time << ":\t";
    for (int i = 0; i < 8; i++) {
        cout << seat[i].getSeat() << "\t";
    }
    cout << endl;
}

void Schedule::setSchedule(int num, string name) {
    seat[num].setSeat(name);
}

void Schedule::deleteSchedule(int num, string name) {
    if (seat[num].getSeat() == name) seat[num].setSeat("---");
}

Seat::Seat() {
    seat = "---";
}

string Seat::getSeat() {
    return seat;
}

void Seat::setSeat(string name) {
    seat = name;
}

void Console::start() {
    cout << "***** 한성항공에 오신것을 환영합니다 *****" << endl;
    while (menu());
    cout << "예약 시스템을 종료합니다." << endl;
}

int Console::menu() {
    int n;
    cout << endl << "예약:1, 취소:2, 보기:3, 끝내기:4>> ";
    cin >> n;
    switch (n)
    {
    case 1: reservation(); break;
    case 2: cancle(); break;
    case 3: show(); break;
    case 4: return 0;
    }
    return 1;
}

void Console::reservation() {
    int n, d;
    string name;
    cout << "07시:1, 12시:2, 17시:3>> ";
    cin >> n;
    air->showSchedule(n - 1);
    cout << "좌석 번호>> ";
    cin >> d;
    cout << "이름 입력>> ";
    cin >> name;
    air->setAirline(n - 1, d - 1, name);
}

void Console::cancle() {
    int n, d;
    string name;
    cout << "07시:1, 12시:2, 17시:3>> ";
    cin >> n;
    air->showSchedule(n - 1);
    cout << "좌석 번호>> ";
    cin >> d;
    cout << "이름 입력>> ";
    cin >> name;
    air->deleteAirline(n - 1, d - 1, name);
}

void Console::show() {
    air->showSchedule(0);
    air->showSchedule(1);
    air->showSchedule(2);
}
728x90

댓글