Google+ insable

2012-08-31

混乱你的代码


会编程的都会把程序搞得一团糟,当然程度不一样。
下面是一个Step by Step的教程,如何把一个清晰的代码变得复杂难懂的。

开始程序

下面是一个素数查找的程序:
开始作案了:


void primes(int cap)
{
    int i, j, composite;
    for(i = 2; i < cap; ++i) {
        composite = 0;
        for(j = 2; j * j < i; ++j) {
            composite += !(i % j);
        }
        if(!composite){
            printf("%dt", i);
        }
    }
}
int main()
{
    primes(100);
}

第一步、把for变成while

一般来说,for循坏要比while循坏简单,上面的程序有二重for循环,我们要把其变成while循环,而且还要把二重循环的变成一重的循环,然后使用很多的if-else语句来判断。


void primes(int cap)
{
    int i, j, composite, t = 0;
    while(t < cap * cap) {
        i = t / cap;
        j = t++ % cap;
        if(i <= 1);
        else if(!j)
            composite = j;
        else if(j == i && !composite)
            printf("%dt",i);
        else if(j > 1 && j < i)
            composite += !(i % j);
    }
}
int main()
{
    primes(100);
}

第二步,把循坏变成递归

递归在某些时候可以把代码变得简单明了,但大多数的情况下是把代码变得复杂没有效率。下面是把上面的while循环变成了递归。变成了递归后,函数的参数都变成3个了。

void primes(int cap, int t, int composite)
{
    int i,j;
    i = t / cap;
    j = t % cap;
    if(i <= 1)
        primes(cap,t+1,composite);
    else if(!j)
        primes(cap,t+1,j);
    else if(j == i && !composite)
        (printf("%dt",i), primes(cap,t+1,composite));
    else if(j > 1 && j < i)
        primes(cap,t+1, composite + !(i % j));
    else if(t < cap * cap)
        primes(cap,t+1,composite);
}
int main()
{
    primes(100,0,0);
}


第三步,弄乱代码结构/使用没有含义的变量名

关于如何弄乱代码结构,其中一个小技巧是,使用“?”表达式代替if-else语句。


void primes(int m, int t, int c)
{
    int i,j;
    i = t / m;
    j = t % m;
    (i <= 1) ? primes(m,t+1,c) : (!j) ? primes(m,t+1,j) : (j == i && !c) ?
    (printf("%dt",i), primes(m,t+1,c)) : (j > 1 && j < i) ?
    primes(m,t+1,c + !(i % j)) : (t < m * m) ? primes(m,t+1,c) : 0;
}
int main()
{
    primes(100,0,0);
}

第四步,取消临时变量

临时变量一般用来保存反复使用的一个表达式的值。使用大量重复的表达式来取消这些临时变量的也可以让代码复杂起来。


void primes(int m, int t, int c)
{
  ((t / m) <= 1) ? primes(m,t+1,c) : !(t % m) ? primes(m,t+1, t % m) :
  ((t % m)==(t / m) && !c) ? (printf("%dt",(t / m)), primes(m,t+1,c)) :
  ((t % m)> 1 && (t % m) < (t / m)) ? primes(m,t+1,c + !((t / m) % (t % m))) :
  (t < m * m) ? primes(m,t+1,c) : 0;
}
int main()
{
    primes(100,0,0);
}

第五步,继续弄乱变量名

我们知道,下划线是合法的变量名,所以,我们不妨用__,___,____来代替m,t,c。函数名也可以使用下划线来代替。让我们来看看求素数的函数能变成什么。

void _(int __, int ___, int ____)
{
    ((___ / __) <= 1) ? _(__,___+1,____) : !(___ % __) ? _(__,___+1,___ % __) :
    ((___ % __)==(___ / __) && !____) ? (printf("%dt",(___ / __)),
    _(__,___+1,____)) : ((___ % __) > 1 && (___ % __) < (___ / __)) ?
    _(__,___+1,____ + !((___ / __) % (___ % __))) : (___ < __ * __) ?
    _(__,___+1,____) : 0;
}
int main()
{
    _(100,0,0);
}

第六步,移除常量

在上面的程序中,还有一些常量,你可以通过增加一个宏定义,或是增加一个函数的形参来取代这一常量。

void _(int __, int ___, int ____, int _____)
{
    ((___ / __) <= _____) ? _(__,___+_____,____,_____) : !(___ % __) ? _(__,___+_____,___ % __, _____) :
    ((___ % __)==(___ / __) && !____) ? (printf("%dt",(___ / __)),
    _(__,___+_____,____,_____)) : ((___ % __) > _____ && (___ % __) < (___ / __)) ?
    _(__,___+_____,____,_____ + !((___ / __) % (___ % __))) : (___ < __ * __) ?
    _(__,___+_____,____,_____) : 0;
}
int main() {
    _(100,0,0,1);
}


程序到这里应该差不多了。

会编程的都会把程序搞得一团糟,真的。

2012-07-24

The Big Bang Theory

The Big Bang Theory


Our whole universe was in a hot dense state 私たちの宇宙はかつて熱くて調密な一点だった 宇宙一度又烫又稠密 (大爆炸初期的模样) Then nearly fourteen billion years ago expansion started. Wait... それから1千4百億年前に,拡張が始まった。待って! 140亿年前终于爆炸 等着瞧... (等待新的哈伯常数值) The Earth began to cool, 地球は冷めた。 地球开始降温 (原始地球冷却) The autotrophs began to drool, 独立栄養バクテリアは活動し始まった。 自养生物开始活动 (生命起源) Neanderthals developed tools, ネアンテルタール人は道具を発明した。 穴居人发明工具 (人类活动) We built a Wall (we built the Pyramids). 私たちは万里の長城を建てた(ピラミッドを建てた)。 我们建了长城(我们造了金字塔)(The Great Wall 和Pyramids 古代工程奇迹) Math, science, history, unraveling the mysteries, 数学,科学,歴史,不思議を解くこと 数学 自然科学 历史 揭开神秘的面纱 (严格讲数学不属于自然科学) That all started with the Big Bang! 全てはビッグバンで始まった。バン! 一切由大爆炸开始 "Since the dawn of man" is really not that long, 「人間の起源から」とはそんなに長い間じゃない。 其实"人类历史"没有多久 As every galaxy was formed in less time than it takes to sing this song. この歌を歌うために必要な時間より短い間にそれぞれの銀河が形成されたんだろう。 各个星系形成时间比唱完这支歌还要短 A fraction of a second and the elements were made. 一マイクロに元素が作られた。 元素在微秒间便形成了 The bipeds stood up straight. 二足動物は立ち上がった。 两足动物直立行走 The dinosaurs all met their fate. 恐竜たちは没してしまった。 恐龙都认命了 They tried to leap but they were late 逃げようとしても,遅すぎた。 想要逃跑(突变进化) 没来得及 (leap有逃跑的意思还有突变的意思,两种意思都说的通,反正这些大块头挂了) And they all died (they froze their asses off). みんな死んでしまった(尻尾が凍えちゃった)。 就死翘翘了(PP都冻僵了,那颗小行星撞击尤卡坦半岛,形成的尘埃导致全球光照不足,引发核冬天) The oceans and Pangaea, パンサラッサもパンゲアも, 泛大洋和盘古大陆 (这是为了对称,所以oceans应该指的是泛大洋) See ya, wouldn't wanna be ya!, さらば! 拜拜您那,才不想这样呢 Set in motion by the same Big Bang! ビッグバンによる動きかけた。 都是爆炸惹的祸 It all started with the Big Bang! 全てはビッグバンで始まった。 一切从大爆炸开始 It's expanding ever outward but one day 宇宙はあいかわらず膨らんでいる, 宇宙向外膨胀 但有一天 It will cause the stars to go the other way, でもいつか星たちは逆転させられるだろう。 星球会反方向运动 (脉动宇宙论,现在的研究结果是宇宙会减慢膨胀速度,趋近于零但不会停止) Collapsing ever inward. We won't be here, it won’t be hurt. 縮み続ける。その時,私たちはもう存在しない。痛くはない。 大坍塌,反正我们不在了,不会觉得疼 Our best and brightest figure that it'll make an even bigger bang! 優れた科学者は,宇宙の果てのビッグバンはより大きいだろうと考えている。 我们美好光辉的形象将引发一场更大的爆炸 Australopithecus would really have been sick of us アウストラロピテクス人は私たちのことは嫌いだろう, 南方古猿肯定不爽我们 Debating out while here they're catching deer (we're catching viruses). 彼らが一生懸命に鹿を狩っているのに,私たちはただ理屈っぷく論じている。(私たちはウイルスをしている。) 在他们捉鹿时唧唧歪歪 (我们现在捉电脑病毒) Religion or Astronomy, Encarta, Deuteronomy, 宗教も天分学も,エンカルタ百科事典も申命記も, 宗教 天文 电子百科 旧约申命记 It all started with the Big Bang! 全てはビッグバンで始まった。 一切从大爆炸开始 Music and Mythology, Einstein and Astrology 音楽も神話も,アインシュタインも占星術も, 音乐,神话,爱因斯坦,占星术 It all started with the Big Bang! 全てはビッグバンで始まった。 一切从大爆炸开始