diff options
author | JinweiClarkChao <[email protected]> | 2015-01-09 21:23:46 +0800 |
---|---|---|
committer | JinweiClarkChao <[email protected]> | 2015-01-09 21:23:46 +0800 |
commit | 0526e79812cf242e12d5b8f4779f067bd8741668 (patch) | |
tree | 75f6f9769dc63d858b91e873b6b3294091342df7 | |
parent | 398335c2bcda0be07938b03a3805310ba2050f7e (diff) | |
download | brainfuck-0526e79812cf242e12d5b8f4779f067bd8741668.tar.gz |
ver0.1
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | brainfuck.c | 18 |
2 files changed, 10 insertions, 9 deletions
@@ -1,6 +1,7 @@ | |||
1 | ## Ignore Visual Studio temporary files, build results, and | 1 | ## Ignore Visual Studio temporary files, build results, and |
2 | ## files generated by popular Visual Studio add-ons. | 2 | ## files generated by popular Visual Studio add-ons. |
3 | 3 | ||
4 | *.exe | ||
4 | # User-specific files | 5 | # User-specific files |
5 | *.suo | 6 | *.suo |
6 | *.user | 7 | *.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{ | |||
9 | char command[MAX_DATA]; | 9 | char command[MAX_DATA]; |
10 | char *ptr; | 10 | char *ptr; |
11 | int count; | 11 | int count; |
12 | int pos; | ||
13 | }bf; | 12 | }bf; |
14 | 13 | ||
15 | bf one; | 14 | bf one; |
@@ -18,11 +17,10 @@ void init() | |||
18 | { | 17 | { |
19 | char c; | 18 | char c; |
20 | memset(&one, 0, sizeof(one)); | 19 | memset(&one, 0, sizeof(one)); |
21 | one.pos = 0; | ||
22 | one.count = 0; | 20 | one.count = 0; |
23 | one.ptr = one.data; | 21 | one.ptr = one.data; |
24 | 22 | ||
25 | while (c = getchar()) | 23 | while ((c = getchar()) != EOF) |
26 | { | 24 | { |
27 | if (c == '\n') | 25 | if (c == '\n') |
28 | break; | 26 | break; |
@@ -31,6 +29,7 @@ void init() | |||
31 | one.command[++one.count] = c; | 29 | one.command[++one.count] = c; |
32 | } | 30 | } |
33 | } | 31 | } |
32 | one.command[0] = one.count; | ||
34 | one.command[one.count] = '\0'; | 33 | one.command[one.count] = '\0'; |
35 | } | 34 | } |
36 | 35 | ||
@@ -39,7 +38,8 @@ void run() | |||
39 | char c; | 38 | char c; |
40 | int i; | 39 | int i; |
41 | 40 | ||
42 | while (c = one.command[++one.pos]) | 41 | one.count = 0; |
42 | while (c = one.command[++one.count]) | ||
43 | { | 43 | { |
44 | switch (c) | 44 | switch (c) |
45 | { | 45 | { |
@@ -66,11 +66,11 @@ void run() | |||
66 | break; | 66 | break; |
67 | else | 67 | else |
68 | { | 68 | { |
69 | for (i = one.pos + 1; i <= one.count; i++) | 69 | for (i = one.count + 1; i <= one.command[0]; i++) |
70 | { | 70 | { |
71 | if (one.command[i] == ']') | 71 | if (one.command[i] == ']') |
72 | { | 72 | { |
73 | one.pos = i; | 73 | one.count = i; |
74 | break; | 74 | break; |
75 | } | 75 | } |
76 | } | 76 | } |
@@ -81,11 +81,11 @@ void run() | |||
81 | break; | 81 | break; |
82 | else | 82 | else |
83 | { | 83 | { |
84 | for (i = one.pos - 1; i >= 1; i--) | 84 | for (i = one.count - 1; i >= 1; i--) |
85 | { | 85 | { |
86 | if (one.command[i] == '[') | 86 | if (one.command[i] == '[') |
87 | { | 87 | { |
88 | one.pos = i; | 88 | one.count = i; |
89 | break; | 89 | break; |
90 | } | 90 | } |
91 | } | 91 | } |