(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "ca-pub-8242763509535969", enable_page_level_ads: true }); [C++] Stack&Queue (예외처리, 소멸자, 템플릿 포함) :: 깜냥깜냥
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
#include<iostream>
using namespace std;
 
const int MAX = 10;
 
template <typename T>
class Memory {
 
protected:
    T *data;
    int top;
    int front;
    T a;
public:
    Memory() {
        data = new T[10];
        top = 0;
    }
 
    void push(T num) {
        if (top == MAX) {
            cout << "FULL!!" << endl;
            return;
        }
        else {
            data[top++] = num;
        }
    }
    virtual T pop() = 0;
    virtual ~Memory() {
        delete[]data;
        cout << "소멸자 함수" << endl;
    }
};
template <typename T>
class Stack :public Memory<T> {
public:
    T pop() {
        if (top == 0) {
            cout << "EMPTY!!!" << endl;
            return 0;
        }
        else {
            return data[--top];
        }
    }
};
 
template <typename T>
class Queue :public Memory<T> {
 
public:
    Queue() {
        front = 0;
    }
    T pop() {
        if (top == front) {
            cout << "EMPTY!!!" << endl;
            return 0;
        }
        else {
            if (front == MAX) {
                front = 0;
            }
            return data[(front++)];
        }
    }
};
 
void main() {
 
    Memory<double> *p;
    Stack<double> s;
    Queue<double> q;
    
 
    int num;
    double a;
 
    do {
        cout << "1.stack" << "\t" << "2.queue" << "\t" << "3.Quit" << endl;
        cin >> num;
 
        if (num == 1) {
            p = &s;
        }
        else if (num == 2) {
            p = &q;
 
        }
        else if (num == 3) {
            continue;
        }
        else {
            cout << "Not num" << endl;
            cout << "Bye" << endl;
            num = 3;
            continue;
        }
 
        cout << "1.push" << "\t" << "2.pop" << endl;
        cin >> num;
 
        if (num == 1) {
 
            cout << "Data input : ";
            cin >> a;
            p->push(a);
 
 
        }
        else {
            cout << " output Data : " << p->pop() << endl;
        }
    } while (num != 3);
}


'code > C++' 카테고리의 다른 글

[C++] 템플릿  (0) 2017.04.20
setw() 함수  (0) 2017.04.20
[c++] 연산자 함수  (0) 2017.04.20
[c++] private 상속  (0) 2017.04.20
[C++] protected 상속  (0) 2017.04.20

+ Recent posts