我在Quora上偶然发现了这个代码。

#include<stdio.h>

main(){
          int           $[]
        ={0x69,       0154,107,
     'e',0x79,0157, 117,'v',0x6a}
     ,_,__;_=__^__;__=_;while(_<(-
      (~(1<<3))+3)){(_==1<<1||_==
       -(~(1<<3))||_==11)?putchar
        (*($+(1>>1))):putchar(*(
          __++ +$)),(_==1>>1||
            _==1<<2||_==(1<<3
              )-1)?putchar
                (' '):1;
                  _++;
                    }
}

程序的输出是i like you viji。很感人,但很神秘。所以我把它格式化成indent来得到一个更好的主意。
main ()
{
  int $[] = { 0x69, 0154, 107,
    'e', 0x79, 0157, 117, 'v', 0x6a
  }
  , _, __;
  _ = __ ^ __;
  __ = _;
  while (_ < (-(~(1 << 3)) + 3))
    {
      (_ == 1 << 1 || _ ==
       -(~(1 << 3)) || _ == 11) ? putchar
(*($ + (1 >> 1))) : putchar (*(__++ + $)), (_ == 1 >> 1 ||
                                _ == 1 << 2 || _ == (1 << 3) - 1) ? putchar
(' ') : 1;
      _++;
    }
}

现在不是很感人,但还是有点神秘。
那么,有谁能解释一下这段代码是如何打印i like you viji
更新:
为变量$___命名,并扩展三元运算符:
int a[] = { 0x69, 0154, 107, 'e', 0x79, 0157, 117, 'v', 0x6a }, x, y;

x = y ^ y;
y = x;

while (x < (-(~(1 << 3)) + 3))
  {
    if (x == 1 << 1 || x == -(~(1 << 3)) || x == 11)
      putchar (*(a + (1 >> 1)));
    else
      {
        putchar (*(y++ + a));
        if (x == 1 >> 1 || x == 1 << 2 || x == (1 << 3) - 1)
          putchar (' ');
        else
          1;
      }
    x++;
  }

最佳答案

重写代码可以得到:

int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'};
int i0, i1;  // Moved to new line instead of using ,

i0 = 0;  // i0 = i1 ^ i1;
i1 = i0;

while (i0 < 12)  // All strange constant like (1<<3) recalculated
{
    if (i0 == 2 || i0 == 9 || i0 == 11)   // "? :" replaced by if
    {
            putchar(*arr1);
    }
    else
    {
        putchar (*(arr1 + i1));
        ++i1;

        if (i0 == 0 || i0 == 4 || i0 == 7)
        {
            putchar(' ');
        }
    }
    i0++;
}

逐步说明:
1)重新格式化行,删除不必要的空格,为可读性插入空格
main()
{
    int $[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , _, __;
    _ = __^__;
    __ = _;
    while(_ < (-(~(1<<3))+3))
    {
        (_ == 1<<1 || _ == -(~(1<<3)) || _ == 11) ?
                putchar(*($ + (1>>1))) :
                putchar(*(__++ +$)), (_ == 1 >> 1 || _ == 1<<2 || _ == (1<<3)-1) ?
                        putchar(' ') : 1;
        _++;
    }
}

2)重命名变量,即$为arr1,0为i0,uu为i1
main()
{
    int arr1[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , i0, i1;
    i0 = i1^i1;
    i1 = i0;
    while(i0 < (-(~(1<<3))+3))
    {
        (i0==1<<1 || i0== -(~(1<<3)) || i0 == 11) ?
                putchar(*(arr1+(1>>1))) :
                putchar(*(i1++ +arr1)), (i0 == 1 >> 1 || i0 == 1<<2 || i0 == (1<<3)-1) ?
                        putchar(' ') : 1;
        i0++;
    }
}

3)使用if语句代替?以下内容:
这包括将逗号行分成两行。
main()
{
    int arr1[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , i0, i1;
    i0=i1^i1;
    i1=i0;
    while(i0 < (-(~(1<<3))+3))
    {
        if (i0 == 1<<1 ||i0== -(~(1<<3)) || i0 == 11)
        {
                putchar(*(arr1+(1>>1)));
        }
        else
        {
                putchar(*(i1++ +arr1));
                if (i0 == 1 >> 1 || i0 == 1<<2 || i0 == (1<<3)-1)
                {
                        putchar(' ');
                }
                else
                {
                    1;  // This does nothing so it can be removed
                }
        }
        i0++;
    }
}

4)重新计算数值常数,使其达到更好的值
实例
0x69与“i”相同
1<-(~(11>>1与0相同
main()
{
    int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'} , i0, i1;
    i0 = 0;
    i1 = i0;
    while(i0 < 12)
    {
        if (i0 == 2 || i0 == 9 || i0 == 11)
        {
                putchar(*(arr1));
        }
        else
        {
                putchar(*(i1++ +arr1));

                if (i0 == 0 || i0 == 4 || i0 == 7)
                {
                        putchar(' ');
                }
        }
        i0++;
    }
}

5)一些小的最终清理
main()
{
    int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'};  // Move i0 and
                                                                  // i1 to nextt line
    int i0, i1;

    i0 = 0;
    i1 = i0;
    while(i0 < 12)
    {
        if (i0 == 2 || i0 == 9 || i0 == 11)
        {
                putchar(*arr1);
        }
        else
        {
                putchar(*(arr1 + i1));  // Splitted into two lines
                ++i1;

                if (i0 == 0 || i0 == 4 || i0 == 7)
                {
                        putchar(' ');
                }
        }
        i0++;
    }
}

现在代码很容易阅读。

关于c - 此❤代码如何工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28419002/

10-11 23:16
查看更多