BrainFuck下的HappyNewYear

今天看了下一個語言叫BrainFuck,寫了個打印「Happy New Year!」的程序app

(今天是2015年1月1日,順便祝你們新年快樂哈)ui

1.寫在前面spa

BrainFuck的主頁在: http://www.muppetlabs.com/~breadbox/bf/code

我找了個BrainFuck代碼解釋器,能夠在Win7下運行,看圖標應該是VB6.0寫的,下載地址在:教程

http://esoteric.sange.fi/brainfuck/compiled/win/BFI.exeip

BrainFuck的語法規則和簡易教程能夠參考英文維基百科的頁面:ci

https://en.wikipedia.org/wiki/Brainfuck字符串

下面這個表就摘自這個維基百科頁面,BrainFuck由這些字符構成get

命令(BrainFuck Command) 等價的C語句(C Equivalent)
程序開始(Program Start) char array[無窮大] = { 0 };
char *ptr = array;
> ++ptr;
< --ptr;
+ ++*ptr;
- --*ptr;
. putchar(*ptr);
, *ptr = getchar();
[ while(*ptr) {
] }

2.程序代碼input

1)縮進版本的代碼

>++++++++
[
    >++++
    >+++++++++
    >+++++++++++++
    >++++++++++++++
    <<<<-
]
>>>----<<<

>>.>---.>..+++++++++.<<<.<
>>++++++.>++++.>--.<<<.<
>>+++++++++++.>.----.>-----.<<<+.

BrainFuck的輸出理念,就是你能夠申請若干個空間,經過「>」和「<」在申請到的空間按申請的前後順序遍歷,用「+」和「-」能夠把改空間的值增1或減1,用「[」和「]」能夠指定一個循環節,這個循環在當前所處空間值爲0的時候結束,全部空間,初始值都是0,用「>」第一次移動到它的身上時就可使用了。一個空間的值能夠用「.」輸出該值做爲ASCII碼時所對應的字符(還有個「,」字符用於輸入,不過本段代碼中並無用到)

2)不縮進版本的代碼

BrainFuck不強制要求縮進,全部的字符能夠寫到同一行,下面這段代碼和1)中的效果同樣:

>++++++++[>++++>+++++++++>+++++++++++++>++++++++++++++<<<<-]>>>----<<<>>.>---.>..+++++++++.<<<.<>>++++++.>++++.>--.<<<.<>>+++++++++++.>.----.>-----.<<<+.

運行結果以下圖:

能夠看到,輸出了字符「Happy New Year!」

3.經過VBS腳本批量生成此類程序

一個用於輸出字符的BrainFuck,若是要作得簡短,須要在以前進行一些手工的計算

若是不考慮程序的長短,那麼批量生成就變得簡單得多

下面這個VBS腳本,能夠從一個寫有字符串的文本文件input.txt讀取信息,輸出到另外一個文本文件output.txt中

輸出的output.txt中保存着大量相似「>+++...(輸入字符對應ASCII碼數量個+)...+++.」的代碼段

Option Explicit

'讀取文件input.txt保存到InputString
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
Dim FRead : Set FRead = FSO.OpenTextFile("input.txt", 1)
Dim InputString : InputString = FRead.ReadAll
FRead.Close

Dim OutputString 

'逐字比對,生成對應BrainFuck代碼
Dim i : For i = 1 to len(InputString)

    Dim TempChar : TempChar = mid(InputString, i, 1)    
    OutputString = OutputString & ">"
    Dim j : For j = 0 To Asc(TempChar) - 1
        OutputString = OutputString & "+"
    Next
    OutputString = OutputString & "." & VbCrLf

Next

'將OutputString的內容寫入到文件output.txt
Dim FWrite : Set FWrite = FSO.OpenTextFile("output.txt", 2, 1)
FWrite.Write OutputString
FWrite.Close

WScript.Echo "操做完畢"

如今假設input.txt中保存了一個字符串「Hello World!」

執行完上面這段VBS腳本後,生成的output.txt中會有下面的文字:

>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>++++++++++++++++++++++++++++++++.
>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>++++++++++++++++++++++++++++++++.
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
>+++++++++++++++++++++++++++++++++.

把這段代碼放入代碼解釋器中,也能獲得「Happy New Year!」的輸出,就是不如以前手算的代碼簡潔罷了 :-)

END

相關文章
相關標籤/搜索