-
Notifications
You must be signed in to change notification settings - Fork 5
/
abcd.cpp
123 lines (108 loc) · 4.81 KB
/
abcd.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int Num_char = 0;
int Position = 0;
int Character;
string Program[10000];
int Register1 = 0;
int Register2 = 0;
int Register3 = 0;
int Register4[] = { 0, 0 };
int Mode = 0;
int Memory[1024];
int Temp;
int iter=0;
void inputFxn(int regNum, bool asNumber) {
Temp = getchar();
if(asNumber) {
Temp -= '0';
}
if ( Temp != EOF ) {
if(regNum == 1)
Register1 = Temp;
else
Register2 = Temp;
}
}
int main(int argc, char *argv[])
{
FILE * FileInd;
if(argc != 2) {
printf("Wrong number of arguments!\n");
printf("Usage: Provide filename as argument.");
return 2;
}
FileInd = fopen(argv[1],"r");
do {
Character = getc(FileInd);
Program[Num_char] = Character;
Num_char ++;
} while ( Character != EOF && Num_char < 9999);
fclose(FileInd);
do {
if ( Program[Position] == "a" ) { Register1 += 1 ;}
else if ( Program[Position] == "b" ) { Register1 -= 1 ;}
else if ( Program[Position] == "c" ) { Register1 += 10 ;}
else if ( Program[Position] == "d" ) { Register1 -= 10 ;}
else if ( Program[Position] == "e" ) { Register1 += 100 ;}
else if ( Program[Position] == "f" ) { Register1 -= 100 ;}
else if ( Program[Position] == "g" ) { Register2 += 1 ;}
else if ( Program[Position] == "h" ) { Register2 -= 1 ;}
else if ( Program[Position] == "i" ) { Register2 += 10 ;}
else if ( Program[Position] == "j" ) { Register2 -= 10 ;}
else if ( Program[Position] == "k" ) { Register2 += 100 ;}
else if ( Program[Position] == "l" ) { Register2 -= 100 ;}
else if ( Program[Position] == "m" ) { Register3 = !Register1 ;}
else if ( Program[Position] == "n" ) { Register3 = !Register2 ;}
else if ( Program[Position] == "o" ) { Register3 = Register1 and Register2 ;}
else if ( Program[Position] == "p" ) { Register3 = Register1 or Register2 ;}
else if ( Program[Position] == "q" ) { Register3 = Register1 xor Register2 ;}
else if ( Program[Position] == "r" ) { Register3 = Register1 + Register2 ;}
else if ( Program[Position] == "s" ) { Register3 = Register1 - Register2 ;}
else if ( Program[Position] == "t" ) { Register3 = Register1 * Register2 ;}
else if ( Program[Position] == "u" ) { Register3 = Register1 / Register2 ;}
else if ( Program[Position] == "v" ) { Register3 = Register1 % Register2 ;}
else if ( Program[Position] == "w" ) { Register3 = Register1 ^ Register2 ;}
else if ( Program[Position] == "x" ) { Register1 = 0 ;}
else if ( Program[Position] == "y" ) { Register2 = 0 ;}
else if ( Program[Position] == "z" ) { Register3 = 0 ;}
else if ( Program[Position] == "A" ) { Register2 = Register1 ;}
else if ( Program[Position] == "B" ) { Register1 = Register2 ;}
else if ( Program[Position] == "C" ) { Register3 = Register1 ;}
else if ( Program[Position] == "D" ) { Register3 = Register2 ;}
else if ( Program[Position] == "E" ) { Register1 = Register3 ;}
else if ( Program[Position] == "F" ) { Register2 = Register3 ;}
else if ( Program[Position] == "G" ) { Register1 = Memory[Register4[0]] ;}
else if ( Program[Position] == "H" ) { Register2 = Memory[Register4[0]] ;}
else if ( Program[Position] == "I" ) { Memory[Register4[0]] = Register3 ;}
else if ( Program[Position] == "J" ) { inputFxn(1, 0) ;}
else if ( Program[Position] == "K" ) { inputFxn(2, 0) ;}
else if ( Program[Position] == "L" ) { if(Register3!=0) cout << char(Register3) ;}
else if ( Program[Position] == "M" ) { cout << int(Register3) ;}
else if ( Program[Position] == "N" ) { if ( Register1 == Register2 ) Position = Register4[1]-1 ;}
else if ( Program[Position] == "O" ) { if ( Register1 != Register2 ) Position = Register4[1]-1 ;}
else if ( Program[Position] == "P" ) { if ( Register1 >= Register2 ) Position = Register4[1]-1 ;}
else if ( Program[Position] == "Q" ) { if ( Register1 <= Register2 ) Position = Register4[1]-1 ;}
else if ( Program[Position] == "R" ) { if ( Register3 == 0 ) Position = Register4[1]-1 ;}
else if ( Program[Position] == "S" ) { Register4[Mode] += 1 ;}
else if ( Program[Position] == "T" ) { Register4[Mode] -= 1 ;}
else if ( Program[Position] == "U" ) { Register4[Mode] += 10 ;}
else if ( Program[Position] == "V" ) { Register4[Mode] -= 10 ;}
else if ( Program[Position] == "W" ) { Register4[Mode] += 100 ;}
else if ( Program[Position] == "X" ) { Register4[Mode] -= 100 ;}
else if ( Program[Position] == "Y" ) { Register4[Mode] = 0 ;}
else if ( Program[Position] == "Z" ) { Mode = ( Mode + 1 ) % 2 ;}
else if ( Program[Position] == "?" ) { if (Temp == EOF) break; }
if(Register4[0] > 1023) {
return 1;
}
if(Register4[1] > 9999) {
return 3;
}
iter++;
Position ++;
} while ( Position <= Num_char && iter < 20000);
}