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

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

by starfish22 2022. 1. 10.
728x90

1.

#include <iostream>
using namespace std;

class Circle {
    int radius;
public:
    Circle(int r) { radius = r; }
    void show() { cout << "반지름이 " << radius << "인 원" << endl; }
};

void swap(Circle &a, Circle &b) {
    Circle temp(a);
    a = b;
    b = temp;
}

int main() {
    Circle a(1), b(2);

    cout << "a : ";
    a.show();
    cout << "b : ";
    b.show();

    swap(a, b);

    cout << "a : ";
    a.show();
    cout << "b : ";
    b.show();
}

2.

#include <iostream>
using namespace std;

void half(double &n) {
    n /= 2;
}

int main() {
    double n = 20;
    half(n);
    cout << n;
}

3.

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

void combine(string a, string b, string &text) {
    text = a + " " + b;
}

int main() {
    string text1("I love you"), text2("very much");
    string text3;
    combine(text1, text2, text3);
    cout << text3;
}

4.

#include <iostream>
using namespace std;

bool bigger(int a, int b, int &big) {
    if (a == b) return true;
    if (a > b) big = a;
    else big = b;
    return false;
}

int main()
{
    int a, b;
    cin >> a >> b;
    int big;
    bool t = bigger(a, b, big);
    if (t) cout << "같다" << endl;
    else cout << big << endl;
}

5.

#include <iostream>
using namespace std;

class Circle {
    int radius;
public:
    Circle(int r) { radius = r; }
    int getRadius() { return radius; }
    void setRadius(int r) { radius = r; }
    void show() { cout << "반지금이 " << radius << "인 원" << endl; }
};

void increaseBy(Circle &a, Circle b) {
    int r = a.getRadius() + b.getRadius();
    a.setRadius(r);
}

int main() {
    Circle x(10), y(5);
    increaseBy(x, y);
    x.show();
}

6.

#include <iostream>
using namespace std;

char &find(char a[], char c, bool &success) {
    for (int i = 0; a[i] != '\0'; i++) {
        if (a[i] == c) {
            success = true;
            return a[i];
        }
    }
    success = false;
}

int main() {
    char s[] = "Mike";
    bool b = false;
    char &loc = find(s, 'M', b);
    if (b == false) {
        cout << "M을 발견할 수 없다" << endl;
        return 0;
    }
    loc = 'm';
    cout << s << endl;
}

7.

#include <iostream>
using namespace std;

class MyIntStack {
    int p[10];
    int tos;
public:
    MyIntStack() : tos(0) {}
    bool push(int n) {
        if (tos == 10) return false;
        p[tos++] = n;
        return true;
    }
    bool pop(int &n) {
        if (tos == 0) return false;
        n = p[--tos];
        return true;
    }
};

int main() {
    MyIntStack a;
    for (int i = 0; i < 11; i++) {
        if (a.push(i)) cout << i << ' ';
        else cout << endl << i + 1 << "번째 stack full" << endl;
    }
    int n;
    for (int i = 0; i < 11; i++) {
        if (a.pop(n)) cout << n << ' ';
        else cout << endl << i + 1 << "번째 stack empty";
    }
    cout << endl;
}

8.

#include <iostream>
using namespace std;

class MyIntStack {
    int *p;
    int size;
    int tos;
public:
    MyIntStack() : MyIntStack(1) {}
    MyIntStack(int size) {
        this->size = size;
        p = new int[size];
        tos = 0;
    }
    MyIntStack(const MyIntStack &s) {
        size = s.size;
        p = new int[size];
        for (int i = 0; i < size; i++) p[i] = s.p[i];
        tos = s.tos;
    }
    ~MyIntStack() { delete[] p; }

    bool push(int n) {
        if (tos == size) return false;
        p[tos++] = n;
        return true;
    }
    bool pop(int &n) {
        if (tos == 0) return false;
        n = p[--tos];
        return true;
    }
};

int main() {
    MyIntStack a(10);
    a.push(10);
    a.push(20);
    MyIntStack b = a;
    b.push(30);

    int n;
    a.pop(n);
    cout << "스택 a에서 팝한 값 " << n << endl;
    b.pop(n);
    cout << "스택 b에서 팝한 값 " << n << endl;
}

9.

#include <iostream>
using namespace std;

class Accumulator {
    int value;
public:
    Accumulator(int value) { this->value = value; }
    Accumulator &add(int n) {
        value += n;
        return *this;
    }
    int get() { return value; }
};

int main() {
    Accumulator acc(10);
    acc.add(5).add(6).add(7);
    cout << acc.get();
}

10.

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

class Buffer {
    string text;
public:
    Buffer(string text) { this->text = text; }
    void add(string next) { text += next; }
    void print() { cout << text << endl; }
};

Buffer &append(Buffer &buf, string str) {
    buf.add(str);
    return buf;
}

int main() {
    Buffer buf("Hello");
    Buffer &temp = append(buf, "Guys");
    temp.print();
    buf.print();
}

11.

(1)

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

class Book {
    char *title;
    int price;
public:
    Book(const char *title, int price) {
        this->title = new char[strlen(title) + 1];
        strcpy(this->title, title);
        this->price = price;
    }
    ~Book() { delete[] title; }
    void set(char *title, int price) {
        if (this->title != nullptr) delete[] this->title;
        this->title = new char[strlen(title) + 1];
        strcpy(this->title, title);
        this->price = price;
    }
    void show() { cout << title << ' ' << price << "원" << endl; }
};

(2)

Book(const Book &book) {
    title = book.title;
    price = book.price;
}

(3)

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

class Book {
    char *title;
    int price;
public:
    Book(const char *title, int price) {
        this->title = new char[strlen(title) + 1];
        strcpy(this->title, title);
        this->price = price;
    }
    ~Book() { delete[] title; }
    Book(const Book &book) {
        title = new char[strlen(book.title) + 1];
        strcpy(title, book.title);
        price = book.price;
    }
    void set(char *title, int price) {
        if (this->title != nullptr) delete[] this->title;
        this->title = new char[strlen(title) + 1];
        strcpy(this->title, title);
        this->price = price;
    }
    void show() { cout << title << ' ' << price << "원" << endl; }
};

int main() {
    Book cpp("명품C++", 10000);
    Book java = cpp;
    java.set("명품자바", 12000);
    cpp.show();
    java.show();
}

(4)

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

class Book {
    string title;
    int price;
public:
    Book(const string title, int price) {
        this->title = title;
        this->price = price;
    }
    void set(string title, int price) {
        this->title = title;
        this->price = price;
    }
    void show() { cout << title << ' ' << price << "원" << endl; }
};

int main() {
    Book cpp("명품C++", 10000);
    Book java = cpp;
    java.set("명품자바", 12000);
    cpp.show();
    java.show();
}

12.

(1)

#include <iostream>
using namespace std;

class Dept {
    int size;
    int *scores;
public:
    Dept(int size) {
        this->size = size;
        scores = new int[size];
    }
    //Dept(const Dept& dept);
    ~Dept() { delete[] scores; }
    int getSize() { return size; }
    void read() {
        cout << size << "개 점수 입력>> ";
        for (int i = 0; i < size; i++) {
            cin >> scores[i];
        }
    }
    bool isOver60(int index) {
        if (scores[index] >= 60) return true;
        return false;
    }
};

int countPass(Dept dept) {
    int count = 0;
    for (int i = 0; i < dept.getSize(); i++) {
        if (dept.isOver60(i)) count++;
    }
    return count;
}

int main() {
    Dept com(10);
    com.read();
    int n = countPass(com);
    cout << "60점 이상은 " << n << "명";
}

(2) countPass함수의 인자가 Dept dept이므로 com을 dept에 복사하기 위해 복사 생성자가 호출된다. 복사 생성자만 선언하고 그 내용을 적지 않았기 때문에 오류가 발생한다.

(3)

#include <iostream>
using namespace std;

class Dept {
    int size;
    int *scores;
public:
    Dept(int size) {
        this->size = size;
        scores = new int[size];
    }
    ~Dept() { delete[] scores; }
    int getSize() { return size; }
    void read() {
        cout << size << "개 점수 입력>> ";
        for (int i = 0; i < size; i++) {
            cin >> scores[i];
        }
    }
    bool isOver60(int index) {
        if (scores[index] >= 60) return true;
        return false;
    }
};

int countPass(Dept &dept) {
    int count = 0;
    for (int i = 0; i < dept.getSize(); i++) {
        if (dept.isOver60(i)) count++;
    }
    return count;
}

int main() {
    Dept com(10);
    com.read();
    int n = countPass(com);
    cout << "60점 이상은 " << n << "명";
}
728x90

댓글