|
도르레 님이 쓰신 글 :
: void __fastcall TForm1::Button1Click(TObject *Sender)
: {
: int temp[9] = { 3, 2, 1, 5, 4, 7, 8, 0, 6 };
: int tempCount = 9;
: int hold=0,loop,i;
:
: for (loop = 0; loop<tempCount; loop++) {
: for (i = 0; i<tempCount-1; i++) {
: if (temp[i] > temp[i+1]) {
: hold = temp[i];
: temp[i] = temp[i+1];
: temp[i+1] = hold;
: printf("%d", temp[i]);
:
: }
: }
: }
:
: }
:
: 마지막에 버블정렬 된 거를 출력하고 printf문을 썼고... 여기서 Button1을 클릭하면 버블 정렬된 숫자를 나열해서 보여주려면 어떻게 해야 되나요?
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int temp[ 9 ] = { 3, 2, 1, 5, 4, 7, 8, 0, 6 };
int tempCount = 9;
int hold = 0, loop, i;
AnsiString _str; // 예제로 보시기위해 추가.
for ( loop = 0; loop < tempCount; loop++ )
{
for ( i = 0; i < tempCount - 1; i++ )
{
if ( temp[ i ] > temp[ i + 1 ] )
{
hold = temp[ i ];
temp[ i ] = temp[ i + 1 ];
temp[ i + 1 ] = hold;
_str += ( AnsiString )temp[ i ]; // 다른형태의 예제를 위해 추가
Memo1->Lines->Add( temp[ i ] ); // 메모장에 뿌릴때.
Panel1->Caption = _str; // Panel의 Caption에 뿌릴때
// ShowMessage( temp[i] ); // 창 형태로 값이 하나씩 나옴
}
}
}
}
//---------------------------------------------------------------------------
윗 분이 쉽게 설명해주셨지만,
쌩 초보일때 저는 못했던 기억이 있어서 조금 더 자세히 답변남깁니다.
위에있는건 단순히 값 표시만을 위한 예제입니다.
나머지는 공부해보세요.
|