|

楼主 |
发表于 2003-9-21 18:04:33
|
显示全部楼层
谢谢各位~~我自己已经写的差不多了~。这是一个排序的程序,结果是正确的,只是有些行为我不太明白
完整代码如下:
#include <stdlib.h>
#include <stdio.h>
int checkData (char char_to_check[]);
int comp (const void *arg1, const void *arg2);
int main (int argc, char *argv[]){
char str[10]; //save the orginal input data
int num[100];
int count = 0;
int i;
printf("please enter the data,and non_numeric data to quit~!\n");
while (count < 100){
printf("enter the data_%d:\n",count+1);
scanf("%s\n",&str);
if (checkData(str) == -1)
break;
num[count++] = atoi(str);
}
qsort(&num,count+1,sizeof(int),comp);
printf("the result is:\n");
for (i = 0; i<count; i++)
printf("%d ",num);
printf("\n");
exit(0);
}
int checkData(char char_to_check[]){
int i;
for (i = 0; i<strlen(char_to_check); i++)
if (!(char_to_check>'0' && char_to_check<'9'))
return -1;
return 0;
}
int comp (const void *arg1, const void *arg2){
return *(int *)arg1-*(int *)arg2;
}
运行结果如下:
please enter the data,and non_numeric data to quit~!
enter the data_1:
1
2
enter the data_2:
3
enter the data_3:
4
enter the data_4:
5
enter the data_5:
3
enter the data_6:
6
enter the data_7:
7
enter the data_8:
q
enter the data_9:
q
the result is:
1 2 3 3 4 5 6 7 |
|