-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainbf.c
105 lines (94 loc) · 3.58 KB
/
mainbf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
void analyse(char* tokens,int len,int loops[500][2],int lenLoops){
unsigned char array[30000];
int ptr = 0;
char input;
int findPos;
for(int i = 0;i < 30000;i++){
array[i] = 0;
}
for(int i = 0; i < len;i++){
switch (tokens[i]){
case '>':
++ptr;
break;
case '<':
--ptr;
break;
case '+':
array[ptr] == 255 ? array[ptr] = 0 : array[ptr]++;
break;
case '-':
array[ptr] == 0 ? array[ptr] = 255 : array[ptr]--;
break;
case '.':
printf("%c",array[ptr]);
break;
case ',':
scanf(" %c",&input);
array[ptr] = input;
break;
case '['://0
for(findPos = 0; findPos < lenLoops;findPos++){
if(i == loops[findPos][0])
break;
}
if(array[ptr] == 0)
i = loops[findPos][1];
break;
case ']'://1
for(findPos = 0; findPos < lenLoops;findPos++){
if(i == loops[findPos][1])
break;
}
i = loops[findPos][0] - 1;
break;
default:
break;
}
if (ptr > 29999 || ptr < 0 ){
printf("error: invalid ptr");
break;
}
}
printf("\n");
}
int main(int argc, char** argv){
if(argc != 2){
printf("invalids arguments");
return 1;
}
char tokens[500];
FILE* ptr;
ptr = fopen(argv[1],"r");
if(NULL == ptr){
printf("file can not be open");
return 1;
}
char ch;
int pos = 0;
int loops[500][2];
int open = 0;
int close = 0;
int len = 0;
int increase = 0;
while(ch != EOF){
ch = fgetc(ptr);
if(ch == '>'|| ch == '<'||ch == '+'||ch == '-'||
ch == '.'||ch == ','||ch == '['|| ch == ']'){
tokens[pos] = ch;
if(ch == '['){
open++;
loops[len++][0] = pos;
}else if(ch == ']'){
close++;
loops[open - close + increase][1] = pos;
open == close ? increase++ : 0;
}
pos++;
}
}
analyse(tokens,pos,loops,len);
return 0;
}