-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAB.cpp
140 lines (120 loc) · 5.55 KB
/
AB.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <csignal>
#define countof(a) ( sizeof(a) / sizeof(*(a)) )
using namespace std;
int Num_char = 0;
int Position = 0;
int NewPosition = 0;
int Character;
string Program;
string Result;
int Register1 = 0;
int Register2 = 0;
int Register3 = 0;
int Register4[] = { 0, 0 };
int Mode = 0;
int Memory[1024];
int Temp;
int ipow(int base, int exponent)
{
int retval = 1;
if ( exponent < 0 )
raise(SIGFPE);
while ( exponent )
{
retval *= ( exponent & 1 ) ? base : 1;
exponent /= 2;
base *= base;
}
return retval;
}
void error(const char* msg)
{
puts(msg);
exit(1);
}
void checkmem(int addr, const char* errormsg)
{
if(!(addr >= 0 && addr < (int)countof(Memory))) error(errormsg);
}
int main(int argc, char *argv[])
{
if ( argc != 2 )
{
fprintf(stderr, "Usage: %s FILE\n", argv[0]);
return 1;
}
FILE * FileInd = fopen(argv[1], "r");
if ( FileInd == NULL )
{
fprintf(stderr, "Could not open file '%s'.\n", argv[1]);
return 1;
}
do {
Character = getc(FileInd);
Program.append(1, char(Character));
Num_char ++;
} while ( Character != EOF );
fclose(FileInd);
do {
NewPosition = Position;
if(!(Position >= 0 && Position < Num_char)) error("Invalid position");
if ( Program[Position] == 'a' ) { Register1 += 1 ;}
if ( Program[Position] == 'b' ) { Register1 -= 1 ;}
if ( Program[Position] == 'c' ) { Register1 += 10 ;}
if ( Program[Position] == 'd' ) { Register1 -= 10 ;}
if ( Program[Position] == 'e' ) { Register1 += 100 ;}
if ( Program[Position] == 'f' ) { Register1 -= 100 ;}
if ( Program[Position] == 'g' ) { Register2 += 1 ;}
if ( Program[Position] == 'h' ) { Register2 -= 1 ;}
if ( Program[Position] == 'i' ) { Register2 += 10 ;}
if ( Program[Position] == 'j' ) { Register2 -= 10 ;}
if ( Program[Position] == 'k' ) { Register2 += 100 ;}
if ( Program[Position] == 'l' ) { Register2 -= 100 ;}
if ( Program[Position] == 'm' ) { Register3 = !Register1 ;}
if ( Program[Position] == 'n' ) { Register3 = !Register2 ;}
if ( Program[Position] == 'o' ) { Register3 = Register1 and Register2 ;}
if ( Program[Position] == 'p' ) { Register3 = Register1 or Register2 ;}
if ( Program[Position] == 'q' ) { Register3 = Register1 xor Register2 ;}
if ( Program[Position] == 'r' ) { Register3 = Register1 + Register2 ;}
if ( Program[Position] == 's' ) { Register3 = Register1 - Register2 ;}
if ( Program[Position] == 't' ) { Register3 = Register1 * Register2 ;}
if ( Program[Position] == 'u' ) { if(Register2 == 0) error("u: division by zero"); Register3 = Register1 / Register2 ;}
if ( Program[Position] == 'v' ) { if(Register2 == 0) error("v: division by zero"); Register3 = Register1 % Register2 ;}
if ( Program[Position] == 'w' ) { Register3 = ipow(Register1, Register2) ;}
if ( Program[Position] == 'x' ) { Register1 = 0 ;}
if ( Program[Position] == 'y' ) { Register2 = 0 ;}
if ( Program[Position] == 'z' ) { Register3 = 0 ;}
if ( Program[Position] == 'A' ) { Register2 = Register1 ;}
if ( Program[Position] == 'B' ) { Register1 = Register2 ;}
if ( Program[Position] == 'C' ) { Register3 = Register1 ;}
if ( Program[Position] == 'D' ) { Register3 = Register2 ;}
if ( Program[Position] == 'E' ) { Register1 = Register3 ;}
if ( Program[Position] == 'F' ) { Register2 = Register3 ;}
if ( Program[Position] == 'G' ) { checkmem(Register4[0], "G: invalid memory address"); Register1 = Memory[Register4[0]] ;}
if ( Program[Position] == 'H' ) { checkmem(Register4[0], "H: invalid memory address"); Register2 = Memory[Register4[0]] ;}
if ( Program[Position] == 'I' ) { checkmem(Register4[0], "I: invalid memory address"); Memory[Register4[0]] = Register3 ;}
if ( Program[Position] == 'J' ) { if (( Temp = getchar()) != EOF ) Register1 = Temp ;}
if ( Program[Position] == 'K' ) { if (( Temp = getchar()) != EOF ) Register2 = Temp ;}
if ( Program[Position] == 'L' ) { cout << char(Register3) ;}
if ( Program[Position] == 'M' ) { cout << int(Register3) ;}
if ( Program[Position] == 'N' ) { if ( Register1 == Register2 ) NewPosition = Register4[1];}
if ( Program[Position] == 'O' ) { if ( Register1 != Register2 ) NewPosition = Register4[1];}
if ( Program[Position] == 'P' ) { if ( Register1 >= Register2 ) NewPosition = Register4[1];}
if ( Program[Position] == 'Q' ) { if ( Register1 <= Register2 ) NewPosition = Register4[1];}
if ( Program[Position] == 'R' ) { if ( Register3 == 0 ) NewPosition = Register4[1] - 1;}
if ( Program[Position] == 'S' ) { Register4[Mode] += 1 ;}
if ( Program[Position] == 'T' ) { Register4[Mode] -= 1 ;}
if ( Program[Position] == 'U' ) { Register4[Mode] += 10 ;}
if ( Program[Position] == 'V' ) { Register4[Mode] -= 10 ;}
if ( Program[Position] == 'W' ) { Register4[Mode] += 100 ;}
if ( Program[Position] == 'X' ) { Register4[Mode] -= 100 ;}
if ( Program[Position] == 'Y' ) { Register4[Mode] = 0 ;}
if ( Program[Position] == 'Z' ) { Mode = ( Mode + 1 ) % 2 ;}
Position = (Position == NewPosition) ? Position + 1 : NewPosition;
} while ( Position < Num_char );
return 0;
}