一步步走向国际乱码大赛-- 恶搞C语言

本文分享了一次将规范的C语言代码转换为混乱代码的尝试过程,涉及从简单替换循环结构开始,逐步引入递归、条件操作符等复杂元素,最终达到代码变得难以辨认但仍能正常运行的效果。通过这一过程,作者不仅展示了对C语言深入理解,还强调了逻辑思维和代码结构的重要性。同时,文章也提到了在恶搞代码过程中遇到的挑战和学习的要点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       大家都一直强调规范编码,但是这个世界上有个大师们娱乐的竞赛——国际乱码大赛。

       能写出来的都是对语言深入了解的master。我从没想自己也能“恶搞”C,一直都是老老实实编码。就在前几天看了一篇帖子。

感觉把很规范的代码变成乱码,很有意思。于是决定动手试一试。


        我不得不说。。。我以为看起来还简单的东西,搞了我一天,我去。。。各种bug。。。不过也有很大的收获。我没想到“把代码写乱”也能给我代码收获。。。我想,这的确很有意思。This blog will memory my work and process with the interesting skill.




感觉hello world级别的改不出什么东西来,不够“炫酷”。挑了几个好玩的代码,于是选中了下面这块实验田:

/* 
              1
            2   3
          4   5   6
        7   8   9  10

    实现输出三角形数阵

*/
#include <stdio.h>
int main ()
{
    int row=0,rank=0,num=0,blank=0;//row行,rank列变量,blank变量控制打印空格
	int n[10];
	for(num=1;num<=10;num++)//给数组赋初值,通过修改这一步得到你想要的数据
	{
	    n[num-1]=num;//通过修改这一步得到你想要的数据,例如2*num-1
	}

	num=0;

	for(row=1;row<=4;row++)//输出
	{
	    for(blank=1;blank<=5-row;blank++)
		   printf(" ");
		   
	        for(rank=1;rank<=row;rank++)
		{
		   printf("%d  ",n[num]);
		     num++;
		   if(rank==row)
		      printf("\n");
		}
	}
	return 0;
}


对于学过C的老手来说,看懂这个完全不是问题

新手可能有点别扭。不过我还是比较喜欢这家伙,打印了一个三角形数字阵

jasonleaster@ubuntu:~/Desktop$ ./a.out
    1  
   2  3  
  4  5  6  
 7  8  9  10 


代码变乱第一步

将for循环改成while循环



void triangle(void)
{
        int row = 0,rank = 0,num=0,blank=0;//row行,rank列变量,blank变量控制打印
空格

        int n[10];

        num = 1;

        while(num<=10)//给数组赋初值,通过修改这一步得到你想要的数据
        {
                n[num-1]=num;//通过修改这一步得到你想要的数据,例如2*num-1
                num++;
        }

        num=0;
        row = 1;
        while(row <= 4)//输出
        {
                blank = 1;
                while(blank <= 5-row)
                {
                        printf(" ");
                        blank++;
                }

                rank = 1;
                while(rank<=row;)
                {
                   printf("%d  ",n[num]);
                     num++;
                   if(rank==row)
                      printf("\n");

                        rank++;
                }
                row++
        }

        return ;
}


这样恶搞的完全还没有深度。。。但是,是个很好的铺垫。。


恶搞代码步骤二,把while循环变递归


通过这个练习可以加强自己的逻辑思维,对于代码的逻辑理解将会很细致。。。后面还会更细致。。。。。


/* 
              1
            2   3
          4   5   6
        7   8   9  10

    实现输出三角形数阵

*/
#include <stdio.h>

int n[10];//Attention. I change the "n" array into a global array

static int location = 0;

int triangle(int num,int row,int rank,int blank);

int main()
{
        triangle(1,1,1,1);

        return 0;
}

int triangle(int num,int row,int rank,int blank)
{

//-------------the first recursion block------------------------------
        if(num<=10)


        {
                triangle(num+1,0,0,0);
                n[num-1] = num;

                if(num != 1)
                {
                        return 0;
                }
        }
        else if(num == 11)
        {
                return 0;
        }
//------------------------------------------------------------------

        //-------------------the fourth recursion block-------------
        if(row <= 4)//输出
        {
                //--------------the second recursion block---------
                if(blank <= 5-row)
                {
                        printf(" ");
                        triangle(12,row,rank,blank+1);//num == 12 pass the first recursion block
                        if(blank != 1)
                        {
                                return 0;
                        }
                }
                else if(blank != 100)
                {
                        return 0;//end the recursion
                }

               //----------------------------------------------

                //-----------the third recursion block------------

                if(rank <= row)
                {
                        printf("%d  ",n[location]);
                                location++;

                        if(rank==row)
                                printf("\n");

                        triangle(12,row,rank+1,100);//pass the first and the second recursion
                        if(rank != 1)
                        {
                                return 0;
                        }
                }
                else
                {
                        return 0;
                }
                //---------------------------------------------

                triangle(12,row+1,rank,blank);// pass the first recursion

        }

        return 0;
}
主要的要领就是通过return 适时的解释当前递归。(我的这种策略还不是很好,其实可以改变代码的逻辑,消除return 让被调用函数自动的在适当的时候挂掉。。而不是通过return 主动杀死。。。正因为这样,我后面花了很多时间改代码的逻辑结构)


昨晚凌晨3点半的时候,放弃了,今天晚上8点多的时候调出来了。。。

这里去掉了return 原因嘛,就是为了 条件操作符做准备,把代码变的更难看

要知道

判断语句? return :0;

这类的语句是违法的,return 不是个expression,所以不能用在 选择操作符的右边。。。昨晚就是因为这个放弃继续恶搞的,改结构很坑爹的说。很锻炼逻辑的说。

//简直就是艰辛!~


/* 
              1
            2   3
          4   5   6
        7   8   9  10

    实现输出三角形数阵

*/
#include <stdio.h>

int n[10];//Attention. I change the "n" array into a global array

static int location = 0;

static int dead = 0;

static int first_r = 1;

static int second_r = 0;

static int third_r = 0;

static int fourth_r = 0;

void triangle(int num,int row,int rank,int blank);

int main()
{
    triangle(1,1,1,1);
    
    return 0;
}

void triangle(int num,int row,int rank,int blank)
{

    dead = 0;
//-------------the first recursion block------------------------------
    if (num<=10)  
    {
        if(first_r !=0 && second_r != 1 && third_r != 1 && fourth_r != 1)
        {
            triangle(num+1,0,0,0);
            n[num-1] = num;
            first_r = 0;
            second_r = 1;//进行第二个递归
            third_r = 0;
            fourth_r = 0;
        }
        
        if(num == 1)
        {
            if(row <= 4) 
            {
                //--------------the second recursion block---------
                    if((blank <= 5-row) && (first_r != 1&& second_r != 0 && third_r != 1 && fourth_r != 1))
                {
                    printf(" ");
                    if(first_r != 1&& second_r != 0 && third_r != 1 && fourth_r != 1)
                    {
                        triangle(1,row,rank,blank+1);
                        first_r = 0;
                        second_r =0;
                        third_r = 1;//进行第三个递归
                        fourth_r = 0;

                        if(blank != 1)
                        {
                            dead = 1;
                        }
                        else
                        {
                            dead = 0;
                        }
                    }
                }
                else
                {
                    dead = 1;
                }

                if(blank == 1)
                {
                    if(rank <= row)
                    {
                        printf("%d  ",n[location]);
                            location++;

                        if(rank==row)
                        {
                            printf("\n");
                            dead = 0;
                        }
                        
                        if(row != rank && (first_r != 1&& second_r != 1 && third_r != 0 && fourth_r != 1))
                        {
                            triangle(1,row,rank+1,1);//pass the first and the second recursion
                            first_r  = 0;
                            second_r = 0;
                            third_r  = 0;
                            fourth_r = 1;
                            dead = 1;
                        }
                    }
                }


                if(dead == 0)
                {
                    first_r = 1;
                    second_r= 0;
                    third_r = 0;
                    fourth_r= 0;
                    triangle(1,row+1,1,1);// pass the first recursion
                }
            }
        }
    }
}



我估计没人会去分析上面的代码了,各种递归,各种条件限制。

Now,it's time to show the shit!

下面那坨东西是可以运行的。。。亲测。。。

/*
              1
            2   3
          4   5   6
        7   8   9  10

    实现输出三角形数阵

*/
#include <stdio.h>

int n[10];//Attention. I change the "n" array into a global array

static int location = 0;

static int dead = 0;

static int first_r = 1;

static int second_r = 0;

static int third_r = 0;

static int fourth_r = 0;

void triangle(int num,int row,int rank,int blank);

int main()
{
        triangle(1,1,1,1);

        return 0;
}


void triangle(int num,int row,int rank,int blank)
{

        dead = 0;
        (num<=10) ? (((first_r !=0 && second_r != 1 && third_r != 1 && fourth_r != 1) ? \
( triangle(num+1,0,0,0),n[num-1] = num,first_r = 0,second_r = 1,third_r = 0,fourth_r = 0):0),\
 ((num == 1) ? ((row <= 4) ? (( ((blank <= 5-row) &&\
 (first_r != 1&& second_r != 0 && third_r != 1 && fourth_r != 1)) ? \
( printf(" "),(first_r != 1&& second_r != 0 && third_r != 1 && fourth_r != 1) ?\
 (triangle(1,row,rank,blank+1),first_r = 0,second_r =0,third_r = 1,fourth_r = 0,((blank != 1) ?\
 (dead = 1) :(dead = 0))) : 0) : (dead = 1) ),((blank == 1) ? ((rank <= row) ?\
 ((printf("%d  ",n[location]),location++),((rank==row) ? ( printf("\n"),dead = 0) : 0),\
((row != rank && (first_r != 1&& second_r != 1 && third_r != 0 && fourth_r != 1)) ?\
 ( triangle(1,row,rank+1,1),first_r  = 0,second_r = 0,third_r  = 0,fourth_r = 1,dead = 1) : 0)) : 0 ) : 0),\
((dead == 0) ? (first_r = 1,second_r= 0,third_r = 0,fourth_r= 0,triangle(1,row+1,1,1)) : 0) ) : 0 ) : 0 )):0;

}

liuzjian@ubuntu:~$ ./a.out
    1  
   2  3  
  4  5  6  
 7  8  9  10
上面那坨翔可能一般看来好像无解,其实还是有解的,逆向只要根据括号的匹配规则是可以把代码变规范的。

只是。。。我估计这是个很蛋疼的工作。。。

怎么把代码变的更操蛋?


第三步,糟糕的变量名



#include <stdio.h>
 
int n[10]; 
static int _ = 0;

static int __ = 0;

static int ___ = 1;

static int ____ = 0;

static int ______ = 0;

static int _______ = 0;

void ____________(int ________,int _________,int __________,int ___________);

int main()
{
        ____________(1,1,1,1);

        return 0;
}

void ____________(int ________,int _________,int __________,int ___________)
{ __ = 0; (________<=10) ? (((___ !=0 && ____ != 1 && ______ != 1 && _______ != 1) ? \
( ____________(________+1,0,0,0),n[________-1] = ________,___ = 0,____ = 1,______ = 0,_______ = 0):0),\
 ((________ == 1) ? ((_________ <= 4) ? (( ((___________ <= 5-_________) &&\
 (___ != 1&& ____ != 0 && ______ != 1 && _______ != 1)) ? \
( printf(" "),(___ != 1&& ____ != 0 && ______ != 1 && _______ != 1) ?\
 (____________(1,_________,__________,___________+1),___ = 0,____ =0,______ = 1,_______ = 0,((___________ != 1) ?\
 (__ = 1) :(__ = 0))) : 0) : (__ = 1) ),((___________ == 1) ? ((__________ <= _________) ?\
 ((printf("%d  ",n[_]),_++),((__________==_________) ? ( printf("\n"),__ = 0) : 0),\
((_________ != __________ && (___ != 1&& ____ != 1 && ______ != 0 && _______ != 1)) ?\
 ( ____________(1,_________,__________+1,1),___  = 0,____ = 0,______  = 0,_______ = 1,__ = 1) : 0)) : 0 ) : 0),\
((__ == 0) ? (___ = 1,____= 0,______ = 0,_______= 0,____________(1,_________+1,1,1)) : 0) ) : 0 ) : 0 )):0;}

jasonleaster@ubuntu:~/Desktop$ ./a.out
    1  
   2  3  
  4  5  6  
 7  8  9  10 

我相信上面这个东东可以把人送进疯人院,但是。。。它确实可以运行。

对于那些变量名乱用的programmer不要有包容心,切记。



通过"恶搞",其实能更深的理解语言本身,能够更好的锻炼逻辑分析能力。学会控制自己的逻辑,不要写自己也不知道的东西。

规范编码很重要。


其实恶搞过程中,我领悟最深刻的是两点

1。代码逻辑的重构

2。普通循环和递归之间的转换

这两点是我最开心的领悟























评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值