Post

[C++] 명품 C++ 프로그래밍 11장 실습문제 풀이

11장 C++ 입출력 시스템


1번

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
    int ch;
    int count = 0;
    while ((ch = cin.get()) != EOF) {
        if (ch == 'a')
            count++;
    }
    cout << count << endl;
}

2번

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main() {
    char ch;
    int count = 0;
    while(true){
    	cin.get(ch);
        if(cin.eof())
        	break;
        if(ch == ' ')
        	count++;
	}
    cout << count << endl;
}

3번

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
    int ch;
    cin.ignore(100,';');
    while ((ch = cin.get()) != EOF) {
        cout.put(ch);
        if (ch == '\n')
            cin.ignore(100,';');
    }
}

4번

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main() {
    int ch;
    while ((ch = cin.get()) != EOF) {
        if (ch == ';') {
            cout.put('\n');
            cin.ignore(100, '\n');
        }
        else
            cout.put(ch);
    }
}

5번

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

int main() {
    string cmd;
    while (true) {
        cout << "종료하려면 exit을 입력하세요 >> ";
        getline(cin, cmd);
        if (cmd == "exit") {
            cout << "프로그램을 종료합니다." << endl;
            return 0;
        }
    }
}

6번

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main() {
    cout << setw(15) << left << "Number";
    cout << setw(15) << left << "Square";
    cout << setw(15) << left << "Square Root"<<endl;

    cout.fill('_');
    for (int i = 0; i < 45; i += 5) {
        cout << setw(15) << i;
        cout << setw(15) << i * i;
        cout << setw(15) << setprecision(3) <<sqrt(i);
        cout << endl;
    }
}

7번

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    for (int i = 0; i < 4; i++)
        cout << "dec\t" << "hexa\t" << "char\t";
    cout << endl;
    for (int i = 0; i < 4; i++)
        cout << "___\t" << "____\t" << "____\t";
    cout << endl;
    cout.fill(' ');
    for (int i = 0; i < 128; i++) {
        if (i != 0 && i % 4 == 0)
            cout << endl;
        cout << setbase(10) << i << '\t';
        cout << setbase(16) << i << '\t';
        if (isprint(char(i)))
            cout << (char)i << '\t';
        else
            cout << '.' << '\t';
    }
}

8번

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
using namespace std;

class Circle {
    string name;
    int radius;
public:
    Circle(int radius = 1, string name = "") {
        this->radius = radius;
        this->name = name;
    }
    friend istream& operator>>(istream& in, Circle& c);
    friend ostream& operator<<(ostream& out, Circle& c);
};
istream& operator>>(istream& in, Circle& c) {
    string name;
    int radius;
    cout << "반지름 >> ";
    in >> radius; in.ignore();
    cout << "이름 >> ";
    in >> name; in.ignore();
    c.name = name;
    c.radius = radius;
    return in;
}
ostream& operator<<(ostream& out, Circle& c) {
    out << "(반지름" << c.radius << "인 " << c.name << ")";
    return out;
}
int main() {
    Circle d, w;
    cin >> d >> w;
    cout << d << w << endl;
}

9번

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
using namespace std;

class Phone {
    string name, telnum, address;
public:
    Phone(string name = "", string telnum = "", string address = "") {
        this->name = name;
        this->telnum = telnum;
        this->address = address;
    }
    friend istream& operator>>(istream& in, Phone& p);
    friend ostream& operator<<(ostream& out, Phone& p);
};

istream& operator>>(istream& in, Phone& p) {
    cout << "이름 : ";
    getline(in, p.name);
    cout << "전화번호 : ";
    getline(in, p.telnum);
    cout << "주소 : ";
    getline(in, p.address);
    return in;
}
ostream& operator<<(ostream& out, Phone& p) {
    out << "(" << p.name << ',' << p.telnum << ',' << p.address << ")";
    return out;
}

int main() {
    Phone girl, boy;
    cin >> girl >> boy;
    cout << girl << endl << boy << endl;
}

10번

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

istream& prompt(istream& in) {
    cout << "암호?";
    return in;
}
int main() {
    string password;
    while (true) {
        cin >> prompt >> password;
        if (password == "C++") {
            cout << "login success!" << endl;
            break;
        }
        else
            cout << "login fail. try again!!" << endl;
    }
}

11번

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

istream& pos(istream& in) {
    cout << "위치는? ";
    return in;
}
int main() {
    int x, y;
    cin >> pos >> x;
    cin >> pos >> y;
    cout << x << ',' << y << endl;
}

12번

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

class ingredient {
protected:
    int remain = 3;
public:
    virtual void show() {
        for (int i = 0; i < remain; i++)
            cout << '*';
        cout << endl;
    }
    void fill() { remain = 3; }
    void consume() { remain--; }
    bool isConsume() {
        if (remain < 1)
            return false;
        else
            return true;
    }
};

class Coffee : public ingredient {
public:
    virtual void show() override {
        cout << setw(8) << left << "Coffee";
        ingredient::show();
    }
};
class Sugar : public ingredient {
public:
    virtual void show() {
        cout << setw(8) << left << "Sugar";
        ingredient::show();
    }
};
class Cream : public ingredient {
public:
    virtual void show() {
        cout << setw(8) << left << "Cream";
        ingredient::show();
    }
};
class Water : public ingredient {
public:
    virtual void show() {
        cout << setw(8) << left << "Water";
        ingredient::show();
    }
};
class Cup : public ingredient {
public:
    virtual void show() {
        cout << setw(8) << "Cup";
        ingredient::show();
    }
};

class CoffeeMachine {
    vector<ingredient*> ingredientVt;
    int getMenu() {
        int selectMenu;
        cout << "보통 커피:0, 설탕 커피:1, 블랙 커피:2, 채우기:3, 종료:4 >> ";
        cin >> selectMenu;
        return selectMenu;
    }
    void fillAll() {
        cout << "모든 통을 채웁니다~~";
        for (auto& a : ingredientVt)
            a->fill();
        cout << endl;
    }
    void showAll() {
        for (auto& a : ingredientVt)
            a->show();
        cout << endl;
    }
    bool isGetCoffee(int coffee) {
        if (!ingredientVt[0]->isConsume() || !ingredientVt[3]->isConsume() || !ingredientVt[4]->isConsume())
            return false;
        switch (coffee) {
        case 0:
            if (!ingredientVt[2]->isConsume())
                return false;
            else
                ingredientVt[2]->consume();
        case 1:
            if (!ingredientVt[1]->isConsume())
                return false;
            else
                ingredientVt[1]->consume();
            break;
        }
        ingredientVt[0]->consume();
        ingredientVt[3]->consume();
        ingredientVt[4]->consume();
        return true;
    }
    void getCoffee(int coffee, bool flag) {
        if (!flag)
            cout << "재료가 부족합니다." << endl;
        else {
            switch (coffee) {
            case 0:
                cout << "맛있는 보통 커피 나왔습니다~~" << endl;
                break;
            case 1:
                cout << "맛있는 설탕 커피 나왔습니다~~" << endl;
                break;
            case 2:
                cout << "맛있는 블랙 커피 나왔습니다~~" << endl;
                break;

            }
        }
    }
public:
    CoffeeMachine() {
        ingredientVt.push_back(new Coffee());
        ingredientVt.push_back(new Sugar());
        ingredientVt.push_back(new Cream());
        ingredientVt.push_back(new Water());
        ingredientVt.push_back(new Cup());
    }
    void run() {
        cout << "----- 명품 커피 자판기입니다. -----" << endl;
        showAll();
        int coffee;
        bool flag;
        while (true) {
            switch (coffee = getMenu()) {
            case 0: case 1: case 2:
                flag = isGetCoffee(coffee);
                getCoffee(coffee, flag);
                break;
            case 3:
                fillAll();
                break;
            case 4:
                return;
            }
            showAll();
        }
    }
};

int main() {
    CoffeeMachine CM;
    CM.run();
}
This post is licensed under CC BY 4.0 by the author.