Last active
August 29, 2015 14:20
-
-
Save jiunbae/de99f77d45edef5a6351 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
정수를 받아들여 오름차순으로 보관하는 클래스 구현 | |
1. pseudo code를 포함한 어떤 언어를 사용하든 상관 없음 | |
2. 정확한 문법은 무시해도 좋고 논리만 알아볼 수 있으면 됨 | |
3. 클래스 이름 : xArray | |
4. 구현해야 할 멤버 함수 | |
bool Seek(int i) : 주어진 인자의 정수가 있는지 확인, 중복 보관 없음 | |
bool Add(int i) : 새로운 정수를 보관, 위의 Seek 함수 이용 | |
bool Del(int i) : 주어진 정수를 제거, 위의 Seek 함수 이용 | |
5. 그 외 필요한 멤버 변수나 함수 등은 제한 없음 | |
*/ | |
#include<stdlib.h> | |
#include<iostream> | |
using namespace std; | |
class xArray{ | |
private: | |
int _size; | |
int * ary; | |
void _realloc(int __size) | |
{ | |
ary = (int*)realloc(ary, __size*sizeof(int) + sizeof(int)); | |
ary[__size] = 0; | |
} | |
void _push_at(int i) | |
{ | |
for (int index = _size; index > (i < 0 ? 0 : i ); index--) | |
ary[index] = ary[index - 1]; | |
} | |
void _pull_at(int i) | |
{ | |
for (int index = i; index < _size; index++) | |
ary[index] = ary[index + 1]; | |
} | |
public: | |
xArray() | |
{ | |
ary = (int*)malloc(sizeof(int)); | |
} | |
~xArray() | |
{ | |
free(ary); | |
} | |
bool Seek(int i) | |
{ | |
for (int index = 0; index<_size; index++) | |
if (ary[index] == i) | |
return true; | |
return false; | |
} | |
bool Add(int i) | |
{ | |
if (Seek(i)) | |
return false; | |
_realloc(++_size); | |
for (int index = 0; index < _size; index++) | |
if (ary[index]>i) | |
{ | |
_push_at(index); | |
ary[index] = i; | |
return true; | |
} | |
ary[_size] = i; | |
return true; | |
} | |
bool Del(int i) | |
{ | |
if (!Seek(i)) | |
return false; | |
for (int index = 0; index < _size; index++) | |
if (ary[index] == i) | |
{ | |
_pull_at(index); | |
_realloc(--_size); | |
break; | |
} | |
return true; | |
} | |
int size() | |
{ | |
return _size; | |
} | |
void show() | |
{ | |
for (int index = 1; index <= _size; index++) | |
printf("%d ", ary[index]); | |
printf("\n"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment