Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // After running the compiler save your brainfuck code as .bf file and run it by opening it on the running compiler code
- #include <iostream>
- #include <vector>
- int main() {
- std::vector<int> tape(30000, 0);
- int ptr = 0;
- std::string code;
- std::cin >> code;
- int codePtr = 0;
- while (codePtr < code.size()) {
- char instruction = code[codePtr];
- if (instruction == '>') {
- ptr++;
- } else if (instruction == '<') {
- ptr--;
- } else if (instruction == '+') {
- tape[ptr]++;
- } else if (instruction == '-') {
- tape[ptr]--;
- } else if (instruction == '.') {
- std::cout << static_cast<char>(tape[ptr]);
- } else if (instruction == ',') {
- char input;
- std::cin >> input;
- tape[ptr] = static_cast<int>(input);
- } else if (instruction == '[') {
- if (tape[ptr] == 0) {
- int loopCount = 1;
- while (loopCount > 0) {
- codePtr++;
- if (code[codePtr] == '[') {
- loopCount++;
- } else if (code[codePtr] == ']') {
- loopCount--;
- }
- }
- }
- } else if (instruction == ']') {
- if (tape[ptr] != 0) {
- int loopCount = 1;
- while (loopCount > 0) {
- codePtr--;
- if (code[codePtr] == ']') {
- loopCount++;
- } else if (code[codePtr] == '[') {
- loopCount--;
- }
- }
- }
- }
- codePtr++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement