这个可以自己解析分析字串的,以下是我写的(充分利用标准库),你参考下(数字可任意个,任意位)
#include
#include
#include
int comp(const void *a,const void *b) //排序比较函数
{
return (*(int *)a)-(*(int *)b);
}
int main()
{
char s[1024],*p,*t;
int a[20],i,n=0;
gets(s); //读入整个串,包括空格
p=s;
while(*p++!='['); //忽略"["前所有字符
t=p;
while(*t && *t++!=']'); //找到"]"
*(t-1)='\0'; //忽略"]"及后所有字符
t=strtok(p,","); //以逗号分割串
do
{
sscanf(t,"%d",&a[n++]); //字串转数值
}while(t = strtok(NULL, ","));
qsort(a,n,sizeof(int),comp);
printf("[%d",a[0]); //先输出一个
for(i=1;i
printf(",%d",a[i]); //输出所有数
printf("]\n");
return 0;
}
测试结果