C,C++

[C++] Dynamic binding~Stack&Queue

너래쟁이 2018. 3. 15. 09: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
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
using namespace std;
 
class Memory
{
public:
    int *p;
    int i;
 
    Memory()
    {
        p = new int[20];
        i = -1;
    }
    ~Memory()
    {
        delete[] p;
    }
 
    void push()
    {
        if(i<19)
        {
            i++;
            cout<<"Input : ";
            cin >> p[i];
        }
        else
            cout<<"Full";
    }
    virtual void pop() = 0;
    void printAll()
    {
        for(int j = 0; j <= i; j ++)
        {
            cout<<j+1<<"번째 공간의 값 : "<<p[j]<<"\n";
        }
    }
};
 
class MyQueue : public Memory
{
public:
    MyQueue(){}
    virtual void pop()
    {
        if(i<0)
        {
            cout<<"NUll!!"<<endl;
        }
        else if(i>=0)
        {
            cout<<"삭제할 정보 : "<<p[i]<<endl;
            p[i]=NULL;    
            i--;
        }
 
 
    }
 
};
 
class MyStack : public Memory
{
public:
    MyStack(){}
    virtual void pop()
    {
        if(i<0)
        {
            cout<<"큐에 삭제할 정보가 없습니다!!"<<endl;
        }
        else if(i>=0)
        {
            cout<<"삭제할정보 : "<<p[0]<<endl;
            for(int j=0;j<i;j++)
            {
                p[j]=p[j+1];
            }
            i--;
        }
    }
};
 
void main()
{
    int num;
    int select = 0;
 
    Memory *p_memory;
 
    MyStack ms;
    MyQueue mq;
 
    while(1)
    {
        cout<<"1. Stack 2. Queue"<<endl;
        cin>>num;
 
        switch(num)
        {
            case 1:
                p_memory = &ms;
                while(1)
                {
                    cout<<"1.push 2.pop 3.출력 4.돌아가기"<<endl;
                    cin>>select;
                    
                    if(select == 1)
                    {
                        p_memory->push();
                    }
                }
        }
    }
}
 
cs