Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class BrainfuckInterpreter {
- public:
- explicit BrainfuckInterpreter(const string& instructions)
- : instructions(instructions), memory(30000) { }
- void execute() {
- auto the_pointer = memory.data();
- for(char instruction : instructions) {
- switch(instruction) {
- case '>':
- ++the_pointer;
- break;
- case '<':
- --the_pointer;
- break;
- case '+':
- ++*the_pointer;
- break;
- case '-':
- --*the_pointer;
- break;
- case '.':
- cout << *the_pointer;
- break;
- case ',':
- cin >> *the_pointer;
- break;
- }
- }
- }
- private:
- string instructions;
- vector<unsigned char> memory;
- };
- int main() {
- string instructions;
- cin >> instructions;
- BrainfuckInterpreter interpreter(instructions);
- interpreter.execute();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement