-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfuck.c
72 lines (62 loc) · 1.15 KB
/
brainfuck.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
#include <stdio.h>
#define MAX_SIZE 1000
void get_input(char *input_str)
{
printf(">>> ");
scanf("%s", input_str);
}
int find_cor(char *input_str)
{
int index = 0;
int stack = 0;
while (!(input_str[index] == '[' && stack == 0)) {
if (input_str[index] == ']')
stack++;
if (input_str[index] == '[')
stack--;
index--;
}
return index;
}
void print_cells(int *bf_cells)
{
for (int i = 0; i < 15; i++)
printf("%d ", bf_cells[i]);
printf("\n");
}
int brain_fuck()
{
int bf_cells[MAX_SIZE] = { 0 };
char input_str[MAX_SIZE] = { 0 };
int index = 0;
int cor;
get_input(input_str);
for (int i = 0; i < strlen(input_str); i++) {
if (input_str[i] == '>')
index++;
if (input_str[i] == '<')
index--;
if (input_str[i] == '+')
bf_cells[index]++;
if (input_str[i] == '-')
bf_cells[index]--;
if (input_str[i] == ',')
scanf("%c", bf_cells + index);
if (input_str[i] == '.')
printf("%c", bf_cells[index]);
if (input_str[i] == '[')
continue;
if (input_str[i] == ']')
{
if (!bf_cells[index])
continue;
cor = find_cor(input_str + i - 1);
i = i + (cor - 2);
}
}
}
int main()
{
brain_fuck();
return 0;
}