我有一个 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/