b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

c 链表的定义和使用 cjson 源码阅读笔记(8)

电脑杂谈  发布时间:2017-12-31 09:23:46  来源:网络整理

数字解析需要考虑科学计数法, 即大概形式如下图

number-gif

/* Parse the input text to generate a number, and populate the result into item. */
static const char *parse_number(cJSON *item,const char *num) {
    double n=0,sign=1,scale=0;
    int subscale=0,signsubscale=1;
    if (*num=='-') sign=-1,num++;/* Has sign? */
    if (*num=='0') num++;/* is zero */
    if (*num>='1' && *num<='9')don=(n*10.0)+(*num++ -'0');
    while (*num>='0' && *num<='9');/* Number? */
    if (*num=='.' && num[1]>='0' && num[1]<='9') {
        num++;   /* Fractional part? */
        don=(n*10.0)+(*num++ -'0'),scale--;
        while (*num>='0' && *num<='9');
    }
    if (*num=='e' || *num=='E') {/* Exponent? */
        num++;
        if (*num=='+') num++;
        else if (*num=='-') signsubscale=-1,num++;/* With sign? */
        while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0');/* Number? */
    }
    n=sign*n*pow(10.0,(scale+subscale*signsubscale));/* number = +/- number.fraction * 10^+/- exponent */
    item->valuedouble=n;
    item->valueint=(int)n;
    item->type=cJSON_Number;
    return num;
}

解析数组, 需要先遇到 '[' 这个符号, 然后挨个的读取节点内容, 节点使用 ',' 分隔, ',' 前后还可能有空格, 最后以 ']' 结尾。

我们要编写的也是这样。

先创建一个数组对象, 判断是否有儿子, 有的话读取第一个儿子, 然后判断是不是有 逗号, 有的话循环读取后面的儿子。

最后读取 ']' 即可。

/* Build an array from input text. */
static const char *parse_array(cJSON *item,const char *value) {
    cJSON *child;
    if (*value!='[') {
        ep=value;    /* not an array! */
        return 0;
    }
    item->type=cJSON_Array;
    value=skip(value+1);
    if (*value==']') return value+1;/* empty array. */
    item->child=child=cJSON_New_Item();
    if (!item->child) return 0; /* memory fail */
    value=skip(parse_value(child,skip(value)));/* skip any spacing, get the value. */
    if (!value) return 0;
    while (*value==',') {
        cJSON *new_item;
        if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
        child->next=new_item;
        new_item->prev=child;
        child=new_item;
        value=skip(parse_value(child,skip(value+1)));
        if (!value) return 0;/* memory fail */
    }
    if (*value==']') return value+1;/* end of array */
    ep=value;
    return 0;/* malformed. */
}


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-57098-8.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...