//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- // µ¿Àû ¾î·¹ÀÌ Å¬·¡½º (Dynamic Array Class) // µ¿ÀûÀ¸·Î º¯ÇÏ´Â ¾ÆÀÌÅÛÀ» // // µ¨ÆÄÀÌÀÇ array of Çü; ¿¡ ´ëÀÀÇÏ¿© »ç¿ëÇصµ µÈ´Ù. // // // °£´ÜÇÑ ÇüÀº ¹«¸®¾øÀÌ ¾µ¼ö ÀÖÀ¸³ª, ´Ù¾çÇÑ µ¿ÀÛÀ» Çϴ Ŭ·¡½ºÀÎ °æ¿ì // ¼Ò½º¸¦ Àߺ¸°í Àû¿ëÇÒ °Í. // À¢¸¸ÇÑ ÇüÀº Àß µÉ °ÍÀÓ. template class DynaArray { private: T* t; bool bAutoClear; public: int Count; public: DynaArray(bool _bAutoClear = true) { t = NULL; Count = 0; bAutoClear = _bAutoClear; } ~DynaArray() { Clear(); } void Clear() { if (t) delete[] t; t = NULL; Count = 0; } void SetLength(int size) { if (size == 0) { Clear(); return; } T* t2 = new T[size]; if (t) { for(int c = 0; c < size && c < Count; c++) { t2[c] = t[c]; } // ¹è¿­ÀÌ Å©Áö´Â °æ¿ì´Â ¼ýÀÚÇüÀÎ °æ¿ì´Â ÀÚµ¿À¸·Î 0À¸·Î ä¿ì¸é ÁÁ´Ù. if (bAutoClear) { while(Count < size) { t2[Count++] = 0; } } delete[] t; } t = t2; Count = size; } T& operator[](int index) { return t[index]; } // int* p = ¿©±â¿¡ ´ëÀԵɶ§´Â ¹è¿­ÀÇ Ã¹¹ø° ¹øÁö°¡ ´ëÀÔµÇ°Ô ÇÑ´Ù. operator T*() { return t; } }; typedef DynaArray ArrayInt; typedef DynaArray ArrayUInt; typedef DynaArray ArrayByte; typedef DynaArray ArrayShort; typedef DynaArray ArrayUShort; // ±âŸ °£´ÜÇÑ ÇüÀº ¾Ë¾Æ¼­ Á¤ÀÇÇØ »ç¿ëÇÏ¸é µÊ. // ½ºÆ®¸µÇüÀ̳ª TDate Çü struct, classµî ÀÚüÀûÀ¸·Î ÃʱâÈ­¸¦ ÇÑ °æ¿ì´Â 0À» ´ëÀÔÇÏ¸é ¾ÈµÉ¼ö ÀÖÀ¸¹Ç·Î ÀÌ·¸°Ô ÇüÀ» ´Ù½Ã Á¤ÀÇÇÑ´Ù. template class DynaArray2 : public DynaArray { public: DynaArray2() : DynaArray(false) {} }; typedef DynaArray2 ArrayString; typedef DynaArray2 ArrayTTime; // ±âŸ º¹ÀâÇÑ ÇüÀº ¾Ë¾Æ¼­ Á¤ÀÇÇØ »ç¿ëÇÏ¸é µÊ. // Å×½ºÆ®... void __fastcall TForm1::Button1Click(TObject *Sender) { ArrayInt aa; aa.SetLength(10); for(int c = 0; c < aa.Count; c++) { aa[c] = c; } aa.SetLength(5); for(int c = 0; c < aa.Count; c++) { Memo1->Lines->Add(aa[c]); } Memo1->Lines->Add(""); aa.SetLength(8); for(int c = 0; c < aa.Count; c++) { Memo1->Lines->Add(aa[c]); } Memo1->Lines->Add(""); ArrayString ss; ss.SetLength(10); for(int c = 0; c < ss.Count; c++) { ss[c] = String().sprintf("%d ¹ø Ç׸ñÀÓ´Ù.", c); } for(int c = 0; c < ss.Count; c++) { Memo1->Lines->Add(ss[c]); } Memo1->Lines->Add(""); ss.SetLength(5); for(int c = 0; c < ss.Count; c++) { Memo1->Lines->Add(ss[c]); } Memo1->Lines->Add(""); ss.SetLength(7); for(int c = 0; c < ss.Count; c++) { Memo1->Lines->Add(ss[c]); } Memo1->Lines->Add("¹®ÀÚ¿­Àº Å©±â¸¦ ´ÃÀ̸é ÀÚµ¿ ºó¹®ÀÚ¿­·Î ÇÒ´çµÈ´Ù."); } //---------------------------------------------------------------------------