fork(5) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n; // Количество ячеек
  6. cin >> n;
  7. char tape[n];
  8. for (int i = 0; i < n; i++)
  9. tape[i] = 0;
  10. int pointer = 0;
  11. string program = "", tmp;
  12. while (cin >> tmp)
  13. program += tmp;
  14. cin >> program;
  15. //cout << program << endl;
  16. for (unsigned int i = 0; i < program.length(); i++) {
  17. //cout << i << " " << program[i] << " " << pointer << " " << (int)tape[pointer] << endl;
  18. if (program[i] == '>')
  19. pointer++;
  20.  
  21. if (program[i] == '<')
  22. pointer--;
  23.  
  24. if (program[i] == '+')
  25. tape[pointer]++;
  26.  
  27. if (program[i] == '-')
  28. tape[pointer]--;
  29.  
  30. if (program[i] == '.')
  31. cout << tape[pointer];
  32.  
  33. if (program[i] == ',')
  34. cin >> tape[pointer];
  35.  
  36. if (program[i] == '[') {
  37. if (tape[pointer] == 0) {
  38. int counter = 1;
  39. while (counter > 0) {
  40. i++;
  41. if (program[i] == '[')
  42. counter++;
  43. if (program[i] == ']')
  44. counter--;
  45. }
  46. }
  47. }
  48. else if (program[i] == ']') {
  49. if (tape[pointer] != 0) {
  50. int counter = 1;
  51. while (counter > 0) {
  52. i--;
  53. if (program[i] == '[')
  54. counter--;
  55. if (program[i] == ']')
  56. counter++;
  57. }
  58. }
  59. }
  60. }
  61.  
  62. return 0;
  63. }
Success #stdin #stdout 0s 3476KB
stdin
30000
+++++++++++ number of digits to output
> #1
+ initial number
>>>> #5
++++++++++++++++++++++++++++++++++++++++++++ (comma)
> #6
++++++++++++++++++++++++++++++++ (space)
<<<<<< #0
[
  > #1
  copy #1 to #7
  [>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]

  <
  divide #7 by 10 (begins in #7)
  [
    >
    ++++++++++  set the divisor #8
    [
      subtract from the dividend and divisor
      -<-
      if dividend reaches zero break out
        copy dividend to #9
        [>>+>+<<<-]>>>[<<<+>>>-]
        set #10
        +
        if #9 clear #10
        <[>[-]<[-]]
        if #10 move remaining divisor to #11
        >[<<[>>>+<<<-]>>[-]]
      jump back to #8 (divisor possition)
      <<
    ]
    if #11 is empty (no remainder) increment the quotient #12
    >>> #11
    copy to #13
    [>>+>+<<<-]>>>[<<<+>>>-]
    set #14
    +
    if #13 clear #14
    <[>[-]<[-]]
    if #14 increment quotient
    >[<<+>>[-]]
    <<<<<<< #7
  ]

  quotient is in #12 and remainder is in #11
  >>>>> #12
  if #12 output value plus offset to ascii 0
  [++++++++++++++++++++++++++++++++++++++++++++++++.[-]]
  subtract #11 from 10
  ++++++++++  #12 is now 10
  < #11
  [->-<]
  > #12
  output #12 even if it's zero
  ++++++++++++++++++++++++++++++++++++++++++++++++.[-]
  <<<<<<<<<<< #1

  check for final number
  copy #0 to #3
  <[>>>+>+<<<<-]>>>>[<<<<+>>>>-]
  <- #3
  if #3 output (comma) and (space)
  [>>.>.<<<[-]]
  << #1

  [>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-
]
stdout
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89