天天看點

數字字元串+逗号解析排序

#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;

}

c++

繼續閱讀