打印自己源代码的程序
来源:互联网 发布:java 获取电脑配置 编辑:程序博客网 时间:2024/05/16 14:27
几天前网上看到一篇用Java写的打印自己源代码的程序,感觉挺有意思,于是写了一个C语言打印自己的程序。所谓打印自己,就是不是通过打开源代码文件,而是代码的目的就是打印出和源代码一样的字符来。
这是我写的:
#include <stdio.h>
#include <string.h>
static char *stats[100];
static int count=0;
char *convert(char *str)
{
int len = strlen(str);
char *newStr = new char[len+100];
int i=0, j=0;
while(i<=len)
{
if (str[i] == 34)
{
newStr[j] = 92; newStr[j+1] = 34;
i++; j+=2;
}
else
{
newStr[j] = str[i];
i++; j++;
}
}
return newStr;
}
void p1(char *str)
{
stats[count++] = convert(str);
printf("%s%c", str, 10);
}
void p2()
{
for(int i=0; i<count; i++)
{
printf("p1(%c%s%c);%c", 34, stats[i], 34, 10);
}
printf(" p2();%c", 10);
printf("}%c", 10);
}
int main()
{
p1("#include <stdio.h>");
p1("#include <string.h>");
p1("static char *stats[100];");
p1("static int count=0;");
p1("char *convert(char *str)");
p1("{");
p1(" int len = strlen(str);");
p1(" char *newStr = new char[len+100];");
p1(" int i=0, j=0;");
p1(" while(i<=len)");
p1(" {");
p1(" if (str[i] == 34)");
p1(" {");
p1(" newStr[j] = 92; newStr[j+1] = 34;");
p1(" i++; j+=2;");
p1(" }");
p1(" else");
p1(" {");
p1(" newStr[j] = str[i];");
p1(" i++; j++;");
p1(" }");
p1(" }");
p1(" return newStr;");
p1("}");
p1("void p1(char *str)");
p1("{");
p1(" stats[count++] = convert(str);");
p1(" printf(/"%s%c/", str, 10);");
p1("}");
p1("void p2()");
p1("{");
p1(" for(int i=0; i<count; i++)");
p1(" {");
p1(" printf(/"p1(%c%s%c);%c/", 34, stats[i], 34, 10);");
p1(" }");
p1(" printf(/" p2();%c/", 10);");
p1(" printf(/"}%c/", 10);");
p1("}");
p1("int main()");
p1("{");
p2();
}
不料,今天在网上看到某强人写的,才知道可以更加简洁搞定
#include <stdio.h>
int main()
{
char *p ="#include <stdio.h>%c int main(){char *p=%c%s%c; printf(p, 10, 34, p, 34);}";
printf(p, 10, 34, p, 34);
}
后一个程序要简洁漂亮得多了
这是我写的:
#include <stdio.h>
#include <string.h>
static char *stats[100];
static int count=0;
char *convert(char *str)
{
int len = strlen(str);
char *newStr = new char[len+100];
int i=0, j=0;
while(i<=len)
{
if (str[i] == 34)
{
newStr[j] = 92; newStr[j+1] = 34;
i++; j+=2;
}
else
{
newStr[j] = str[i];
i++; j++;
}
}
return newStr;
}
void p1(char *str)
{
stats[count++] = convert(str);
printf("%s%c", str, 10);
}
void p2()
{
for(int i=0; i<count; i++)
{
printf("p1(%c%s%c);%c", 34, stats[i], 34, 10);
}
printf(" p2();%c", 10);
printf("}%c", 10);
}
int main()
{
p1("#include <stdio.h>");
p1("#include <string.h>");
p1("static char *stats[100];");
p1("static int count=0;");
p1("char *convert(char *str)");
p1("{");
p1(" int len = strlen(str);");
p1(" char *newStr = new char[len+100];");
p1(" int i=0, j=0;");
p1(" while(i<=len)");
p1(" {");
p1(" if (str[i] == 34)");
p1(" {");
p1(" newStr[j] = 92; newStr[j+1] = 34;");
p1(" i++; j+=2;");
p1(" }");
p1(" else");
p1(" {");
p1(" newStr[j] = str[i];");
p1(" i++; j++;");
p1(" }");
p1(" }");
p1(" return newStr;");
p1("}");
p1("void p1(char *str)");
p1("{");
p1(" stats[count++] = convert(str);");
p1(" printf(/"%s%c/", str, 10);");
p1("}");
p1("void p2()");
p1("{");
p1(" for(int i=0; i<count; i++)");
p1(" {");
p1(" printf(/"p1(%c%s%c);%c/", 34, stats[i], 34, 10);");
p1(" }");
p1(" printf(/" p2();%c/", 10);");
p1(" printf(/"}%c/", 10);");
p1("}");
p1("int main()");
p1("{");
p2();
}
不料,今天在网上看到某强人写的,才知道可以更加简洁搞定
#include <stdio.h>
int main()
{
char *p ="#include <stdio.h>%c int main(){char *p=%c%s%c; printf(p, 10, 34, p, 34);}";
printf(p, 10, 34, p, 34);
}
后一个程序要简洁漂亮得多了
- 打印自己源代码的程序
- 打印自己的程序
- 打印自身源代码的程序
- 用C#打印自己的源代码
- 一段自己打印自己的c程序
- 一段自己打印自己的c程序
- 一段自己打印自己的c程序
- 一段自己打印自己的c程序
- 程序自己打印自己
- 程序自己打印自己
- 趣味程序:打印自己代码的程序
- 程序将自己的源代码写入记事本
- 打印自己的一段漂亮C程序
- 打印自身源代码的程序,一个通用方法
- 打印C程序的自身源代码(译)
- 自己写的一个vpn监听程序的源代码
- 一个有意思的东东 linux c打印自己的程序
- 一个有意思的东东,linux C 打印自己的程序
- RMS从入门到精通之四
- 软件工程75个要点(转)
- 生成静态文件的新闻系统核心代码(.net C#)
- struts的国际化编程问题
- 生成静态文件的新闻系统核心代码(.net C#)二
- 打印自己源代码的程序
- J2EE vs .NET
- 编写健壮程序,首先正确的理解类库
- 实战揭秘:开发.Net平台应用系统框架
- JFreeChart介绍及经典入门资料
- 推荐一个免费的支持JSP、Servlet的web hosting
- JAVA中最常用的异常类
- 换blog咯!
- 一个sql语句(left outer join)