我正在研究一种算法,我正在尝试以下输出:
给定值/输入:
char *Var = "1-5,10,12,15-16,25-35,67,69,99-105";
int size = 29;
这里的“1-5”表示一个范围值,即它会被理解为“1,2,3,4,5”,而只有“,”的值是单独的值。
我正在编写一个算法,其中最终输出应该是这样的,它将给出完整的输出范围:
int list[]=1,2,3,4,5,10,12,15,16,25,26,27,28,29,30,31,32,33,34,35,67,69,99,100,101,102,103,104,105;
如果有人熟悉这个问题,那么非常感谢您的帮助。提前致谢!
我最初的代码方法是:
if(NULL != strchr((char *)grp_range, '-'))
{
int_u8 delims[] = "-";
result = (int_u8 *)strtok((char *)grp_range, (char *)delims);
if(NULL != result)
{
start_index = strtol((char*)result, (char **)&end_ptr, 10);
result = (int_u8 *)strtok(NULL, (char *)delims);
}
while(NULL != result)
{
end_index = strtol((char*)result, (char**)&end_ptr, 10);
result = (int_u8 *)strtok(NULL, (char *)delims);
}
while(start_index <= end_index)
{
grp_list[i++] = start_index;
start_index++;
}
}
else if(NULL != strchr((char *)grp_range, ','))
{
int_u8 delims[] = ",";
result = (unison_u8 *)strtok((char *)grp_range, (char *)delims);
while(result != NULL)
{
grp_list[i++] = strtol((char*)result, (char**)&end_ptr, 10);
result = (int_u8 *)strtok(NULL, (char *)delims);
}
}
但它只有在我有“0-5”或“0,10,15”时才有效。我期待着让它更加通用。