我试图了解以下程序如何工作。它是命令行科学计算器。来源取自here。对于IOCCC条目来说,这似乎很可读,但显然不是。

#include <stdio.h>
#include <math.h>
#define clear 1;if(c>=11){c=0;sscanf(_,"%lf%c",&r,&c);while(*++_-c);}\
  else if(argc>=4&&!main(4-(*_++=='('),argv))_++;g:c+=
#define puts(d,e) return 0;}{double a;int b;char c=(argc<4?d)&15;\
  b=(*_%__LINE__+7)%9*(3*e>>c&1);c+=
#define I(d) (r);if(argc<4&&*#d==*_){a=r;r=usage?r*a:r+a;goto g;}c=c
#define return if(argc==2)printf("%f\n",r);return argc>=4+
#define usage main(4-__LINE__/26,argv)
#define calculator *_*(int)
#define l (r);r=--b?r:
#define _ argv[1]
#define x

double r;
int main(int argc,char** argv){
  if(argc<2){
    puts(
      usage: calculator 11/26+222/31
      +~~~~~~~~~~~~~~~~~~~~~~~~calculator-\
      !                          7.584,367 )
      +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
      ! clear ! 0 ||l   -x  l   tan  I (/) |
      +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
      ! 1 | 2 | 3 ||l  1/x  l   cos  I (*) |
      +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
      ! 4 | 5 | 6 ||l  exp  l  sqrt  I (+) |
      +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
      ! 7 | 8 | 9 ||l  sin  l   log  I (-) |
      +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(0
    );
  }
  return 0;
}


使用-Wall在gcc 4.7.2(Linux)上编译时不会发出警告。我试图简化它,但是对下面给出的略微修改的形式进行的任何进一步更改都会产生意外的结果(错误的输出)。

#include <stdio.h>
#include <math.h>
#define clear 1;if(c>=11){c=0;sscanf(_,"%lf%c",&r,&c);while(*++_-c);}\
  else if(argc>=4&&!main(4-(*_++=='('),argv))_++;g:c+=
#define puts(d,e) return 0;}{double a;int b;char c=(argc<4?d)&15;\
  b=(*_%__LINE__+7)%9*(3*e>>c&1);c+=
#define I(d) (r);if(argc<4&&*#d==*_){a=r;r=usage?r*a:r+a;goto g;}c=c
#define return if(argc==2)printf("%f\n",r);return argc>=4+
#define usage main(4-__LINE__/26,argv)
#define calculator *_*(int)
#define l (r);r=--b?r:
#define _ argv[1]
#define x

double r;
int main(int argc,char** argv){
  if(argc<2){
    puts(
      usage: calculator 11/26+222/31
      +calculator-\
      !                          7.584,367 )
      +
      ! clear ! 0 ||l   -x  l   tan  I (/) |
      +
      ! 1 | 2 | 3 ||l  1/x  l   cos  I (*) |
      +
      ! 4 | 5 | 6 ||l  exp  l  sqrt  I (+) |
      +
      ! 7 | 8 | 9 ||l  sin  l   log  I (-) |
      +(0);
  }
  return 0;
}


有人可以解释一下它是如何工作的吗?

最佳答案

它扩展为如下所示:

double r;
int
main (int argc, char **argv)
{
  if (argc < 2)
    {
      if (argc == 2)
    printf ("%f\n", r);
      return argc >= 4 + 0;
    }
  {
    double a;
    int b;
    char c = (argc < 4 ? main (4 - 21 / 26,
                   argv) : *argv[1] * (int) 11 / 26 + 222 / 31 +
          ~~~~~~~~~~~~~~~~~~~~~~~~*argv[1] * (int) -!7.584) & 15;
    b = (*argv[1] % 21 + 7) % 9 * (3 * 367 >> c & 1);
    c += +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+!1;
    if (c >= 11)
      {
    c = 0;
    sscanf (argv[1], "%lf%c", &r, &c);
    while (*++argv[1] - c);
      }
    else if (argc >= 4 && !main (4 - (*argv[1]++ == '('), argv))
      argv[1]++;
  g:c += !0 || (r);
    r = --b ? r : -(r);
    r = --b ? r : tan (r);
    if (argc < 4 && *"/" == *argv[1])
      {
    a = r;
    r = main (4 - 23 / 26, argv) ? r * a : r + a;
    goto g;
      }
    c = c | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+!1 | 2 | 3 || (r);
    r = --b ? r : 1 / (r);
    r = --b ? r : cos (r);
    if (argc < 4 && *"*" == *argv[1])
      {
    a = r;
    r = main (4 - 25 / 26, argv) ? r * a : r + a;
    goto g;
      }
    c = c | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+!4 | 5 | 6 || (r);
    r = --b ? r : exp (r);
    r = --b ? r : sqrt (r);
    if (argc < 4 && *"+" == *argv[1])
      {
    a = r;
    r = main (4 - 27 / 26, argv) ? r * a : r + a;
    goto g;
      }
    c = c | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+!7 | 8 | 9 || (r);
    r = --b ? r : sin (r);
    r = --b ? r : log (r);
    if (argc < 4 && *"-" == *argv[1])
      {
    a = r;
    r = main (4 - 29 / 26, argv) ? r * a : r + a;
    goto g;
      }
    c = c | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(0);
  }
  if (argc == 2)
    printf ("%f\n", r);
  return argc >= 4 + 0;
}


它仍然不完全可读,但至少现在没有任何东西对您隐藏。您应该能够进行简化而不会被严重咬伤。

由于@cmaster所说的相同原因,我将不做完整的解释。

07-26 06:07