打印自己代码的程序

打印自己代码的程序
程序运行时可以打印自己源代码的程序叫叫 Quine:
具体的描述可以参见维基百科:http://en.wikipedia.org/wiki/Quine_%28computing%29

以下是几个在网上找的例子,由于手边儿有现在的 C# 环境,所以找是 C# 写的程序。
一个 C# 版的
----------------------------------------

using System; namespace Quine { class Class1 {
const string a="using System; namespace Quine { class Class1 {";
const string b="[STAThread] static void Main(string[] args) { string q=Convert.ToChar(34).ToString(); System.Console.WriteLine(a);System.Console.WriteLine(s + ‘a‘ + ‘=‘ + q + a + q + ‘;‘);System.Console.WriteLine(s + ‘b‘ + ‘=‘ + q + b + q + ‘;‘); System.Console.WriteLine(s + ‘s‘ + ‘=‘ + q + s + q + ‘;‘); System.Console.Write(b); } } }";
const string s="const string ";
[STAThread] static void Main(string[] args) { string q=Convert.ToChar(34).ToString(); System.Console.WriteLine(a); System.Console.WriteLine(s + ‘a‘ + ‘=‘ + q + a + q + ‘;‘); System.Console.WriteLine(s + ‘b‘ + ‘=‘ + q + b + q + ‘;‘); System.Console.WriteLine(s + ‘s‘ + ‘=‘ + q + s + q + ‘;‘); System.Console.Write(b); } } }

----------------------------------------
源自 http://blog.vyvojar.cz/radim/archive/2004/06/24/1325.aspx

另一个 C# 版的
----------------------------------------

using System;
class Quine
{
    static void Main()
    {
        string s = "using System;{4}class Quine{4}{2}{4}{5}static void Main(){4}{5}{2}{4}{5}{5}string s = {1}{0}{1};{4}{5}{5}Console.Write(string.Format(s,s,(char)34,(char)123,(char)125,(char)10,(char)9));{4}{5}{5}Console.ReadKey(true);{4}{5}{3}{4}{3}";
        Console.Write(string.Format(s, s, (char)34, (char)123, (char)125, (char)10, (char)9));
        Console.ReadKey(true);
    }
}

----------------------------------------
源自 http://verydemo.com/demo_c92_i165129.html

再来一个短一点儿的
----------------------------------------

class P{static void Main(){string s="class P{{static void Main(){{string s={1}{0}{1};System.Console.WriteLine(s,s,(char)34);}}}}";System.Console.WriteLine(s,s,(char)34);}}

----------------------------------------
源自 http://www.cnblogs.com/Ninputer/archive/2006/04/04/366554.html

再写个 C 的:
----------------------------------------

#include <stdio.h>
char *s = "#include <stdio.h>%cchar *s=%c%s%c;%cint main(){printf(s,10,34,s,34,10);}";
int main(){printf(s,10,34,s,34,10);}

----------------------------------------

简单的说下这种打印源代码的共同的模式,就是在格式化打印自己的时候,能把格式化本身传给它自己。
就像上面的 *s=%c%s%c,这里的 %s 接收的还是它自己。

试试来个 Lua2.4 的,这个比较短,更容易看出上面说的那个模式。
----------------------------------------

str="str=%c%s%c%cwrite(format(str,34,str,34,10))"
write(format(str,34,str,34,10))

----------------------------------------

时间: 2025-01-14 09:25:15

打印自己代码的程序的相关文章

程序打印自身代码的两种方法

程序打印自身代码的两种方法 方法一:利用fopen,打开可执行程序对应的source code file /***************************************** code writer : EOF code file : print_my_self.c code date : 2014.08.01 e-mail: [email protected] code purpose : Aha, print out myself! *********************

1、打印二进制机器码,程序内存分析,大端序小端序,指针数组,数组指针,数组的三种访问方式,typedef,#if-0-#endif,求数组大小,括号表达式

 1.打印二进制机器码(分别表示32位的和64位的) #include <stdio.h> /*按照8位的长度打印一个数值*/ void dis8bit(char val) { int bit = 8; while(bit--) { if(1<<bit&val){ printf("1"); } else { printf("0"); } if(!(bit%4)) printf(" "); } putchar(1

Java Date类和Calendar类的一个控制台打印日期的小程序

Java Date类和Calendar类的一个打印日期的小程序,可以直接用. package com.boy.Idate.calendar; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 控制台可视化日历 * @author 田硕 */ public class VisualCalendar {

python批量同步web服务器代码核心程序

#!/usr/bin/env python #coding:utf8 import os,sys import md5,tab from mysql_co.my_db import set_mysql from ssh_co.ssh_connect import sshd from ssh_co.cfg.config import ssh_message,item_path from file import findfile def my_mysql(): db_file={} my_conne

代码块练习题:看代码写程序的执行结果。

1 /* 2 代码块练习题: 3 看代码写程序的执行结果. 4 5 输出结果是: 6 林青霞都60了,我很伤心 7 我是main方法 8 Student 静态代码块 9 Student 构造代码块 10 Student 构造方法 11 Student 构造代码块 12 Student 构造方法 13 */ 14 15 class Student { 16 static { 17 System.out.println("Student 静态代码块"); 18 } 19 20 { 21 S

一套代码小程序&amp;Web&amp;Native运行的探索05——snabbdom

接上文:一套代码小程序&Web&Native运行的探索04——数据更新 对应Git代码地址请见:https://github.com/yexiaochai/wxdemo/tree/master/mvvm 参考: https://github.com/fastCreator/MVVM(极度参考,十分感谢该作者,直接看Vue会比较吃力的,但是看完这个作者的代码便会轻易很多,可惜这个作者没有对应博客说明,不然就爽了) https://www.tangshuang.net/3756.html ht

一套代码小程序&amp;Web&amp;Native运行的探索06——组件系统

接上文:一套代码小程序&Web&Native运行的探索05——snabbdom 对应Git代码地址请见:https://github.com/yexiaochai/wxdemo/tree/master/mvvm 参考: https://github.com/fastCreator/MVVM(极度参考,十分感谢该作者,直接看Vue会比较吃力的,但是看完这个作者的代码便会轻易很多,可惜这个作者没有对应博客说明,不然就爽了) https://www.tangshuang.net/3756.htm

第一次自己去做一个程序,而不是照着书本上的代码打程序。

我之前想过用窗体做这个关于运算程序出来,因为那样可以让用户更加一目了然,更加方便的使用.但是一直在研究了好久之后,发现不知道该怎么样让用户在点击+-*/按钮的时候能够自动列出算式以及自动运用于计算.所以最终还是选择了用控制台应用程序来做. 这个程序大概是可以应用于小学,让低年级的小学生自己在电脑上做作业,既能够开发学生的思维,也可以借助小学生对电脑的兴趣来让他们对学习动脑更加努力. 设计思路大概就是运用之前学过树上的加法随机数运算,改变运算的规律,算法.运用计算机的计算功能以及if.else语句

iphone编程,使用代码结束程序

(ios 4环境) 1.使用代码可以结束自己只有一种方式,而且apple不建议这样用,就是调用exit()函数.在2.0以前uiapplication类中有一个方法可以调用,但是现在已经没有了,而且如果你强制使用2.0以前的编译环境,apple会拒绝你发布这个程序到app store. 2.用户按下home键后,程序转入后台运行. 3.连续按两次home键后,在iphone屏幕下面会出现一个程序列表,这个列表,我原先以为是转入后台运行的程序列表,通过试验结果看到,我错了,这个只是最近使用的程序列