我需要数以万计的部件的部件号等。
int a1;
int a2;
int a3;
int a4;
int a = 15346;
a = ?
最佳答案
您可以使用模 (%
) 运算符来获取最后一位数字,然后减去该值并重复。例如
int value = 15436
int ones, tens, hundreds, thousands, tenthousands;
ones = value % 10 // gives 6
value = value - ones // leaves 15340
tens = value % 100 // gives 40
value = value - tens // leaves 15300
hundreds = value % 1000 // gives 300
value = value - hundreds // leaves 15000
thousands = value % 10000 // gives 5000
value = value = thousands // leaves 10000
tenthousands = value % 100000 // gives 10000
等等。这是一个非常幼稚的示例,您可能希望在循环和数组中执行此操作,但概念是相同的。 我想更好的方法是取模和除法而不是取模和减法
int val = 15346
int place = val % 10 // gives 6
val = val / 10 // leaves 1534
place = val % 10 // gives 4
所以
int val = 15346;
int place[5];
int indx = 0;
while(val > 10 && idx < 5) {
place[idx--] = val % 10;
val = val / 10;
}
关于C++/MFC 如何从 15346 : 15000, 300,40,6 生成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47148319/