2015-05-08 18 views
12

Próbowałem testowania mojego brainfuck tłumacza wc z tym kodem kształcie butelki w brainfuck:Handling pętle Brainf * ck

   +>+++++++[>>>+++ 
       +++++<<<<+++++ 
       +++>-]+++++++++>>> 
       +>>++++++++++[>+ 
       +++++++++<-]>[ 
       >>+>+>+>+>+>+> 
       +>>+>+>>+>+>+> 
       +>>+>+>>+>+>+> 
       >+>+>+>+>>>>>+ 
       >+>+>+>>+>+>+> 
       >+>+>+>+>>+>+> 
       +>>+>+>+>+>>+>+> 
       >+>+>+>+>+>+>>>> 
       +>+>>+>+>+>+<<<< 
       <<<<<<<<<<<<<<<< 
       <<<<<<<<<<<<<<<<<< 
       <<<<<<<<<<<<<<<<<< 
       <<<<<<<<<<<<<<<<<< 
       -]<++++[>++++++++< 
       -]>[>+>>>>>>>>+>>> 
       +>>>>>+>>>+>>>>+>> 
       >>>+>+>>+>>>>>+>>>>+ 
       >>>>>+>>>>+>>>>>+>>> 
       +>>>>>>>+>+>+>>>+>>> 
       >>+<<<<<<<<<<<<<<<<< 
       <<<<<<<<<<<<<<<<<<<< 
      <<<<<<<<<<<<<<<<<<<<<< 
      <<<<<<<<<<<<<<<<-]+++++[ 
      >>>++>+++>+++>++>>+++>>++> 
      >>>>>+++>>++>++>>+++>+>>>+++ 
     +>->++>++>++>+++>++>>--->->+>>>+ 
     +>++>>>>++>++++>++>>->++>>>++>->++ 
    +>+++>>+>+++>>>+++>++>+++>++>>>++>>++> 
    ++>>++>++>+++<<<<<<<<<<<<<<<<<<<<<<<<<<< 
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
    <<<<-]>>-->+>+>+>-->+>>>+>++>>-->+>+>->>+>>> 
    +>->+>>->++>-->-->++>->>>->++>++>+>>+>>+>>>+ 
>->>>++>>>>++>++>>>>>+>>++>->+>++>>>>+++>>>+>> 
->+>->+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
<<<<<<<<<<<<<<<<<<<<<<<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>. 
>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.<<<<<<<<<<<<<<<<<< 
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
<<<<-.>>>>.>.>.>.>.>.>.>>.>.>.>.>.>.>.>.>.>.>.>. 
>.>.>.>.>.>.>.>.>>.>..<<<<<<<<<<<<<<<<<<<<<<<<<< 
<<<<<<->]<<]<.>>>>.>.>.>.>.>.>.>>.>.>.>.>.>.>.>. 
>.>.>.>.>.>.>.>.>.>.>.>.>.>>>.<<<<<<<<<<<<<<<<<< 
<<<<<<<<<<<<<<<<<.>>>>.>.>.>.>.>.>.>>.>.>.>.>.>. 
>.>.>>>>>>>>>>>>>>.>.>>.>.>.>.>.>.>.>.>.>.>.>.>. 
>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>. 
>.>.>.>.>.<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<.>.>.>.>.>.>. 
>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>>.> 

I zamiast ouf outputing cały '99 butelek piwa na ścianie”piosenki z 99 do 1, wypuszcza tylko wersety od 99 do 96 butelek. Czy coś jest nie tak z pętlami w kodzie Brainfucka lub w tłumaczu?

Oto mój kod c:

#include <stdio.h> 
#include <stdlib.h> 

int loop(char *commands, int indexOfCommand, int mode){ 
    int sum = 0; 
    int movingIndex = indexOfCommand; 
    while(sum > -1){ 
     movingIndex -= mode; 
     if(commands[movingIndex] == ']') 
      sum += mode; 
     else if(commands[movingIndex]=='[') 
      sum -= mode; 
    } 
    return movingIndex; 
} 

int main(){ 
    unsigned char array[30000] = {0}; // all elements are 0 
    unsigned char commands[60000] = {0}; // all elements are 0 
    int counter = 0; 
    int c; 

    int loop(char *commands, int indexOfCommand, int mode); 

    while((c = getchar()) != EOF){ 
     commands[counter] = c; 
     counter++; 
    } 

    int indexOfArray = counter; 
    int indexOfCommands = 0; 
    int numOfCommands = 0; 

    int length = sizeof(commands)/sizeof(commands[0]); // the length of an array 

    while (indexOfCommands < length && numOfCommands < 60000){ 
     switch(commands[indexOfCommands]){ 
      case '+': 
       if(array[indexOfArray]==255){ 
        array[indexOfArray]=0; 
       } else array[indexOfArray]++; 
      break; 
      case '-': 
       if(array[indexOfArray]==0){ 
        array[indexOfArray]=255; 
       }else array[indexOfArray]--; 
      break; 
      case '>': 
       indexOfArray++; 
      break; 
      case '<': 
       indexOfArray--; 
      break; 
      case '.': 
       putchar(array[indexOfArray]); 
      break; 
      case ']': 
       if(array[indexOfArray]!=0) { 
        indexOfCommands=loop(commands,indexOfCommands,1); 
       } 
      break; 
      case '[': 
       if(array[indexOfArray]==0){ 
        indexOfCommands=loop(commands,indexOfCommands,-1); 
       } 
      break; 
      default: 
       // if there is any other character, just ignore it 
       break; 
     } 
     indexOfCommands++; 
     numOfCommands++; 
    } 

    return 0; 
} 
+8

Uwielbiam sposób, w jaki piszesz bro: D –

+5

właśnie zrobiłem mój weekend – cerkiewny

+0

musiał zrzutu ekranu ten, dzięki @ lol dla lolz –

Odpowiedz

6

Warunkiem przedwczesnego zakończenia realizacji programu jest tutaj:

while (indexOfCommands < length && numOfCommands < 60000) { 
            ^^^^^^^^^^^^^^^^^^^^^ 
    // interpret BF opcodes 

    numOfCommands++; 
} 

pan lekceważyć ile instrukcja BF musi zrobić prostych rzeczy. (Nie jestem pewien, dlaczego ten warunek jest tam, usuń go, a zobaczysz wszystkie 99 wersetów.)