How does Duff's device work?

前端 未结 11 1905
日久生厌
日久生厌 2020-11-22 04:56

I\'ve read the article on Wikipedia on the Duff\'s device, and I don\'t get it. I am really interested, but I\'ve read the explanation there a couple of times and I still do

11条回答
  •  故里飘歌
    2020-11-22 05:29

    Just experimenting, found another variant getting along without interleaving switch statement and do-while-loop:

    int n = (count + 1) / 8;
    switch (count % 8)
    {
        LOOP:
    case 0:
        if(n-- == 0)
            break;
        putchar('.');
    case 7:
        putchar('.');
    case 6:
        putchar('.');
    case 5:
        putchar('.');
    case 4:
        putchar('.');
    case 3:
        putchar('.');
    case 2:
        putchar('.');
    case 1:
        putchar('.');
    default:
        goto LOOP;
    }
    

    Technically, the goto still implements a loop, but this variant might be slightly more readable.

提交回复
热议问题