본문 바로가기
C++

[명품 c++] 3장 실습문제

by Meaning_ 2022. 10. 18.
728x90
반응형

1번

 

 

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
#include <iostream>
using namespace std;
 
class Tower {
 
    int height;
 
public:
    Tower() {
        height = 1;
    };
    Tower(int height) {
        this->height = height;
    
    }
 
    int getHeight() {
        return height;
    }
};
 
int main() {
 
    Tower myTower;
    Tower seoulTower(10);
    
    cout << "높이는 " << myTower.getHeight() << "미터"<<endl;
    cout << "높이는 " << seoulTower.getHeight() <<"미터"<< endl;
 
    
}
cs

 

 

2번

 

 

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
#include <iostream>
#include <string>
using namespace std;
 
 
class Date {
 
    int year;
    int month;
    int day;
    string str;
 
public:
 
    Date(int year, int month, int day) {
        this->year = year;
        this->month = month;
        this->day = day;
 
    }
 
    Date(string str) {
        this->str = str;
 
    }
    void show() {
        int index1 = str.find("/");
        string year = str.substr(0, index1);
        int index2 = str.find("/", index1 + 1); //찾을 문자열, 시작할 인덱스
        string month = str.substr(index1 + 1, index2-index1+1); //시작위치, 길이
        string day = str.substr(index2 + 1, str.length() - index2 + 1);
 
        cout << stoi(year) << "년" << stoi(month) << "월" << stoi(day) << "일" << endl;
 
 
 
        
    }
 
    int getMonth() {
        return month;
    }
 
    int getYear() {
        return year;
    }
 
    int getDay() {
        return day;
    }
};
 
int main() {
 
    Date birth(2014320);
    Date indepedenceDay("1945/8/15");
    indepedenceDay.show();
    cout << birth.getYear() << ',' << birth.getMonth() << ',' << birth.getDay() << endl;
 
}
cs

 

 

find함수는 find(찾고자하는 문자열, 시작 할 인덱스)

substr함수는 substr(시작할 인덱스, 길이)

 

 

3번

 

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
#include <iostream>
#include <string>
using namespace std;
 
 
class Account {
 
    string owner;
    int id;
    int amount;
 
public:
    Account(string owner, int id, int amount) {
        this->owner = owner;
        this->id = id;
        this->amount = amount;
    }
    string getOwner() {
        return owner;
    }
 
    int  withdraw(int money) {
        amount -= money;
        return money;
    }
 
    void depoist(int money) {
        amount += money;
 
    }
 
    int inquiry() {
        return amount;
    }
 
};
 
int main() {
 
    Account a("kitae"15000);
    a.depoist(50000);
    cout << a.getOwner() << "의 잔액은 " << a.inquiry() << endl;
    int money = a.withdraw(20000);
    cout << a.getOwner() << "의 잔액은 " << a.inquiry() << endl;
 
 
}
cs

 

 

4번

 

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
#include <iostream>
#include <string>
using namespace std;
 
 
class CoffeeMachine {
 
    int coffee;
    int water;
    int sugar;
 
public:
    CoffeeMachine(int coffee, int water, int sugar) {
        this->coffee = coffee;
        this->water = water;
        this->sugar = sugar;
    }
 
    void drinkEspresso() {
        coffee -= 1;
        water--;
    }
 
    void drinkAmericano() {
        coffee--;
        water -= 2;
    }
 
    void drinkSugarCoffee() {
        coffee--;
        water -= 2;
        sugar--;
    }
 
    void show() {
        cout << "커피 머신 상태, " << "커피:" << coffee << " 물:" << water << " 설탕:" << sugar << endl;
    }
 
    void fill() {
        coffee = 10;
        water = 10;
        sugar = 10;
    }
};
 
int main() {
 
    CoffeeMachine java(5103);
    java.drinkEspresso();
    java.show();
    java.drinkAmericano();
    java.show();
    java.drinkSugarCoffee();
    java.show();
    java.fill();
    java.show();
}
cs

 

 

5번

 

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <random>
using namespace std;
 
 
class Random {
 
public:
    Random() {
        srand((unsigned)time(0));
    }
    int next() {
 
        // a부터b까지 랜덤한 rand()%(b-a+1)+a;
        //return rand() % (RAND_MAX + 1);
 
        return rand() % (RAND_MAX + 1);
    }
 
    int nextInRange(int a, int b) {
        return rand() % (b - a + 1+ a;
    }
 
 
};
 
int main() {
    Random r;
    cout << "-- 0에서 " << RAND_MAX << " 까지의 랜덤 정수 10개--" << endl;
    for (int i = 0; i < 10; i++) {
        int n = r.next();
        cout << n << ' ';
    }
 
    cout << endl << endl << "-- 2에서" << "4 까지의 랜덤 정수 10개 --" << endl;
    for (int i = 0; i < 10; i++) {
        int n = r.nextInRange(24);
        cout << n << ' ';
    }
    cout << endl;
 
}
cs

 

 

 

6번

 

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <random>
using namespace std;
 
 
class EventRandom {
 
public:
    EventRandom() {
        srand((unsigned)time(0));
    }
    int next() {
 
        // a부터b까지 랜덤한 rand()%(b-a+1)+a;
        //return rand() % (RAND_MAX + 1);
 
        int randNum;
 
        while (true) {
 
            //짝수가 나올때까지 while을 돌린다.
 
            randNum = rand() % (RAND_MAX + 1);
            if (randNum % 2 == 0) {
                break;
            }
 
        }
 
        return randNum;
 
        
 
 
        
    }
 
    int nextInRange(int a, int b) {
        int randNum;
 
        while (true) {
            randNum = rand() % (b - a + 1+ a;
            if (randNum % 2 == 0) {
                break;
            }
        }
 
        return randNum;
    }
 
 
 
};
 
class Random {
 
public:
    Random() {
        srand((unsigned)time(0));
    }
    int next() {
 
        // a부터b까지 랜덤한 rand()%(b-a+1)+a;
        //return rand() % (RAND_MAX + 1);
 
        return rand() % (RAND_MAX + 1);
    }
 
    int nextInRange(int a, int b) {
        return rand() % (b - a + 1+ a;
    }
 
 
};
 
int main() {
    //Random r;
    /*cout << "-- 0에서 " << RAND_MAX << " 까지의 랜덤 정수 10개--" << endl;
    for (int i = 0; i < 10; i++) {
        int n = r.next();
        cout << n << ' ';
    }
 
    cout << endl << endl << "-- 2에서" << "4 까지의 랜덤 정수 10개 --" << endl;
    for (int i = 0; i < 10; i++) {
        int n = r.nextInRange(2, 4);
        cout << n << ' ';
    }
    cout << endl;*/
 
    EventRandom er;
 
    cout << "-- 0에서 " << RAND_MAX << " 까지의 랜덤 정수 10개--" << endl;
    for (int i = 0; i < 10; i++) {
        int n = er.next();
        cout << n << ' ';
    }
 
    cout << endl << endl << "-- 2에서" << "4 까지의 랜덤 정수 10개 --" << endl;
    for (int i = 0; i < 10; i++) {
        int n = er.nextInRange(24);
        cout << n << ' ';
    }
    cout << endl;
 
 
}
cs

 

7번

 

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <random>
using namespace std;
 
 
class SelectRandom {
 
    bool e;
 
public:
    SelectRandom();
    SelectRandom(bool e);
 
    int next();
    int nextInRange(int a,int b);
 
};
 
SelectRandom::SelectRandom() {
    e = true//짝수일 때 
    srand((unsigned)time(0));
 
}
 
SelectRandom::SelectRandom(bool e) {
    this->= e;
    srand((unsigned)time(0));
    
 
}
 
int SelectRandom::next() {
 
    
    if (e) {
        //짝수일 때는
        while (true) {//짝수가 나올때까지 뽑는다
            int randNum = rand() % (RAND_MAX + 1);
            if (randNum % 2 == 0) {
                return randNum;
            }
        }
 
    }
    else {
        //홀수일 때는
        while (true) {
            int randNum = rand() % (RAND_MAX + 1);
            if (randNum % 2 == 1) {
                return randNum;
            }
        }
 
    }
}
 
int SelectRandom::nextInRange(int a,int b) {
 
    if (e) {
        //짝수일 때는
        while (true) {
            int randNum = rand() % (b-a+1)+a;
            if (randNum % 2 == 0) {
                return randNum;
            }
        }
 
    }
    else {
        //홀수일 때는
        while (true) {
            int randNum = rand() % (b-a+1)+a;
            if (randNum % 2 == 1) {
                return randNum;
            }
        }
 
    }
 
 
}
 
 
int main() {
 
    SelectRandom evenRandom; // 짝수 랜덤발생기
    cout << "-- 0에서 " << RAND_MAX << "까지의 짝수 랜덤 정수 10 개--" << endl;
    for (int i = 0; i < 10; i++) {
        int n = evenRandom.next(); // 0에서 RAND_MAX(32767) 사이의 랜덤한 짝수 정수
        cout << n << ' ';
    }
    SelectRandom oddRandom(false); // 홀수 랜덤발생기
    cout << endl << endl << "-- 2에서 " << "9 까지의 랜덤 홀수 정수 10 개 --" << endl;
    for (int i = 0; i < 10; i++) {
        int n = oddRandom.nextInRange(29); // 2에서 9 사이의 랜덤한 정수
        cout << n << ' ';
    }
    cout << endl;
 
 
}
cs

 

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
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <random>
using namespace std;
 
 
class Oval {
    int width;
    int height;
 
public:
    Oval(int width,int height);
    Oval();
    ~Oval();
    int getWidth();
    int getHeight();
    void set(int width, int height);
    void show();
 
};
 
Oval::Oval(int width,int height) {
    this->width = width;
    this->height = height;
}
 
Oval::Oval() {
    width = 1;
    height = 1;
}
Oval::~Oval() {
    cout << "Oval 소멸 : width =" << width << ", height=" << height << endl;
}
 
int Oval::getWidth() {
    return width;
}
 
int Oval::getHeight() {
    return height;
}
 
void Oval::set(int width, int height) {
    this->width = width;
    this->height = height;
}
 
void Oval::show() {
    cout << "width = " << width << ", height= " << height << endl;
}
 
 
int main() {
    Oval a, b(34);
    a.set(1020);
    a.show();
    cout << b.getWidth() << "," << b.getHeight() << endl;
 
 
 
}
cs

 

 

10번

 

 

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <random>
using namespace std;
 
 
class Add {
    int a;
    int b;
 
public:
    void setValue(int a, int b) {
        this->= a;
        this->= b;
    }
    int calculate() {
        return a + b;
    }
 
};
 
class Sub {
 
    int a;
    int b;
 
public:
    void setValue(int a, int b) {
        this->= a;
        this->= b;
    }
    int calculate() {
        return a-b;
    }
 
};
 
class Mul {
 
    int a;
    int b;
 
public:
    void setValue(int a, int b) {
        this->= a;
        this->= b;
    }
    int calculate() {
        return a*b;
    }
 
};
 
class Div {
 
 
    int a;
    int b;
 
public:
    void setValue(int a, int b) {
        this->= a;
        this->= b;
    }
    int calculate() {
        return a/b;
    }
 
};
 
int main() {
    Add a;
    Sub s;
    Mul m;
    Div d;
 
    while (true) {
 
        cout << "두 정수와 연산자를 입력하세요>>" << endl;
        int x;
        int y;
        string str;
        cin >> x >> y  >> str;
        if (str == "+") {
            a.setValue(x, y);
            cout<<a.calculate()<<endl;
 
        }
        else if (str == "-") {
            s.setValue(x, y);
            cout<<s.calculate()<<endl;
        }
        else if (str == "*") {
            m.setValue(x, y);
            cout<<m.calculate()<<endl;
        }
        else if (str == "/") {
            d.setValue(x, y);
            cout<<d.calculate()<<endl;
        }
    }
}
cs

 

 

 

728x90
반응형

댓글