-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
90 lines (85 loc) · 1.81 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <string>
struct node {
int value = 0;
node* prev = nullptr;
node* next = nullptr;
};
int main(int argc, char** argv) {
node* cn = new node; //current node
std::ifstream fcode("code.txt");
std::string code = "";
std::string symbols = ",.+-<>[]";
/**Convertion in a string that contains only the accepted symbols**/
for (int i = 0; !fcode.eof(); ++i) {
char fchar = fcode.get();
int j = 0;
for (; j < symbols.length() && fchar != symbols[j]; ++j)
;
if (fchar == symbols[j])
code += fchar;
}
/**Execution of the code**/
for (int i = 0; i < code.length(); ++i) { //i of the Code
char to_exec = code[i];
switch (to_exec) {
case ',':
char in;
std::cin >> in;
cn->value = (int)in;
break;
case '.':
std::cout << (char)cn->value;
break;
case '>':
if (cn->next == nullptr)
cn->next = new node;
cn->next->prev = cn;
cn = cn->next;
break;
case '<':
if (cn->prev == nullptr)
cn->prev = new node;
cn->prev->next = cn;
cn = cn->prev;
break;
case '+':
cn->value++;
break;
case '-':
cn->value--;
break;
case ']':
if (cn->value != 0) {
int in_sospeso = 0; //Number of suspended parentheses
do {
i--;
if (code[i] == ']')
in_sospeso++;
else if (code[i] == '[')
if (in_sospeso != 0)
in_sospeso--;
} while (!(code[i] == '[' && in_sospeso == 0));
}
break;
}
}
/**Deletes all the node**/
node* cn_prev_copy = cn->prev;
while (cn->next != nullptr) {
cn = cn->next;
delete cn->prev;
}
delete cn;
if (cn_prev_copy != nullptr) {
cn = cn_prev_copy;
while (cn->prev != nullptr) {
cn = cn->prev;
delete cn->next;
}
delete cn;
}
for (;;);
return 0;
}