把副程式 移到主程式的 後面
(高顯忠, sjgau4311@gmail.com, 2010-12-23 15:16)
1樓
#include <cstdlib>
#include <iostream>
// --------------------------------------------------------
#include <time.h>
// --------------------------------------------------------
using namespace std;
// --------------------------------------------------------
void swap_int(int *a, int *b);
template <class T> void swap2(T &a, T &b);
void gen_data(int a[], int n, int i1, int i2, int ctrl_code);
void show_data(int a[], int n);
void bubble_sort(int a[], int n);
void check_data(int a[], int n);
// --------------------------------------------------------
int main(int argc, char *argv[])
{
int a[1024], n, i1, i2, ctrl_code;// 1K * 4 Bytes, --> 4K
n= 10;
i1= 10;
i2= 99;
ctrl_code= -123;
gen_data(a, n, i1, i2, ctrl_code);
show_data(a, n);
// ------------------------------------------
bubble_sort(a, n);
printf("\n *** after the bubble_sort(), \n");
system("pause");
check_data(a, n);
show_data(a, n);
return EXIT_SUCCESS;
}// end of main()
// ----------------------------------------------
// swap_int( &i1, &i2);
void swap_int(int *a, int *b)
{
int temp;
temp= (*a);
(*a)= (*b);
(*b)= temp;
}// end of swap_int()
// --------------------------------------------------------
// swap2( i1, i2);
template <class T> void swap2(T &a, T &b)
{
T temp;
temp= a;
a= b;
b= temp;
}// end of swap2()
// --------------------------------------------------------
// gen_data( a, n, 10, 99, 123);
void gen_data(int a[], int n, int i1, int i2, int ctrl_code)
{
int i;
// must i1 <= i2
if (i1 > i2) {
swap2(i1, i2);
}
// --------------------------------------------------------
if (ctrl_code <= 0) {// set srand()
srand((unsigned) time(NULL));
}
else {// used the ctrl_code to seed
srand(ctrl_code);
}
for (i=0;i<n;i++) {
a[i]= (rand()%(i2 - i1 + 1)) + i1;
}
}// end of gen_data()
// --------------------------------------------------------
// show_data( a, n);
void show_data(int a[], int n)
{
int i;
printf("\n show data following: \n");
for (i=0;i<n;i++) {
printf("%3d, ", a[i]);
}
printf("\n");
system("pause");
}// end of show_data()
// --------------------------------------------------------
// bubble_sort( a, n);
void bubble_sort(int a[], int n)
{
int i, j, k, flag, m;
// do n times
m= (n-1) - 1;
for (i=0;i<n;i++) {
printf("\n i= %d, m= %d\n", i, m);
system("pause");
flag= 0;
for (j=0;j<=m;j++) {
k= j + 1;
// we hope, a[j] <= a[k]
if (a[j] > a[k]) {
printf("\n swap data of a[%d], a[%d], \n", j, k);
printf(" a[j]= %d, a[k]= %d\n", a[j], a[k]);
system("pause");
swap2(a[j], a[k]);
flag= 1;
}
}
show_data(a, n);
if (flag == 0) break;
m--;
}
}// end of bubble_sort()
// --------------------------------------------------------
// check_data( a, n);
void check_data(int a[], int n)
{
int i, j;
for (i=0;i<(n-1);i++) {
j= i + 1;
// we hope, a[i] <= a[j]
if (a[i] > a[j]) {
printf("\n *** error in check_data(), \n");
printf("a[i]= %d, a[j]= %d\n", a[i], a[j]);
system("pause");
}
}
printf("\n *** check OK!\n");
system("pause");
}// end of check_data()