#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include
//#include <stdafx.h>
void Calculate(const charinput, const char output) {
int array[128] = { 0 };
int itmp = 0;
size_t iIndex = 0;
char tmp[128] = { 0 };
int temp = 0;
if (input == NULL)
return;
if (output == NULL)
return;
//分割字符串到整型数组
while (*input!=’\0’)
{
if (*input != ‘,’) {
tmp[itmp] = *input;
itmp++;
}
else if(*input==’,’){
array[iIndex] = atoi(tmp);
memset(tmp, 0, sizeof(tmp));
itmp = 0;
iIndex++;
}
input++;
}
array[iIndex] = atoi(tmp);//数组生成
iIndex++;
//冒泡排序
for (size_t i = 0; i < iIndex; i++) {
for (size_t j = i+1; j < iIndex; j++) {
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
//转化字符串输出
char convertbuff[128] = { 0 };
size_t ilen = 0;
for (size_t i = 0; i < iIndex; i++) {
if (i == 0)
sprintf_s(convertbuff, “%d”, array[i]);
else
sprintf_s(convertbuff, “,%d”, array[i]);
memcpy((void *)(output + ilen), convertbuff, strlen(convertbuff));
ilen += strlen(convertbuff);
}
}
int main()
{
char c[] = “-1,1,2,10”;
char coef[128] = { 0 };
Calculate("-1,10,1,2", coef);
std::cout << strcmp(c,coef)<< std::endl;
}
数字字符串+逗号解析排序
最新推荐文章于 2023-04-14 13:56:56 发布