From 0526e79812cf242e12d5b8f4779f067bd8741668 Mon Sep 17 00:00:00 2001 From: JinweiClarkChao Date: Fri, 9 Jan 2015 21:23:46 +0800 Subject: ver0.1 --- .gitignore | 1 + brainfuck.c | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index d579511..d409a8f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. +*.exe # User-specific files *.suo *.user diff --git a/brainfuck.c b/brainfuck.c index cda6857..8e80d07 100644 --- a/brainfuck.c +++ b/brainfuck.c @@ -9,7 +9,6 @@ typedef struct brainfuck{ char command[MAX_DATA]; char *ptr; int count; - int pos; }bf; bf one; @@ -18,11 +17,10 @@ void init() { char c; memset(&one, 0, sizeof(one)); - one.pos = 0; one.count = 0; one.ptr = one.data; - - while (c = getchar()) + + while ((c = getchar()) != EOF) { if (c == '\n') break; @@ -31,6 +29,7 @@ void init() one.command[++one.count] = c; } } + one.command[0] = one.count; one.command[one.count] = '\0'; } @@ -39,7 +38,8 @@ void run() char c; int i; - while (c = one.command[++one.pos]) + one.count = 0; + while (c = one.command[++one.count]) { switch (c) { @@ -66,11 +66,11 @@ void run() break; else { - for (i = one.pos + 1; i <= one.count; i++) + for (i = one.count + 1; i <= one.command[0]; i++) { if (one.command[i] == ']') { - one.pos = i; + one.count = i; break; } } @@ -81,11 +81,11 @@ void run() break; else { - for (i = one.pos - 1; i >= 1; i--) + for (i = one.count - 1; i >= 1; i--) { if (one.command[i] == '[') { - one.pos = i; + one.count = i; break; } } -- cgit v1.2.3