-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrainFuck-Interpreter.cpp
More file actions
99 lines (90 loc) · 2.16 KB
/
BrainFuck-Interpreter.cpp
File metadata and controls
99 lines (90 loc) · 2.16 KB
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
// Enter this below code to print "Hello World". This is NOT a complete project!
//>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>++++++++[<---------->-]<+.>+++++[<++++++++++>-]<+++++.>++[<++++++++++>-]<++++.+++.------.--------.
#include <iostream>
#include <string>
using namespace std;
int Array[100]={};
int index=0;
int nextcell()
{
index++;
return 0;
}
int prevcell()
{
if(index>0)
{
index--;
}
return 0;
}
int increment()
{
Array[index]++;
return 0;
}
int decrement()
{
Array[index]--;
return 0;
}
int display()
{
int ascii = Array[index];
char character = char(ascii);
cout<<character;
return 0;
}
int charinput()
{
cin>>Array[index];
return 0;
}
int main()
{
int i;
char input[1000];
cout<<"Any input other than the valid inputs will be ignored \n {< , > , . , [ , ] , , , + , _ } \n Maximum inputs are till 100 limit\n";
cout<<"enter the code: ";
cin>>input;
for(i=0;input[i]!='\0';i++)
{
switch (input[i])
{
case '>' : nextcell();
break;
case '<' : prevcell();
break;
case '+' : increment();
break;
case '-' : decrement();
break;
case '.' : display();
break;
case ',' : charinput();
break;
case '[' : if(Array[index]==0)
{
while(input[i]!=']')
{
++i;
}
}
break;
case ']' : if(Array[index]==0)
{
continue;
}
else if(Array[index]!=0)
{
while(input[i]!='[')
{
--i;
}
}
break;
default:
break;
}
}
}