解析一个 int 变量
Parsing an int variable
我有一个 int 数组,其中包含以下格式的数字:
1160042
5900277
3200331
1001
1370022
有没有一种方法可以在不先将它们转换为字符串的情况下解析这些整数?我希望使用 00 作为标记,将其之前的所有内容分配给一个临时变量,将 00 之后的所有内容分配给第二个临时变量
00 是这些数字的自定义语义,以 10 为基数。
但是,由于机器在基数 2 上工作并且不知道所提供数字中双 0 的含义,您可能必须将其转换为字符串才能按照您描述的方式解析它。
#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;
}
我有一个 int 数组,其中包含以下格式的数字:
1160042
5900277
3200331
1001
1370022
有没有一种方法可以在不先将它们转换为字符串的情况下解析这些整数?我希望使用 00 作为标记,将其之前的所有内容分配给一个临时变量,将 00 之后的所有内容分配给第二个临时变量
00 是这些数字的自定义语义,以 10 为基数。 但是,由于机器在基数 2 上工作并且不知道所提供数字中双 0 的含义,您可能必须将其转换为字符串才能按照您描述的方式解析它。
#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;
}