Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define SIZE 30000
- #define INDEX (SIZE - 1)
- int main(int argc, char *argv[])
- {
- if(argc < 2) return 1; // Need a file!
- FILE *file = fopen(argv[1], "r");
- if(!file) return 1;
- char array[SIZE];
- char *ptr = array;
- long int depth = 0;
- while(!feof(file))
- {
- char curr = fgetc(file);
- long int currDepth = depth;
- switch(curr)
- {
- case '>' : ptr = ptr == array + INDEX ? array : ptr + 1; break;
- case '<' : ptr = ptr == array ? array + INDEX : ptr - 1; break;
- case '+' : ++(*ptr); break;
- case '-' : --(*ptr); break;
- case '.' : putchar(*ptr); break;
- case ',' : while((*ptr = getchar()) == '\n'); break;
- case '[' : ++depth; if(!*ptr) while(depth > currDepth) switch(curr = fgetc(file))
- {
- case '[' : ++depth; break;
- case ']' : --depth; break;
- }
- break;
- case ']' : --depth; if(*ptr) while(depth < currDepth)
- {
- fseek(file, -2, SEEK_CUR);
- switch(curr = fgetc(file))
- {
- case '[' : ++depth; break;
- case ']' : --depth; break;
- }
- }
- break;
- }
- }
- fclose(file);
- putchar('\n');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment