题目:输入一组数值 期间用逗号隔开, 按大小排序后 同样用逗号隔开输出。
#include
#include
void Ch_Number(int *Dest, char *Str)
{
char TmpBuf[5] = {0};
int LenStr = 0, i = 0, j = 0, k = 0;
int TmpValue = 0;
if(NULL == Dest || NULL == Str)
{
return;
}
LenStr = strlen(Str);
for(i = 0; i <= LenStr; i++)
{
if(',' == *(Str + i) || i >= (LenStr))
{
j = 0;
*(Dest + k) = atoi(TmpBuf);
memset(TmpBuf, 0x0, 5);
k++;
}
else
{
TmpBuf[j] = *(Str + i);
j++;
}
}
return;
}
void Num_Char(char *Dest, int *Str)
{
char *tmp = NULL;
char Svalue = 0, char_value = 0, k = 0, j = 0;
if(NULL == Dest)
{
return;
}
tmp = Dest;
while('\0' != *(Str + k))
{
Svalue = (char)*(Str + k);
while(Svalue)
{
char_value = ((Svalue%10) + '0');
sprintf((tmp + j),"%c",char_value);
Svalue = Svalue/10;
j++;
}
sprintf((tmp + j),"%c", ',');
j++;
k++;
}
return;
}
void FindMaxNumber(int *Dest)
{
int *tmp = NULL;
int LenStr = 0, k = 0, j = 0;
int TmpValue = 0;
if(NULL == Dest)
{
return;
}
tmp = Dest;
while('\0' != *(tmp + k))
{
TmpValue = *(tmp + k);
j = k;
while('\0' != *(tmp + j))
{
if(TmpValue < *(tmp + j))
{
*(tmp + k) = *(tmp + j);
*(tmp + j) = TmpValue;
TmpValue = *(tmp + k);
}
j++;
}
k++;
}
return;
}
int main()
{
char OutBuff[28] = {0},InBuff[28] = {0};
int TmpBuff[28] = {0};
printf("Please Input Number:");
scanf("%s", InBuff);
//将字符转换成数字
Ch_Number(TmpBuff, InBuff);
//冒泡排序
FindMaxNumber(TmpBuff);
//将数字转换成字符
Num_Char(OutBuff, TmpBuff);
printf("%s", OutBuff);
}
阅读(1532) | 评论(0) | 转发(0) |