- UID
- 364360
- 在线时间
- 0 小时
- 最后登录
- 2013-6-2
- 注册时间
- 2013-2-10
- 宅魂
- 112 点
- 贡献
- 47 点
- 宅币
- 462 枚
- 宅の石(入宅度)
- 0 块
- 元气(技能点)
- 2 点
- 活跃
- 0 ℃
- 听众
- 5
- 收听
- 0
该用户从未签到
Continue
- 积分
- 1039
|
首发地址:(我的博客)http://me.ivydom.com/archives/brainfuckinter.html
Brainfuck是一种极小化的计算机语言
下面是这八种状态的描述,其中每个状态由一个字符标识:[1]
字符
含义
>
指针加一
<
指针减一
+
指针指向的字节的值加一
-
指针指向的字节的值减一
.
输出指针指向的单元内容(ASCⅡ码)
,
输入内容到指针指向的单元(ASCⅡ码)
[
如果指针指向的单元值为零,向后跳转到对应的]指令的次一指令处
]
如果指针指向的单元值不为零,向前跳转到对应的[指令的次一指令处
(按照更节省时间的简单说法,"]“也可以说成“向后跳转到对应的”["状态”。这两解释是一样的。)
(第三种同价的说法,"["意思是"向前跳转到对应的"]“”,]意思是”向后跳转到对应的[指令的次一指令处,如果指针指向的字节非零。")
Brainfuck程序可以用下面的替换方法翻译成C语言(假设ptr是char*类型):
Brainfuck
C
>
++ptr;
<
--ptr;
+
++*ptr;
-
--*ptr;
.
putchar(*ptr);
,
*ptr =getchar();
[
while (*ptr) {
]
}
当前位置清零
[-] 将当前指针的值归零
之前位置清零
[[-]<] 将当前指针以及之前的指针归零
字符I/O
,. 从键盘读取一个字符并输出到屏幕上。
简单的循环
,[.,] 这是一个连续从键盘读取字符并回显到屏幕上的循环。注意,这里假定0表示输入结束,事实上有些系统并非如此。以-1和”未改变”作为判断依据的程序代码分别是”,+[-.,+]“和”,[.[-],]”。
指针维护
>,[.>,] 通过移动指针保存所有的输入,供后面的程序使用。
加法
[->+<]
把当前位置的值加到后面的单元中(破坏性的加,它导致左边的单元被归零)。
以上摘自百度百科
[mw_shl_code=vbnet,true]ublic Class Form1
Const codes = "+++++[>++++++[>+?<-]<-]"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim aa(4) As Byte
'putnum(aa, "222")
execute(codes)
End Sub
Sub putnum(ByVal temp() As Byte, ByVal datas As Byte)
Dim a, tmp As String
Dim i As Integer
a = datas.ToString
For i = 1 To Len(a)
tmp = Mid(CByte(a), i, 1)
Debug.WriteLine(tmp)
temp(i) = Asc(tmp)
Debug.WriteLine(Asc(tmp))
Next
temp(i) = 32
End Sub
Sub execute(ByVal codesignal As String)
Dim length, pcode, pmemory, match, index As Integer
Dim memory(1024) As Byte
Dim test As String
Dim n(1024) As Byte
Dim myEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
Dim syllablecode As Byte() = myEncoding.GetBytes(codesignal)
Debug.WriteLine(syllablecode)
length = syllablecode.Length
pcode = 1
pmemory = 1
TextBox2.Text = ""
TextBox1.Focus()
index = 0
While pcode <= length
If syllablecode(pcode) = 60 Then '<
pmemory -= 1
ElseIf syllablecode(pcode) = 62 Then '>
pmemory += 1
ElseIf syllablecode(pcode) = 43 Then '+
memory(pmemory) += 1
ElseIf syllablecode(pcode) = 45 Then '-
memory(pmemory) -= 1
ElseIf syllablecode(pcode) = 44 Then ',
test = InputBox("", , "0")
memory(pmemory) = Asc(test)
ElseIf syllablecode(pcode) = 46 Then ',
n(index) = memory(pmemory)
TextBox2.Text = n.ToString
ElseIf syllablecode(pcode) = 91 Then '[
match = 1
If memory(pmemory) = 0 Then
While pcode <= length
pcode += 1
If syllablecode(pcode) = 93 Then
match -= 1
If (match = 0) Then
Exit While
End If
ElseIf syllablecode(pcode) = 91 Then
match += 1
End If
End While
End If
ElseIf syllablecode(pcode) = 93 Then ']
match = 1
If memory(pmemory) <> 0 Then
While pcode > length
pcode -= 1
If syllablecode(pcode) = 91 Then
match -= 1
If (match = 0) Then
Exit While
End If
ElseIf syllablecode(pcode) = 93 Then
match += 1
End If
End While
End If
ElseIf syllablecode(pcode) = 63 Then '?
putnum(n, memory(pmemory))
TextBox2.Text = n.ToString
Else
MsgBox("代码有误!")
Exit While
End If
index += 1
pcode += 1
End While
End Sub
End Class[/mw_shl_code] |
评分
-
查看全部评分
|