有人問我在採訪中瞭解到 如何顯示整個源程序爲輸出當同一程序在執行在執行時顯示源程序作爲輸出?
- 一般我們使用
File-handling
概念對於這一點,有沒有其他的辦法完成這項任務
對此有何建議?
更新的解決方案: 作爲我研究的東西我有一個解決方案,我們可以通過Quine
- 它執行代碼,並打印源程序做到這一點,但我們需要給整個程序作爲輸入
String array
..as @Basile評論如下
有人問我在採訪中瞭解到 如何顯示整個源程序爲輸出當同一程序在執行在執行時顯示源程序作爲輸出?
File-handling
概念對於這一點,有沒有其他的辦法完成這項任務對此有何建議?
更新的解決方案: 作爲我研究的東西我有一個解決方案,我們可以通過Quine
String array
..as @Basile評論如下這是運行將顯示itself.The祕密當一個程序是創建包含linesof程序字符串數組,除了字符串在數組本身。所以你有第一個循環來顯示Strings數組之前的代碼行。然後你有一個循環來顯示數組Strings,然後你有一個循環來顯示錶示代碼剩餘行的數組Strings。
請注意,空格字符的ASCII值是32,雙引號的ASCII值是34。下面創建'quote'和'sixSpaces'的原因是,當顯示它們時,是爲了避免使用轉義字符\,當試圖顯示引號時。
public class ProgramThatDisplaysItself {
public static void main(String[] args) {
char space = (char)32;
char quote = (char)34;
String emptyStr = new String();
String spaceStr = String.valueOf(space);
String sixSpaces =
emptyStr.concat(spaceStr).concat(spaceStr).concat(spaceStr)
.concat(spaceStr).concat(spaceStr).concat(spaceStr);
String linesOfCode[] = {
"package programthatdisplaysitself;",
"",
"public class ProgramThatDisplaysItself {",
"",
" public static void main(String[] args) {",
" char space = (char)32;",
" char quote = (char)34;",
" String emptyStr = new String();",
" String spaceStr = String.valueOf(space);",
" String sixSpaces = ",
" emptyStr.concat(spaceStr).concat(spaceStr).concat(spaceStr)",
" .concat(spaceStr).concat(spaceStr).concat(spaceStr);",
"",
" String linesOfCode[] = {",
// Note: here's where the String array itself is skipped
" };",
"",
" for (int i = 0; i < 14; i++) {",
" System.out.println(linesOfCode[i]);",
" } // end for i",
"",
" for (int j = 0; j < linesOfCode.length; j++) {",
" System.out.println(sixSpaces + quote + linesOfCode[j] + quote + ',');",
" } // end for j",
"",
" for (int k = 14; k < linesOfCode.length; k++) {",
" System.out.println(linesOfCode[k]);",
" } // end for k",
"",
" } // end main()",
"",
"} // end class ProgramThatDisplaysItself",
}; // end linesOfCode[]
//display the lines until the String array elements
for (int i = 0; i < 14; i++) {
System.out.println(linesOfCode[i]);
} // end for i
//display the String array elements
for (int j = 0; j < linesOfCode.length; j++) {
System.out.println(sixSpaces + quote + linesOfCode[j] + quote + ',');
} // end for j
//display the lines after the String array elements
for (int k = 14; k < linesOfCode.length; k++) {
System.out.println(linesOfCode[k]);
} // end for k
} // end main()
} // end class ProgramThatDisplaysItself
一旦它在字節碼我不認爲你可以從它得到的源代碼。 我可以看到實現這一目標的唯一方法是如果程序持有其代碼的字符串副本或從用於編譯和打印的文件加載相同的源代碼。
編輯:檢查奎因計算機後,該示例是在程序中使用一個字符串與代碼的副本。
你能定義任何例子嗎...... – kark
你可以這樣編碼來打印源代碼。這是你在找什麼?
class A{
public static void main(String args[]) throws Exception {
FileReader f = new FileReader(filePathOfThisClass);
BufferedReader b = new BufferedReader(f);
String s= null;
while ((str = b.readLine()) != null)
System.out.println(s);
}
}
UPDATE
根據巴西萊的評論我很好奇。因此我找到了幾個選擇。從喹頁檢查此頁面例如programes
這是我們做的..但我問的是..有沒有其他辦法 – kark
@kark你檢查了basile的評論? – SpringLearner
對不起,我誤解了你的問題..這是我知道的唯一方法..請分享,如果你發現任何其他選項。 – stinepike
上[奎因程序]見WIKIPAGE(http://en.wikipedia.org/wiki/Quine_%28computing%29) –
不太清楚這個問題值得多的選票(如表現出良好的努力,找到解決方案)如果有必要的信息很容易找到:「自我+印刷+程序」 - http://www.bing.com/search?q=self+printing+program –
@ BasileStarynkevitch..fine,但似乎我們正在給源代碼作爲預定義的輸入 – kark