C++

[λͺ…ν’ˆ c++] 8μž₯ μ—°μŠ΅λ¬Έμ œ

Meaning_ 2022. 12. 5. 10:06
728x90
λ°˜μ‘ν˜•

πŸ₯‘ λ“€μ–΄κ°€κΈ° 전에

 

- 8μž₯은 상속에 λŒ€ν•œ λ‚΄μš© , μ›¬λ§Œν•˜λ©΄ public으둜 νŒŒμƒν΄λž˜μŠ€μ— μƒμ†ν•΄μ£Όμž!

- MyQueue::MyQueue(int capacity):BaseArray(capacity) 

-> μ΄λŸ°μ‹μ˜ μƒμ„±μž 상속(?) 잘 써주기 μœ μš©ν•˜λ‹€!

 

 

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
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<cmath>
#include <ctime>
#include<algorithm>
 
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 head, tail;
    int size;
 
public:
    MyQueue(int capacity);
    void enqueue(int num);
    int capacity();
    int dequeue();
    int length();
};
 
 
MyQueue::MyQueue(int capacity):BaseArray(capacity) {
 
    head = 0;
    tail = -1;
    size = 0;
 
}
 
void MyQueue::enqueue(int num) {
    if (size == capacity()) {
        return;
    }
    put(head, num);
    head++;
    head = head % capacity();
    size++;
}
 
int MyQueue::dequeue() {
    if (size == 0) {
        return -1;
    }
    tail++;
    size--;
    tail = tail % capacity();
    return get(tail);
}
 
int MyQueue::capacity() {
    return getCapacity();
}
 
int MyQueue::length() {
    return size;
}
 
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;
 
}
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
111
112
113
#include <iostream>
#include <string>
#include<cmath>
#include <ctime>
#include<algorithm>
 
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 tos;//top of stack
public:
    MyStack(int capacity);
    void push(int num);
    int capacity();
    int length();
    int pop();
    
};
 
MyStack::MyStack(int capacity) :BaseArray(capacity) {
 
    tos = 0// λ‹€μŒ ν‘Έμ‰¬ν•  μœ„μΉ˜, ν¬κΈ°λ‘œ λ‹€λ€„도 λ¨
 
}
 
void MyStack::push(int num) {
    if (tos == capacity()) {
        return;
    }
 
    put(tos, num);
    tos++;
    
 
}
 
int MyStack::capacity() {
 
    return getCapacity();
 
}
 
int MyStack::length() {
 
    return tos;
 
}
 
int MyStack::pop() {
 
    if (tos == 0) {
        return -1;
    }
    
    tos--;
    return get(tos);
 
}
 
 
 
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;
 
    
 
}
cs
 
728x90
λ°˜μ‘ν˜•