fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. using namespace std;
  5.  
  6. bool isBFCommand(char c) {
  7. return c == '>' || c == '<' ||
  8. c == '+' || c == '-' ||
  9. c == '.' || c == ',' ||
  10. c == '[' || c == ']';
  11. }
  12.  
  13. string stringToBrainfuck(const string& s) {
  14. string res;
  15. int dpValue = 0;
  16.  
  17. //ideone still has trouble with range based for?
  18. for(auto it = s.begin(); it != s.end(); ++it) {
  19. char c = *it;
  20.  
  21. int distance = (c > dpValue) ? (c - dpValue) : (dpValue - c);
  22. int direction = (c > dpValue) ? +1 : -1;
  23.  
  24. if(distance > 127) {
  25. distance = 256 - distance;
  26. direction *= -1;
  27. }
  28.  
  29. res += (!isBFCommand(c) && isprint(c)) ? c : ' ';
  30. res += " " + string(distance, ((direction == +1) ? '+' : '-') )
  31. + ".\n";
  32. dpValue = c;
  33. }
  34. return res;
  35. }
  36.  
  37. string fetchInput() {
  38. string ret, line;
  39. while(getline(cin, line))
  40. ret += line + '\n';
  41. return ret;
  42. }
  43.  
  44. int main() {
  45. cout << stringToBrainfuck( fetchInput() ) << "\n";
  46. }
Success #stdin #stdout 0s 3068KB
stdin
Hello, World!
stdout
H ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
e +++++++++++++++++++++++++++++.
l +++++++.
l .
o +++.
  -------------------------------------------------------------------.
  ------------.
W +++++++++++++++++++++++++++++++++++++++++++++++++++++++.
o ++++++++++++++++++++++++.
r +++.
l ------.
d --------.
! -------------------------------------------------------------------.
  -----------------------.