程序如下,编译工具是VC6++:
#include <stdio.h>
#include <string.h>
#include <time.h>
/*macro defintion*/
#define ARRAY_MAX_NUM 50
#define RAND_MIN_NUM 100
#define RAND_MAX_NUM 200
/*globle value*/
int a[ARRAY_MAX_NUM];
int DIGITSORT_DataProduct(void)
{
int i;
srand(time(NULL));
for(i=0; i<ARRAY_MAX_NUM; i++)
{
a[i] = RAND_MIN_NUM+rand()%(RAND_MAX_NUM-RAND_MIN_NUM+1);
}
printf("Rand data print:\n");
for(i=0; i<ARRAY_MAX_NUM; i++)
{
if((i != 0) && (i%10 == 0))
{
printf("\n");
}
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
int DIGITSORT_max_a(int *a, int s, int e)
{
int s32MaxDigit, s32MaxSubscript;
int i;
s32MaxDigit = a[s];
s32MaxSubscript = s;
for(i=(s+1); i<(e-s+1); i++)
{
if(a[i] > s32MaxDigit)
{
s32MaxDigit = a[i];
s32MaxSubscript = i;
}
}
printf("Max digit is: %d, subscript is %d\n", s32MaxDigit, s32MaxSubscript);
return 0;
}
int DIGITSORT_sum_a(int *a)
{
int i, s32Sum = 0;
for(i=0; i<ARRAY_MAX_NUM; i++)
{
s32Sum += a[i];
}
printf("Array-a sum is %d\n", s32Sum);
return 0;
}
int DIGITSORT_asc(int *a)
{
int i, j, tmp;
for(i=0; i<ARRAY_MAX_NUM; i++)
{
tmp = a[i];
for(j=i+1; j<ARRAY_MAX_NUM; j++)
{
if(tmp > a[j])
{
a[i] = a[j];
a[j] = tmp;
tmp = a[i];
}
}
}
printf("Asc data print:\n");
for(i=0; i<ARRAY_MAX_NUM; i++)
{
if((i != 0) && (i%10 == 0))
{
printf("\n");
}
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
int DIGITSORT_desc(int *a)
{
int i, j, tmp;
for(i=0; i<ARRAY_MAX_NUM; i++)
{
tmp = a[i];
for(j=i+1; j<ARRAY_MAX_NUM; j++)
{
if(tmp < a[j])
{
a[i] = a[j];
a[j] = tmp;
tmp = a[i];
}
}
}
printf("Desc data print:\n");
for(i=0; i<ARRAY_MAX_NUM; i++)
{
if((i != 0) && (i%10 == 0))
{
printf("\n");
}
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
int main()
{
int s32StartSub, s32EndSub;
char s32Sortord;
DIGITSORT_DataProduct();
printf("Please input start subscript and end subscript\n");
scanf("%d %d", &s32StartSub, &s32EndSub);
DIGITSORT_max_a(a, s32StartSub, s32EndSub);
DIGITSORT_sum_a(a);
while(1)
{
getchar(); /*get '\n'*/
printf("Please input sort order, letter a means asc b means desc\n");
scanf("%c", &s32Sortord);
if(s32Sortord == 'a')
DIGITSORT_asc(a);
else if(s32Sortord == 'd')
DIGITSORT_desc(a);
else
printf("Please input correct\n");
}
return 0;
}
输出结果:
以上,