C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[893] 파일명 or 날짜순 정렬 and 이진검색(binary search)
박영목.월천 [gsbsoft] 9876 읽음    2009-05-20 17:56
1. 지정한 폴더에 있는 파일들만 몽땅 가져오기...     날짜, 파일명
폴더는 제 작업에 필요없어서 제외... 혹 할려면 폴더를 StringList에 넣고 재귀호출 그
냥 재귀호출 해도 되는데.. 정렬문제 때문에 저번에그렇게 한 것 같습니다.  원하는데로 되었음..

2. FileName으로 정렬, Date로 정렬

3. 파일명 binary 검색... 정확한 파일이 있는지 확인을 위한 것으로...
   정확하지 않으면... -1을 Return, 조금 수정하면 근접한 파일도 찾을 수 있을 것입니다.
   이것은 몇 년 전에 필요해서 한 것 같은데..   생각 안남.... ㅋㅋㅋ 

                               끝....  부산에서 월천 박영목....  행복하세요.....   바바 ^^

-------------------------------------------------------------------------------------------


typedef struct ST_FILE_DATE
{
  FILETIME   ftDate;     
  AnsiString asFile;
} stFileDate;


void __fastcall QSortFileName( TList *sl, int left, int right )                 //TStringList *sl, int left, int right )
{
  int i, j;

  stFileDate *sPx, *sPw;
  stFileDate *p, *q;

  do
  {
    p = (stFileDate*)sl->Items[ (left+right)/2 ];                               //sPx = sl->Strings[ (left+right)/2 ];

    i = left;
    j = right;
    do
    {
      //while( strcmp( sl->Strings[i].c_str(), sPx.c_str()  ) < 0 )  i++;
      while( i<=right )                                                         //while( sl->   p-> ) i++;   //while( strcmp( sl->Strings[i].c_str(), sPx.c_str()  )  < 0 ) i++;          //sl->Strings[i].c_str(),  <  sPx.c_str()
      {
        q = (stFileDate*)sl->Items[ i ];
                                                                                //if( q->ftDate < p->ftDate ) i++;
        if( strcmp( q->asFile.c_str(), p->asFile.c_str() ) < 0 )                      //sl->Strings[i].c_str(), sPx.c_str()  ) < 0 )                //(q->ftDate.dwHighDateTime < p->ftDate.dwHighDateTime) || ((q->ftDate.dwHighDateTime == p->ftDate.dwHighDateTime) && (q->ftDate.dwLowDateTime < p->ftDate.dwLowDateTime)) )
        {
          i++;
        }
        else break;
      }

      //while( strcmp( sl->Strings[j].c_str(), sPx.c_str()   ) > 0 ) j--;

      while( 0<=j )
      {
        q = (stFileDate*)sl->Items[ j ];
                                                                                //if( q->ftDate > p->ftDate ) j--;
        if( strcmp( q->asFile.c_str(), p->asFile.c_str() ) > 0 )                      //if( (q->ftDate.dwHighDateTime > p->ftDate.dwHighDateTime) || ((q->ftDate.dwHighDateTime == p->ftDate.dwHighDateTime) && (q->ftDate.dwLowDateTime > p->ftDate.dwLowDateTime)) )
        {
          j--;
        }
        else break;
      }


      if( i>j ) break;

      sPw = (stFileDate*)sl->Items[ i ];
      sl->Items[i] = sl->Items[j];
      sl->Items[j] = sPw;

    } while( ++i <= --j );

    if( j - left < right - i )
    {
      if( left < j ) QSortFileName( sl, left, j );
      left = i; j = right;
    }
    else
    {
      if( i < right ) QSortFileName( sl, i, right );
      right = j; i = left;
    }
  }
  while( left < right );
}


void __fastcall QSortDate( TList *sl, int left, int right )
{
  int i, j;
  stFileDate *sPx, *sPw;
  stFileDate *p, *q;

  do
  {
    p = (stFileDate*)sl->Items[ (left+right)/2 ];                               //sPx = sl->Strings[ (left+right)/2 ];

    i = left;
    j = right;
    do
    {
      while( i<=right )                                                         //while( sl->   p-> ) i++;   //while( strcmp( sl->Strings[i].c_str(), sPx.c_str()  )  < 0 ) i++;          //sl->Strings[i].c_str(),  <  sPx.c_str()
      {
        q = (stFileDate*)sl->Items[ i ];
                                                                                //if( q->ftDate < p->ftDate ) i++;
        if( (q->ftDate.dwHighDateTime < p->ftDate.dwHighDateTime) || ((q->ftDate.dwHighDateTime == p->ftDate.dwHighDateTime) && (q->ftDate.dwLowDateTime < p->ftDate.dwLowDateTime)) )
        {
          i++;
        }
        else break;
      }

      while( 0<=j )
      {
        q = (stFileDate*)sl->Items[ j ];
                                                                                //if( q->ftDate > p->ftDate ) j--;
        if( (q->ftDate.dwHighDateTime > p->ftDate.dwHighDateTime) || ((q->ftDate.dwHighDateTime == p->ftDate.dwHighDateTime) && (q->ftDate.dwLowDateTime > p->ftDate.dwLowDateTime)) )
        {
          j--;
        }
        else break;
      }

      //while( strcmp( sl->Strings[j].c_str(), sPx.c_str()  )  > 0 ) j--;         //sl->Strings[j].c_str(),  >  sPx.c_str()

      if( i>j ) break;

      sPw = (stFileDate*)sl->Items[ i ];
      sl->Items[i] = sl->Items[j];
      sl->Items[j] = sPw;

    } while( ++i <= --j );

    if( j - left < right - i )
    {
      if( left < j ) QSortDate( sl, left, j );
      left = i; j = right;
    }
    else
    {
      if( i < right ) QSortDate( sl, i, right );
      right = j; i = left;
    }
  } while( left < right );
}
//------------------------------------------------------------------------------


static int BinarySearch( TList *sl, int max, AnsiString asValue )
{
  int pos;
  int begin = 0;
  int end   = max - 1;
  int cond  = 0;
  stFileDate *p;

  while(begin <= end)
  {
    pos = (begin + end) / 2;

    p = (stFileDate*)sl->Items[ pos ];

    if( (cond = strcmp( p->asFile.c_str(), asValue.c_str()))==0 )
      return pos;
    else if(cond < 0)
           begin = pos + 1;
         else
           end   = pos - 1;
  }

  return -1;
}

+ -

관련 글 리스트
893 파일명 or 날짜순 정렬 and 이진검색(binary search) 박영목.월천 9876 2009/05/20
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.