下のプログラム例を参考にして、10個以上の文字列の バブルソートを行いながらその様子をウィンドウ上に表示する プログラムを作成してください。 なお下の例は、ウィンドウ上で文字列が並べ変えられる様子を ウィンドウ上に表示するものです。
ヒント
●文字列の比較(辞書並び順)を行う関数は strcmp() である。
●秒単位での時間待ちを行う関数は sleep() である。
-----------------------
#include <stdio.h>
#include <Xtc.h>
char *text[10]
= { "Programming ", "UNIX ", "C ", "language ", "TextEditor ",
"AnswerBook ", "OpenWindow ", "keyboard ", "mail ", "Display " };
char *index[10];
void re_disp(void)
{
int i;
cleardevice(); /* 画面の消去 */
for( i = 0; i < 10; i++ ) /* 文字列の表示 */
outtextxy( 100, i * 30 + 50, index[ i ] );
xflush();
sleep(1); /* 時間待ち(秒単位) */
return;
}
void main(void)
{
int i,j,n;
char *tmp;
for( i = 0; i < 10; i++ )
index[ i ] = text[ i ];
initgraph();
re_disp();
for( i = 0; i < 2; i++ ){ /* 文字列の順番の並べ替え(一番下 */
for( j = 9; j > 0; j-- ){ /* 下から順に一番上へ持ってくる) */
tmp = index[ j - 1 ];
index[ j - 1 ] = index[ j ];
index[ j ] = tmp;
re_disp(); /* 並べ替え結果を表示する */
}
}
xtcmainloop(3);
closegraph();
}
-----------------------