在C缀前pressions转换为后缀(RPN)的捷径 [英] The shortest way to convert infix expressions to postfix (RPN) in C

查看:122
本文介绍了在C缀前pressions转换为后缀(RPN)的捷径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原来的表述给出这里(你也可以试试您的正确性程序)。

附加规则:结果
 1.程序应该从标准输入读取和写入标准输出。结果
 2.程序应返回零到呼叫系统/程序。结果
 3.程序应编制并用gcc -O2 -lm -s -fomit-frame-pointer的运行。

所面临的挑战有一定的历史:呼吁短期已实现在的 2009年9月波兰编程大赛博客比赛结束后,在最短的code为81个字符长。后来第二个电话已经取得了更短的code和今年matix2267的发表了自己的解决方案在78字节:

<$p$p><$c$c>main(c){read(0,&c,1)?c-41&&main(c-40&&(c%96<27||main(c),putchar(c))):exit(0);}

任何人,使其更短或证明这是不可能的?


解决方案

下面是减少code到 76 字符的方式:

<$p$p><$c$c>main(c){read(0,&c,1)?c-41&&main(c-40&&putchar(c,c%96>26&&main(c))):exit(0);}

有一个较长的评论版清晰:

  INT主(INT C)
{
   如果(读(0,和C,1)){/ *读字符* /
       如果(C-41){/ *如果没有')'* /
           如果(C-40){/ *然后如果没有'('* /
               如果(C%96 GT; 26){/ *那么,如果运营商(不是字母或LT; LF&GT;)* /
                   主(三); / * *递归/
               }
               的putchar(C); / * *打印/
           }
           主(三); / * *递归/
       }
   }否则退出(0); / *程序结束* /
}

Original formulation is given here (you can try also your program for correctness) .

Additional rules:
1. The program should read from standard input and write to standard output.
2. The program should return zero to the calling system/program.
3. The program should compile and run with gcc -O2 -lm -s -fomit-frame-pointer.

The challenge has some history: the call for short implementations has been announced at the Polish programming contest blog in September 2009. After the contest, the shortest code was 81 chars long. Later on the second call has been made for even shorter code and after the year matix2267 published his solution in 78 bytes:

main(c){read(0,&c,1)?c-41&&main(c-40&&(c%96<27||main(c),putchar(c))):exit(0);}

Anyone to make it even shorter or prove this is impossible?

解决方案

Here's a way to reduce the code down to 76 chars:

main(c){read(0,&c,1)?c-41&&main(c-40&&putchar(c,c%96>26&&main(c))):exit(0);}

A longer, commented version for clarity:

int main(int c)
{
   if (read(0,&c,1)) {          /* read char */
       if (c-41) {              /* if not ')' */
           if (c-40) {          /* then if not '(' */
               if (c%96>26) {   /* then if operator (not alphabet or <LF>) */
                   main(c);     /* recurse */
               }
               putchar(c);      /* print */
           }
           main(c);             /* recurse */
       }        
   } else exit(0);              /* end program */
}

这篇关于在C缀前pressions转换为后缀(RPN)的捷径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆