简体   繁体   English

这两个将 integer 分成数字的 while 循环如何工作?

[英]How do these two while loops to separate an integer into digits work?

The code is about to separate an integer into digits, the code works, but I am having trouble with how the two "while" work together.该代码即将将 integer 分成数字,该代码有效,但我对这两个“while”如何协同工作有疑问。

#include <stdio.h>

int main() {
    int num, temp, factor = 1;

    printf("Enter a 5 digit number: ");
    scanf("%d", &num);

    temp = num;
    while (temp) {
        temp = temp / 10;
        factor = factor * 10;
    }

    while (factor > 1) {
        factor = factor / 10;
        printf("%d   ", num / factor);
        num = num % factor;
    }
    return 0;
}

This not so much a programming question as it is a basic arithmetic one, you mention the while s confuse you but you have to look at the code as a whole:这与其说是一个编程问题,不如说是一个基本的算术问题,你提到while让你感到困惑,但你必须将代码作为一个整体来看待:

//...
temp = num;

Stores the original input to be used ahead.存储要提前使用的原始输入。


while (temp) {
    temp = temp/10;
    factor = factor*10;
}

This cycle will divide the value temp by 10 until the result is 0 at which point it will evaluate to false and exit the loop, keep in mind that integer division in C truncates de result, eg 2/3 ~ 0.67 = 0 , the factor is simply accumulates the number of divisions needed to get to 0 , in this case 5 times so 100000 .此循环会将值temp除以10 ,直到结果为0 ,此时它将评估为 false 并退出循环,请记住 Z0D61F8370CAD1D412F80B84D1D412F80B84D143E1257Z 中的 integer 除法会截断结果,例如2/3 ~ 0.67 = 0 ,是简单地累积达到0所需的除法次数,在这种情况下为 5 次,因此100000


while (factor>1) {
    factor = factor/10;
    printf("%d   ", num/factor);
    num = num % factor;
}

Here the same principle happens, the first loop divides the number by the factor, so 12345 / 10000 = 1.2345 but since integer division truncates the decimal part you get 1 , next the modulus operator( % ) is applied, and what it does is to return the decimal part of a division, so 2345 , next loop 2345 / 1000 = 2.345 = 2 , decimal part 345 , and so on.这里发生相同的原理,第一个循环将数字除以因子,因此12345 / 10000 = 1.2345但由于 integer 除法会截断您得到的小数部分1 ,接下来应用模运算符( % ),它的作用是返回除法的小数部分,因此2345 ,下一个循环2345 / 1000 = 2.345 = 2 ,小数部分345 ,依此类推。


Note that, since you know the number of digits in the original input, you wouldn't even need the first loop you could simple harcode the factor:请注意,由于您知道原始输入中的位数,您甚至不需要第一个循环,您可以简单地对因子进行硬编码:

#include <stdio.h>

int main() {
    int num, factor;

    printf("Enter a 5 digit number: ");
    scanf("%d", &num);

    factor= 100000; //<--

    while (factor > 1) {
        factor = factor / 10;
        printf("%d   ", num / factor);
        num = num % factor;
    }

    return 0;
}
#include 

int main() {
    int num, temp, factor = 1;

    printf("Enter a 5 digit number: ");
    scanf("%d", &num);                             60403

    temp = num;                                    60403
    //while (temp) {
        temp = temp / 10;                           6040
        factor = factor * 10;                         10

        temp = temp / 10;                            604
        factor = factor * 10;                        100

        temp = temp / 10;                             60
        factor = factor * 10;                       1000

        temp = temp / 10;                              6
        factor = factor * 10;                      10000

        temp = temp / 10;                              0
        factor = factor * 10;                     100000
    //}

    //while (factor > 1) {
        factor = factor / 10;                      10000
        printf("%d   ", num / factor);                      60403/10000 is 6
        num = num % factor;                          403

        factor = factor / 10;                       1000
        printf("%d   ", num / factor);                        403/1000 is 0
        num = num % factor;                          403

        factor = factor / 10;                        100
        printf("%d   ", num / factor);                        403/100 is 4
        num = num % factor;                            3

        factor = factor / 10;                         10
        printf("%d   ", num / factor);                          3/10 is 0
        num = num % factor;                            3

        factor = factor / 10;                          1
        printf("%d   ", num / factor);                          3/1 is 3
        num = num % factor;                            0
    //}
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM