免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6442 | 回复: 17
打印 上一主题 下一主题

[C] 趣题:可打印自身源代码的C程序(不使用文件操作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-01 18:11 |只看该作者 |倒序浏览

题目:不使用文件操作,写一个把自己的源码内容打印出来的C程序。

下面是我写的程序。


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
char* pat = "#include <stdlib.h>#include <stdio.h>#include <string.h>int main(){char* pat = !;int len = strlen(pat);char* self;char* ps;char* pc;int num = 0;pc = pat;while (*pc){    if (*pc==123) num += 2;    if (*pc==62 || *pc==125 || *pc==59) num += 1;    pc++;}self = malloc(2*len+num+2);pc = pat;ps = self;while (*pc != '!'){    if (*pc==123) *ps++ = 10;    *ps++ = *pc;    if (*pc==62 || *pc==123 || *pc==125 || *pc==59) *ps++ = 10;    pc++;}*ps++ = 34;strncpy(ps, pat, pc - pat);ps += pc - pat;*ps++ = '!';strcpy(ps, pc+1);ps += len - (pc - pat) -1;*ps++ = 34;while (*++pc){    if (*pc==123) *ps++ = 10;    *ps++ = *pc;    if (*pc==62 || *pc==123 || *pc==125 || *pc==59) *ps++ = 10;}*ps = 0;printf(self);free(self);return 0;}";
int len = strlen(pat);
char* self;
char* ps;
char* pc;
int num = 0;
pc = pat;
while (*pc)
{
    if (*pc==123) num += 2;
    if (*pc==62 || *pc==125 || *pc==59) num += 1;
    pc++;
}
self = malloc(2*len+num+2);
pc = pat;
ps = self;
while (*pc != '!')
{
    if (*pc==123) *ps++ = 10;
    *ps++ = *pc;
    if (*pc==62 || *pc==123 || *pc==125 || *pc==59) *ps++ = 10;
    pc++;
}
*ps++ = 34;
strncpy(ps, pat, pc - pat);
ps += pc - pat;
*ps++ = '!';
strcpy(ps, pc+1);
ps += len - (pc - pat) -1;
*ps++ = 34;
while (*++pc)
{
    if (*pc==123) *ps++ = 10;
    *ps++ = *pc;
    if (*pc==62 || *pc==123 || *pc==125 || *pc==59) *ps++ = 10;
}
*ps = 0;
printf(self);
free(self);
return 0;
}





从“char* pat = ”那一行至连续的下数5行代码,源文件中是在一行上,这里自动折行了。

运行的结果self.exe >> out.txt,self.c 和 out.txt两个文件在字节水平上一模一样,完全相同。

注意:self.c中不能增删空行、空格和TAB,否则打印的内容会不完全一致,除非你看懂了程序,知道怎么修改pat字符串。

我是在VC6上编写和测试的。

Self.rar

25.17 KB, 下载次数: 64

源代码和执行程序

论坛徽章:
0
2 [报告]
发表于 2009-03-01 21:17 |只看该作者
看晕了我。

论坛徽章:
0
3 [报告]
发表于 2009-03-01 21:23 |只看该作者
确实有趣,不过几年前就搞过

只是没搞出来而已

论坛徽章:
0
4 [报告]
发表于 2009-03-01 21:27 |只看该作者
混乱c代码比赛的,c专家编程上好像有

论坛徽章:
0
5 [报告]
发表于 2009-03-02 04:25 |只看该作者
下面这个也行:

  1. char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
复制代码

论坛徽章:
0
6 [报告]
发表于 2009-03-02 09:55 |只看该作者
《C语言参考手册》上有

论坛徽章:
0
7 [报告]
发表于 2009-03-02 09:55 |只看该作者
原帖由 emacsnw 于 2009-3-2 04:25 发表
下面这个也行:

char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}

emacsnw大侠,这个代码太强悍了,太强悍了,太说明问题了,拜过了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

论坛徽章:
0
8 [报告]
发表于 2009-03-02 10:02 |只看该作者
俺智商低,没看明白。。。。

论坛徽章:
0
9 [报告]
发表于 2009-03-02 10:37 |只看该作者
这有什么应用啊?

论坛徽章:
0
10 [报告]
发表于 2009-03-02 17:51 |只看该作者
这是C语言之父写过的一篇论文,名字叫self什么的,看过很久了,现在忘光了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP