//실습 1번
#include<iostream>
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 n);
int dequeue();
int capacity();
int length();
void list();
int getTail() { return tail; }
};
void MyQueue::list() {
int index = getTail();
for (int i = 0; i < length(); i++) {
index++;
index = index % capacity();
cout << get(index) << " ";
}
}
MyQueue::MyQueue(int capacity) : BaseArray(capacity) {
head = 0; //삽입할 위치는 head
tail = -1;//tail+1 위치에서 꺼내기
size = 0; // 초기 개수는 0
}
void MyQueue::enqueue(int n) {//삽입하는 함수
if (size == capacity()) {
cout << "삽입할 수 없습니다! " << endl;
return; //queue full error
}
put(head, n); //put함수 호출. mem[head] = n
head++;
head = head % capacity();
size++;
//cout << "size ; " << size << endl;
}
int MyQueue::dequeue() { //인출하는 함수
if (size == 0) return -1;
size--;
tail++;
tail = tail % capacity();
cout << get(tail) << endl;
return get(tail); //mem[tail] 리턴
}
int MyQueue::capacity() { //capacity 리턴 함수
return getCapacity();
}
int MyQueue::length() { //size 리턴 함수
return size;
}
int main() {
cout << "IT공학부 17122861 김한나 " << endl << "실습 1번" << endl << endl;
int num, capacity, s, index;
cout << "용량 입력 >> ";
cin >> capacity;
MyQueue mQ(capacity);
while (1) {
cout << "1) 삽입 2) 인출 3) 전체보기 4)끝내기 " << endl;
int n;
cin >> n; //삽입, 인출, 전체보기, 끝내기 중 선택
switch (n) {
case 1:
cout << "삽입할 수 입력>> ";
cin >> num;
mQ.enqueue(num);
break;//큐에 삽입
case 2:
s = mQ.dequeue();
if (s == -1) cout << "인출할 수 없습니다! " << endl;
else cout << s << " 인출했습니다!" << endl;
break;//인출
case 3: {
mQ.list();
cout << endl;
break; }
case 4: cout << "종료 합니다아아아아" << endl << endl; return 0; //종료
default: cout << "잘못된 입력입니당 다시 입력하세욤" << endl; break;
}
cout << endl << endl << "===================================================================" << endl << endl;
}
}
C++ 상속받는 문제!
'Programming > C & C++' 카테고리의 다른 글
[c 언어] 포인터와 call by address : 실제 값에 영향 있는 경우와 없는 경우! (0) | 2021.03.27 |
---|---|
[C언어] 포인터 문자열 VS 배열 문자열 (같지 않다!) (0) | 2021.02.25 |
[C#] 입출력과 데이터 (0) | 2020.10.07 |
[C++] 다양한 별 찍기 문제 연습 (0) | 2020.07.26 |
[C++] c++ 시작하기 : cout, cin, namespace, 오버로딩 (0) | 2020.05.18 |