c - 解析 int 变量

标签 c parsing int

我有一个 int 数组,其中填充了以下格式的数字:

1160042
5900277
3200331
1001
1370022

有没有一种方法可以解析这些整数而不先将它们转换为字符串?我希望使用 00 作为标记,将其之前的所有内容分配给一个临时变量,将 00 之后的所有内容分配给第二个临时变量

最佳答案

#include <stdio.h>

void splitInt(int v, int *first, int *second){
    int pre = -1;
    int zeroCount = 0;
    int denomination = 10;
    int temp1, temp2;
    *first = *second = -1;//not found
    while(temp1 = v / denomination){
        temp2 = v % denomination;
        if(pre == temp2){
            if(++zeroCount==2){
                *first  = temp1;
                *second = temp2;
                return ;
            }
        } else {
            zeroCount = 0;
        }
        pre = temp2;
        denomination *= 10;
    }
}

int main(void) {
    int data[5] = {1160042, 5900277, 3200331, 1001, 1370022};
    int i, temp1, temp2;
    for(i = 0; i < 5; ++i){
        splitInt(data[i], &temp1, &temp2);
        printf("%d, %d\n", temp1, temp2);
    }
    return 0;
}

关于c - 解析 int 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28953822/

相关文章:

c - (;;) 和 while(); 的作用是什么? C中的意思

c - 整个程序的 getpid()、getppid()?

ios - SWIFT:var someVariable:int = textfield.text.toInt() x textfield2.text.toInt() 并强制展开

python - 更快地将 int 转换为单个数字列表?

Java 将 int 转换为 char 不起作用

c - 将数组从文件 1 中的方法传递到文件 2 中的方法,而不在方法中使用额外参数

c - 如何在 CLion 中添加链接器标志

javascript - worklight 中的 JSon 解析错误

c++ - 如何在代码中获取函数名称

python - 在文本中创建语法突出显示的方法?