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

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

by starfish22 2022. 1. 26.
728x90

1.

(1)

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

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title; this->price = price; this->pages = pages;
    }
    Book &operator+=(int x) {
        price += x;
        return *this;
    }
    Book &operator-=(int x) {
        price -= x;
        return *this;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle() { return title; }
};

int main() {
    Book a("청춘", 20000, 300), b("미래", 30000, 500);
    a += 500;
    b -= 500;
    a.show();
    b.show();
}

(2)

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

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title; this->price = price; this->pages = pages;
    }
    friend void operator+=(Book &book, int x);
    friend void operator-=(Book &book, int x);
    void show() {
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle() { return title; }
};

void operator+=(Book &book, int x) {
    book.price += x;
}
void operator-=(Book &book, int x) {
    book.price -= x;
}

int main() {
    Book a("청춘", 20000, 300), b("미래", 30000, 500);
    a += 500;
    b -= 500;
    a.show();
    b.show();
}

2.

(1)

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

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title; this->price = price; this->pages = pages;
    }
    bool operator==(int x) {
        if (price == x) return true;
        else return false;
    }
    bool operator==(string text) {
        if (title == text) return true;
        else return false;
    }
    bool operator==(Book op) {
        if (title == op.title && price == op.price && pages == op.pages) return true;
        else return false;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle() { return title; }
};

int main() {
    Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
    if (a == 30000) cout << "정가 30000원" << endl;
    if (a == "명품 C++") cout << "명품 C++ 입니다." << endl;
    if (a == b) cout << "두 책이 같은 책입니다." << endl;
}

(2)

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

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title; this->price = price; this->pages = pages;
    }
    friend bool operator==(Book book, int x);
    friend bool operator==(Book book, string text);
    friend bool operator==(Book book, Book op);
    void show() {
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle() { return title; }
};

bool operator==(Book book, int x) {
    if (book.price == x) return true;
    else return false;
}
bool operator==(Book book, string text) {
    if (book.title == text) return true;
    else return false;
}
bool operator==(Book book, Book op) {
    if (book.title == op.title && book.price == op.price && book.pages == op.pages) return true;
    else return false;
}

int main() {
    Book a("명품 C++", 30000, 500), b("고품 C++", 30000, 500);
    if (a == 30000) cout << "정가 30000원" << endl;
    if (a == "명품 C++") cout << "명품 C++ 입니다." << endl;
    if (a == b) cout << "두 책이 같은 책입니다." << endl;
}

3.

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

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title; this->price = price; this->pages = pages;
    }
    bool operator!() {
        if (price == 0) return true;
        else return false;
    }
    void show() {
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle() { return title; }
};

int main() {
    Book book("벼룩시장", 0, 50);
    if (!book) cout << "공짜다" << endl;
}

4.

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

class Book {
    string title;
    int price, pages;
public:
    Book(string title = "", int price = 0, int pages = 0) {
        this->title = title; this->price = price; this->pages = pages;
    }
    friend bool operator<(Book op1, Book op2);
    void show() {
        cout << title << ' ' << price << "원 " << pages << " 페이지" << endl;
    }
    string getTitle() { return title; }
};

bool operator<(Book op1, Book op2) {
    if (op1.title < op2.title) return true;
    else return false;
}

int main() {
    Book a("청춘", 20000, 300);
    string b;
    cout << "책 이름을 입력하세요>>";
    getline(cin, b);
    if (b < a) cout << a.getTitle() << "이 " << b << "보다 뒤에 있구나!" << endl;
}

5.

(1)

#include <iostream>
using namespace std;

class Color {
    int red, green, blue;
public:
    Color(int r = 0, int g = 0, int b = 0) {
        red = r;
        green = g;
        blue = b;
    }
    Color operator+(Color op) {
        Color tmp;
        tmp.red = red + op.red;
        tmp.green = green + op.green;
        tmp.blue = blue + op.blue;
        return tmp;
    }
    bool operator==(Color op) {
        if (red == op.red && green == op.green && blue == op.blue) return true;
        else return false;
    }
    void show() { cout << red << ' ' << green << ' ' << blue << endl; }
};

int main() {
    Color red(255, 0, 0), blue(0, 0, 255), c;
    c = red + blue;
    c.show();

    Color fuchsia(255, 0, 255);
    if (c == fuchsia) cout << "보라색 맞음";
    else cout << "보라색 아님";
}

(2)

#include <iostream>
using namespace std;

class Color {
    int red, green, blue;
public:
    Color(int r = 0, int g = 0, int b = 0) {
        red = r;
        green = g;
        blue = b;
    }
    friend Color operator+(Color op1, Color op2);
    friend bool operator==(Color op1, Color op2);
    void show() { cout << red << ' ' << green << ' ' << blue << endl; }
};

Color operator+(Color op1, Color op2) {
    Color tmp;
    tmp.red = op1.red + op2.red;
    tmp.green = op1.green + op2.green;
    tmp.blue = op1.blue + op2.blue;
    return tmp;
}
bool operator==(Color op1, Color op2) {
    if (op1.red == op2.red && op1.green == op2.green && op1.blue == op2.blue) return true;
    else return false;
}

int main() {
    Color red(255, 0, 0), blue(0, 0, 255), c;
    c = red + blue;
    c.show();

    Color fuchsia(255, 0, 255);
    if (c == fuchsia) cout << "보라색 맞음";
    else cout << "보라색 아님";
}

6.

(1)

#include <iostream>
using namespace std;

class Matrix {
    int arr[4];
public:
    Matrix(int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0) {
        arr[0] = a1; arr[1] = a2; arr[2] = a3; arr[3] = a4;
    }
    Matrix operator+(Matrix op) {
        Matrix tmp;
        for (int i = 0; i < 4; i++) {
            tmp.arr[i] = arr[i] + op.arr[i];
        }
        return tmp;
    }
    void operator+=(Matrix op) {
        for (int i = 0; i < 4; i++) {
            arr[i] += op.arr[i];
        }
    }
    bool operator==(Matrix op) {
        for (int i = 0; i < 4; i++) {
            if (arr[i] != op.arr[i]) return false;
        }
        return true;
    }
    void show() {
        cout << "Matrix = { ";
        for (int i = 0; i < 4; i++) cout << arr[i] << ' ';
        cout << "}" << endl;
    }
};

int main() {
    Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
    c = a + b;
    a += b;
    a.show();
    b.show();
    c.show();
    if (a == c) cout << "a and c are the same" << endl;
}

(2)

#include <iostream>
using namespace std;

class Matrix {
    int arr[4];
public:
    Matrix(int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0) {
        arr[0] = a1; arr[1] = a2; arr[2] = a3; arr[3] = a4;
    }
    friend Matrix operator+(Matrix op1, Matrix op2);
    friend void operator+=(Matrix &op1, Matrix op2);
    friend bool operator==(Matrix op1, Matrix op2);
    void show() {
        cout << "Matrix = { ";
        for (int i = 0; i < 4; i++) cout << arr[i] << ' ';
        cout << "}" << endl;
    }
};

Matrix operator+(Matrix op1, Matrix op2) {
    Matrix tmp;
    for (int i = 0; i < 4; i++) {
        tmp.arr[i] = op1.arr[i] + op2.arr[i];
    }
    return tmp;
}
void operator+=(Matrix &op1, Matrix op2) {
    for (int i = 0; i < 4; i++) {
        op1.arr[i] += op2.arr[i];
    }
}
bool operator==(Matrix op1, Matrix op2) {
    for (int i = 0; i < 4; i++) {
        if (op1.arr[i] != op2.arr[i]) return false;
    }
    return true;
}

int main() {
    Matrix a(1, 2, 3, 4), b(2, 3, 4, 5), c;
    c = a + b;
    a += b;
    a.show();
    b.show();
    c.show();
    if (a == c) cout << "a and c are the same" << endl;
}

7.

(1)

#include <iostream>
using namespace std;

class Matrix {
    int arr[4];
public:
    Matrix(int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0) {
        arr[0] = a1; arr[1] = a2; arr[2] = a3; arr[3] = a4;
    }
    void operator>>(int x[]) {
        for (int i = 0; i < 4; i++) x[i] = arr[i];
    }
    void operator<<(int x[]) {
        for (int i = 0; i < 4; i++) arr[i] = x[i];
    }
    void show() {
        cout << "Matrix = { ";
        for (int i = 0; i < 4; i++) cout << arr[i] << ' ';
        cout << "}" << endl;
    }
};

int main() {
    Matrix a(4, 3, 2, 1), b;
    int x[4], y[4] = {1, 2, 3, 4};
    a >> x;
    b << y;

    for (int i = 0; i < 4; i++) cout << x[i] << ' ';
    cout << endl;
    b.show();
}

(2)

#include <iostream>
using namespace std;

class Matrix {
    int arr[4];
public:
    Matrix(int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0) {
        arr[0] = a1; arr[1] = a2; arr[2] = a3; arr[3] = a4;
    }
    friend void operator>>(Matrix op, int x[]);
    friend void operator<<(Matrix &op, int x[]);
    void show() {
        cout << "Matrix = { ";
        for (int i = 0; i < 4; i++) cout << arr[i] << ' ';
        cout << "}" << endl;
    }
};

void operator>>(Matrix op, int x[]) {
    for (int i = 0; i < 4; i++) x[i] = op.arr[i];
}
void operator<<(Matrix &op, int x[]) {
    for (int i = 0; i < 4; i++) op.arr[i] = x[i];
}

int main() {
    Matrix a(4, 3, 2, 1), b;
    int x[4], y[4] = {1, 2, 3, 4};
    a >> x;
    b << y;

    for (int i = 0; i < 4; i++) cout << x[i] << ' ';
    cout << endl;
    b.show();
}

8.

#include <iostream>
using namespace std;

class Circle {
    int radius;
public:
    Circle(int radius = 0) { this->radius = radius; }
    friend Circle &operator++(Circle &op);
    friend Circle operator++(Circle &op, int x);
    void show() { cout << "radius = " << radius << " 인 원" << endl; }
};

Circle &operator++(Circle &op) {
    op.radius++;
    return op;
}
Circle operator++(Circle &op, int x) {
    Circle tmp = op;
    op.radius++;
    return tmp;
}

int main() {
    Circle a(5), b(4);
    ++a;
    b = a++;
    a.show();
    b.show();
}

9.

#include <iostream>
using namespace std;

class Circle {
    int radius;
public:
    Circle(int radius = 0) { this->radius = radius; }
    friend Circle operator+(int x, Circle op);
    void show() { cout << "radius = " << radius << " 인 원" << endl; }
};

Circle operator+(int x, Circle op) {
    op.radius += x;
    return op;
}

int main() {
    Circle a(5), b(4);
    b = 1 + a;
    a.show();
    b.show();
}

10.

#include <iostream>
using namespace std;

class Statistics {
    int *arr;
    int max, size;
public:
    Statistics(int max = 10) {
        arr = new int[max];
        size = 0;
    }
    bool operator!() {
        if (size == 0) return true;
        else false;
    }
    Statistics &operator<<(int x) {
        arr[size++] = x;
        return *this;
    }
    void operator~() {
        for (int i = 0; i < size; i++) cout << arr[i] << ' ';
        cout << endl;
    }
    void operator>>(int &avg) {
        int sum = 0;
        for (int i = 0; i < size; i++) sum += arr[i];
        avg = sum / size;
    }
    ~Statistics() { delete[] arr; }
};

int main()
{
    Statistics stat;
    if (!stat) cout << "현재 통계 데이타가 없습니다." << endl;

    int x[5];
    cout << "5 개의 정수를 입력하라>>";
    for (int i = 0; i < 5; i++) cin >> x[i];

    for (int i = 0; i < 5; i++) stat << x[i];
    stat << 100 << 200;
    ~stat;

    int avg;
    stat >> avg;
    cout << "avg=" << avg << endl;
}

11.

#include <iostream>
using namespace std;

class Stack {
    int *arr;
    int max, size;
public:
    Stack(int max = 10) {
        arr = new int[max];
        size = 0;
    }
    Stack &operator<<(int x) {
        arr[size++] = x;
        return *this;
    }
    void operator>>(int &x) {
        x = arr[size - 1];
        size--;
    }
    bool operator!() {
        if (size == 0) return true;
        else return false;
    }
    ~Stack() { delete[] arr; }
};

int main() {
    Stack stack;
    stack << 3 << 5 << 10;
    while (true) {
        if (!stack) break;
        int x;
        stack >> x;
        cout << x << ' ';
    }
    cout << endl;
}

12.

#include <iostream>
using namespace std;

class SortedArray {
    int size;
    int *p;
    void sort();
public:
    SortedArray();
    SortedArray(SortedArray &src);
    SortedArray(int p[], int size);
    ~SortedArray();
    SortedArray operator+(SortedArray &op2);
    SortedArray &operator=(const SortedArray &op2);
    void show();
};

void SortedArray::sort() {
    for (int i = 0; i < size - 1; i++) {
        for (int j = i + 1; j < size; j++) {
            if (p[i] > p[j]) {
                int tmp = p[i];
                p[i] = p[j];
                p[j] = tmp;
            }
        }
    }
}

SortedArray::SortedArray() {
    p = nullptr;
    size = 0;
}

SortedArray::SortedArray(SortedArray &src) {
    if (p) delete[] p;
    p = new int[src.size];
    for (int i = 0; i < src.size; i++) p[i] = src.p[i];
    size = src.size;
}

SortedArray::SortedArray(int p[], int size) {
    this->p = new int[size];
    this->size = size;
    for (int i = 0; i < size; i++) this->p[i] = p[i];
    sort();
}

SortedArray::~SortedArray() {
    if (p) delete[] p;
}

SortedArray SortedArray::operator+(SortedArray &op2) {
    SortedArray tmp;
    tmp.p = new int[this->size + op2.size];
    for (int i = 0; i < this->size; i++) tmp.p[tmp.size++] = this->p[i];
    for (int i = 0; i < op2.size; i++) tmp.p[tmp.size++] = op2.p[i];
    tmp.sort();
    return tmp;
}

SortedArray &SortedArray::operator=(const SortedArray &op2) {
    if (p) delete[] p;
    p = new int[op2.size];
    for (int i = 0; i < op2.size; i++) p[i] = op2.p[i];
    size = op2.size;
    return *this;
}

void SortedArray::show() {
    cout << "배열 출력 : ";
    for (int i = 0; i < size; i++) cout << p[i] << ' ';
    cout << endl;
}

int main() {
    int n[] = {2, 20, 6};
    int m[] = {10, 7, 8, 30};
    SortedArray a(n, 3), b(m, 4), c;

    c = a + b;

    a.show();
    b.show();
    c.show();
}
728x90

댓글