从字符串中提取数字/数字范围的复杂算法 [英] Complex algorithm to extract numbers/number range from a string

查看:38
本文介绍了从字符串中提取数字/数字范围的复杂算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种算法,我正在尝试以下输出:

I am working on a algorithm where I am trying the following output:

给定值/输入:

char *Var = "1-5,10,12,15-16,25-35,67,69,99-105";int size = 29;

这里1-5"描述了一个范围值,即它会被理解为1,2,3,4,5""," 是单个值.

Here "1-5" depicts a range value, i.e. it will be understood as "1,2,3,4,5" while the values with just "," are individual values.

我正在编写一个算法,其中最终输出应该是这样的,它将提供完整的输出范围:

I was writing an algorithm where end output should be such that it will give complete range of output as:

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 anyone is familiar with this issue then the help would be really appreciated. Thanks in advance!

我最初的代码方法是:

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"时它才有效.我期待让它变得更加通用.

But it only works if I have either "0-5" or "0,10,15". I am looking forward to make it more versatile.

推荐答案

您的问题似乎是误解了 strtok 的工作原理.看看这个.

You're issue seems to be misunderstanding how strtok works. Have a look at this.

#include <string.h>
#include <stdio.h>

int main()
{
        int i, j;
        char delims[] = " ,";
        char str[] = "1-5,6,7";
        char *tok;
        char tmp[256];
        int rstart, rend;

        tok = strtok(str, delims);

        while(tok != NULL) {
                for(i = 0; i < strlen(tok); ++i) {
                        //// range
                        if(i != 0 && tok[i] == '-') {
                                strncpy(tmp, tok, i); 
                                rstart = atoi(tmp);
                                strcpy(tmp, tok + i + 1); 
                                rend = atoi(tmp);
                                for(j = rstart; j <= rend; ++j)
                                        printf("%d\n", j); 
                                i = strlen(tok) + 1;
                        }   
                        else if(strchr(tok, '-') == NULL)
                                printf("%s\n", tok);
                }   

                tok = strtok(NULL, delims);
        }   

        return 0;
}

这篇关于从字符串中提取数字/数字范围的复杂算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆