- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试编写一个 brainfuck 解释器,但我缺少一些上下文或其他内容。应该调用以处理“+><>
”等转换的函数应该是:
std::vector<int> Interpreter::interpret(const std::string &src_,
const std::vector<int> & input_)
程序测试如下:
int main()
{
std::vector<int> res;
// output: 1
res = interpret("+.");
for (auto i : res)
std::cout << i << " ";
2
// output: 2
res = interpret(",.", {2});
for (auto i : res)
std::cout << i << " ";
return 0;
}
http://www.muppetlabs.com/~breadbox/bf/
我真的不明白这是在做什么。我看过其他视频,但这没有任何意义。有人可以解释一下目标吗?
如果给函数一个要翻译的数组,那么 30000 字节的数组有什么意义?
编辑:我应该编写 C++ 代码,将字符转换为 brainfuck 命令的字符,并且它们应该在一些 30000 字节的数组上执行相应的命令,以及这意味着什么。
编辑:提供说明
Abstract Write a simple interpreter for Brainfk. 1 Introduction
A Brainfk program has an implicit byte pointer, called the pointer, which is free to move around within an array of 30000 bytes, initially all set to zero. The pointer itself is initialized to point to the beginning of this array. The Brainfuck programming language consists of eight commands, each of which is represented as a single character.
•
>
Increment the pointer.
•<
Decrement the pointer.
•+
Increment the byte at the pointer.
•-
Decrement the byte at the pointer.
•.
A dot, output the byte at the pointer.
•,
A comma, input a byte and store it in the byte at the pointer.
•[
Jump forward past the matching ] IF the byte at the pointer is zero.
•]
Jump backward to the matching [ UNLESS the byte at the pointer is zero.For example, one version of the "Hello, World!" program in Brainfk is
++++++++++[>+++++++>++++++++++>+++>+<<<<-]
>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.
+++.------.--------.>+.>.2 Requirement
2.1 Test Program
I will use program to test and grade your code in batch. So please double check your function signature. Failure to run properly may impact your project grade. The entry function will all have the nameinterpret
. And you may implement as many other helper functions as you want. The following sections elaborate on the specifications.2.1.1 C++ I would use C++11 (
g++ -std=c++11 ...
) to test your program. So feel free to employ some of the recent goodies added to C++, e.g., lambda function, array initialization, etc. For convenience, please separate your declaration and implementation code inbf.h
andbf.cpp
. The function signature isstd::vector<int> interpret(const std::string
&src, const std::vector<int> &input = {});My test program would look like
int main()
{
std::vector<int> res;
// output: 1
res = interpret("+.");
for (auto i : res)
std::cout << i << " ";
// output: 2
res = interpret(",.", {2});
for (auto i : res)
std::cout << i << " ";
return 0;
}
编辑:我目前拥有的:
BFK.h
#pragma once
#include <vector>
#include <iostream>
using namespace std;
char arr[30000];
char* p = arr;
void incPtr();
void decPtr();
void incByte();
void decByte();
void printByte();
void setByte();
void jumpF();
void jumpB();
std::vector<int> interpret(const std::string &src,
const std::vector<int> & input = {});
BFK.cpp
#include "BFK.h"
void incPtr() {
p++;
}
void decPtr() {
p--;
}
void incByte() {
(*p)++;
}
void decByte() {
(*p)--;
}
void printByte() {
std::cout << *p;
}
void setByte() {
std::cin >> *p;
}
void jumpF() {
if (*p == 0) {
}
}
void jumpB() {
}
std::vector<int> interpret(const std::string &src_,
const std::vector<int> & input_){
int i = 0;
int max = src_.size();
while (i < max) {
switch (src_[i]) {
case '>':
incPtr();
break;
case '<':
decPtr();
break;
case '+':
incByte();
break;
case '-':
decByte();
break;
case '.':
printByte();
break;
case ',':
setByte();
break;
case '[':
jumpF();
break;
case ']':
jumpB();
break;
}
}
return input_;
}
您应该能够在不实例化任何东西的情况下调用解释,所以我不知道还有另一种方法可以将它们组合在一起。我还没有实现跳转功能。
最佳答案
What is the point of the 30000 byte array if the function is given an array to translate?
假设您得到两个数字 - 2 和 5 - 并希望您的 Brainfuck 程序打印它们的和。
当您所能做的就是操纵当前单元格中的值并“选择”当前单元格时,您是怎么做的?当然,您有时会需要一些临时内存。
为了在两个单独的单元格中有两个值,为加法做准备,您可以这样做:
,>,
如果用户输入 2
和 3
(十进制,不是 ascii)那么 Brainfuck 程序存储器或磁带的前两个字节将如下所示:
[2, 3, 0, 0, ...]
// ^ This is where our tape pointer is now. We incremented it with `>`
很好,现在加法呢?一种解决方案是使用 Brainfuck 循环。
让我们将两个自然整数相加,除了递增和递减值外什么都不做:
int a = 2, b = 3
while (a != 0)
{
b += 1
a -= 1
}
基本上,我们正在递减第一个值直到它达到零,随着它的减少,我们正在递增第二个值。随着时间的推移,这将显示:
a = 2, b = 3
a = 1, b = 4
a = 0, b = 5
a == 0, break
因此,我们确实得到了 2 + 3 = 5
.这很简单,可以在 brainfuck 中实现。
, Get value for first cell (a)
>, Get value for second cell (b)
< Move back to a
[ Loop until a == 0
>+ Decrement b
<- Increment a
We are still at a at this point, so everything is alright
] Loop again if a != 0
>. Print the final result (sum of a plus b)
...所有这些都是为了演示如何使用 Brainfuck 的磁带存储器来做实际的事情。
我相信很多 Brainfuck 程序都将磁带作为堆栈进行操作。有趣的是,您实际上并不需要了解 Brainfuck 程序采用的以可用方式临时存储值的技术。
让我们看看 Brainfuck 解释器如何粗略地与上述程序一起工作,一个指令一个指令。
*
在堆栈中的值之后意味着它是堆栈指针现在指向的位置。
我会将其中一些分组,以免让这段时间太长。
,
- tape[index] = input()
,堆栈现在是 [2*, 0, 0, ...]
>
- index += 1
,堆栈现在是 [2, 0*, 0, ...]
,
- tape[index] = input()
,堆栈现在是 [2, 3*, 0, ...]
<
- index -= 1
,堆栈现在是 [2*, 3, 0, ...]
[
- if (tape[index] == 0) { skipToLoopEnd(); }
,不跳转(2 == 0
为假),堆栈保持不变>+
- 堆栈现在是 [2, 4*, 0, ...]
<-
- 堆栈现在是 [1*, 4, 0, ...]
]
- if (tape[index] != 0) { skipToLoopBegin(); }
, 跳转( 1 != 0
为真), 栈左边不变>+
- 堆栈现在是 [1, 5*, 0, ...]
<-
- 堆栈现在是 [0*, 5, 0, ...]
]
- if (tape[index] != 0) { skipToLoopBegin(); }
, 不跳转(0 != 0
为假),堆栈保持不变>
- index += 1
,堆栈现在是 [0, 5*, 0, ...]
.
- print(tape[index])
, 打印 5
!不用说,我没有意识到这个问题来自 2015 年! (耶!)至少有人会发现这在未来很有用...... :^)
关于c++ - 有人可以解释 brainfuck 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33991173/
是 Brainfuck如果单元是位,并且 + 和 - 操作只是翻转一点,那么图灵完备?是否有一个简单的证据表明无论单元大小如何,类 Brainfuck 的语言都是图灵完备的,还是我需要考虑一个模拟图灵
作为我的 Brainf*ck 实现的调试器/测试程序,我创建了以下计数循环: +[[>+]+] 对于单字节、环绕单元和 30k 单元,这会创建一个无限计数循环。它将每个单元格设置为 1,然后将每个单元
我想知道是否可以只用代码开头的数字 k 来计算 brainfuck 中 1+2+3+...+k 的总和? 例如是否可以像这样做 1+2+3: +++> (这里的代码创建了一个二和三个相加,创建一个并相
我正在研究一个直接解释 brainfuck 的物理面包板 8 位 CPU。 语言规范表明左括号和右括号都有逻辑: [ => Jump to matching ] If Zero ] => Jump t
我正在尝试在 Brainfuck 中制作一个我认为也称为“Brainflake”的程序,它将添加两个用 ASCII 数字字符输入的单位十进制整数,并在输出中以 ASCII 数字字符显示总和。我将如何着
例如,如果数组像 0 0 0 0 ... 0 0[n]s o m e d a t a 4 9 9 9 9 9 9 8 3 7 ...,如何移动指针通过 n,在指针移动后不改变 s o m e d a
有人把这个发给我,并声称这是 Brainfuck 中的一个 Hello World (我希望如此......) ++++++++++[>+++++++>++++++++++>+++>+++.>+.++
我正在尝试使用 BrainFuck 编写一个程序,该程序可以读取最多为 9 的两个数字,计算它们的总和,然后将结果打印出来,例如3 & 5 给出结果 8 。 我只是想了解 BF 语言,但它看起来比我想
有人可以在程序设计语言Brainf * ck中发布一个代码段,以将两个一位数字相乘吗? 最佳答案 ,>, go to cell #2 [ ->+>+> go to cell #4 [ -
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
你能创建一种像 Brainfuck 这样只有一个符号的编程语言吗? 最佳答案 是的,以前已经做过 - 请参阅 Unary . 基本上这是一种奇怪的 Brainfuck 编码。将每个 BF 命令视为一个
这是我正在使用的代码,它测试“b”的输入,如果有其他输入,应该输出“Y”。 拜托,没有人问我为什么选择使用“Y”作为错误值.. > , ----- ---- ----- ---
我有以下正在通过最小测试套件的 Brainfuck 解释器。除了一个相当大的问题,比如打印斐波那契数列似乎失败了(套件中的最后一个测试)。我解释执行失败的 brainfuck fibonacci 代码
我决定使用 Borland 的 CppBuilder6 用 C++ 编写一个简单的 Brainfuck IDE。我已经为 RichEdit 装上了我的,让它看起来有点像记事本。然后我为输入添加了 TE
我是 C 的新手。目前我正在尝试编写一个 Brainfuck 解释器。到目前为止,我已经尝试过了。 #include #include char *line; int curr_po
我认为我的程序并非在所有情况下都支持嵌套循环。这太荒谬了,但是这个 brainfuck 程序与我的拦截器一起正常工作并打印“Hello World!”到标准输出: ++++++++[>++++[>++
我坐下来尝试实现 BrainFuck。 syntax看起来很简单。我无法让愚蠢的事情发挥作用。我已经有一段时间了;我承认我需要 sleep 。也许这就是问题所在。解释器没有输出任何东西。我很确定问题很
是的,我正在制作 Brainfuck 解释器,但我还需要从其代码创建 AST。可以很容易地在节点中使用原始操作(+ - . , > rightBracketsIds; private rea
我写了一个 brainfuck ide 的 alpha 版本。我写了自己的解释器,虽然我从我的老师那里得到了关于代码循环的大量帮助,因为我很难以“IT 方式”理解它。现在对于我的报告,我需要解释器算法
我正在为该语言编写解释器 Brainfuck 我用的是命令行 java bfinterpreter/BFInterpreter >output.bin bf 程序应该输出 00 到 FF 的十六进制,
我是一名优秀的程序员,十分优秀!