This question is the adaptation of this question in C++. (I'm the author of that code as well).
The code:
#include <bits/stdc++.h>
std::string bf_interpreter(std::string code){
long long int ptr = 0;
std::vector<int> values = {0};
long long int length = 1;
std::vector<int> brackets;
std::string result = "";
for(unsigned long long int index = 0; index < code.size(); index++){
while(length <= ptr){
length++;
values.push_back(0);
}
char character = code[index];
switch(character){
case '>': ptr++; break;
case '<': ptr--; break;
case '+': values[ptr] = (values[ptr] + 1) % 256; break;
case '-': values[ptr] = (values[ptr] - 1) % 256; break;
case '[':
brackets.push_back(index);
break;
case ']':
if(values[ptr] == 0)
brackets.pop_back();
else
index = brackets[brackets.size() - 1];
break;
case '.':
result += (char)values[ptr];
break;
case ',':
char input; std::cin >> input;
values[ptr] = (int)input;
break;
}
}
return result;
}
int main()
{
std::string s;
std::cin >> s;
std::cout << bf_interpreter(s);
}
I'm quite a beginner in C++, so please excuse me if I'm not following any standard conventions.
#include <bits/stdc++.h>is one of the worst things to do in c++! \$\endgroup\$