-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phase1.java
201 lines (185 loc) · 5.75 KB
/
Phase1.java
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.io.*;
public class Phase1 {
static char M[][];
static char R[] = new char[4];
static char IR[] = new char[4];
static int IC;
static int c, SI;
static char buffer[] = new char[41];
static String s;
static BufferedReader br;
static void init() {
M = new char[100][4];
}
static void printMemory() {
for (int i = 0; i < 100; i++) {
System.out.print("M[" + i + "][] ");
for (int j = 0; j < 4; j++) {
System.out.print(M[i][j]);
}
System.out.println();
}
}
private static void startExecution() throws Exception {
IC = 0;
executeUserProgram();
}
private static void executeUserProgram() throws Exception {
int t1 = 0;
String ts = "";
while (IR[0] != 'H') {
for (int i = 0; i < 4; i++)
IR[i] = M[IC][i];
String ch = Character.toString(IR[0]);
ch += Character.toString(IR[1]);
if (IR[0] != 'H') {
ts = String.valueOf(IR[2]);
ts += String.valueOf(IR[3]);
t1 = Integer.parseInt(ts);
}
IC += 1;
switch (ch) {
case "GD":
SI = 1;
break;
case "PD":
SI = 2;
break;
case "H":
SI = 3;
break;
case "LR":
for (int i = 0; i < 4; i++) {
R[i] = M[t1][i];
}
break;
case "SR":
for (int i = 0; i < 4; i++) {
M[t1][i] = R[i];
}
break;
case "CR":
for (int i = 0; i < 4; i++) {
if (R[i] == M[t1][i]) {
c = 1;
} else {
c = 0;
break;
}
}
break;
case "BT":
if (c == 1) {
IC = t1;
}
break;
}
if (SI == 1) {
readF();
SI = 0;
}
if (SI == 2) {
writeF();
SI = 0;
}
if (SI == 3) {
terminate();
}
}
}
private static void readF() throws Exception {
s = br.readLine();
String ts = String.valueOf(IR[2]);
ts += String.valueOf(IR[3]);
int t1 = Integer.parseInt(ts);
int t = t1;
int c = 0;
for (int i = 0; i < s.length(); i++) {
M[t1][c] = s.charAt(i);
c++;
if (c == 4) {
c = 0;
t1++;
}
if (t == (t1 + 10))
break;
}
}
private static void writeF() {
String ts = String.valueOf(IR[2]);
ts += String.valueOf(IR[3]);
int t1 = Integer.parseInt(ts);
int t = t1;
int c = 0;
String s = "";
while (M[t1][c] != 0) {
s += M[t1][c++];
if (c == 4) {
t1++;
c = 0;
}
if (t == (t1 + 10))
break;
}
try {
FileWriter outputF = new FileWriter("F:\\VIT\\Module6\\Lab\\OS\\cp\\Output.txt", true);
outputF.write(s);
outputF.write("\n");
outputF.close();
} catch (Exception e) {
System.out.println(e);
}
}
private static void terminate() {
try {
FileWriter outputF = new FileWriter("F:\\VIT\\Module6\\Lab\\OS\\cp\\Output.txt", true);
outputF.write("\n\n");
outputF.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
FileReader file = new FileReader("F:\\VIT\\Module6\\Lab\\OS\\cp\\Input.txt");
br = new BufferedReader(file);
s = br.readLine();
buffer = s.toCharArray();
while (s != null) {
if (buffer[0] == '$' && buffer[1] == 'A' && buffer[2] == 'M' && buffer[3] == 'J') {
init();
int c = 0;
int k = 0;
s = br.readLine();
int ct = 0;
while (!(buffer[0] == '$' && buffer[1] == 'D' && buffer[2] == 'T' && buffer[3] == 'A')) {
while (ct != s.length()) {
for (int i = 0; i < 4; i++) {
if (ct == s.length())
break;
buffer[i] = s.charAt(ct);
ct++;
}
for (int i = 0; i < 4; i++) {
if (buffer[0] == 'H'){
break;
}
M[k][i] = buffer[i];
}
k++;
}
s = br.readLine();
buffer = s.toCharArray();
}
M[k][c] = 'H';
}
if (buffer[0] == '$' && buffer[1] == 'D' && buffer[2] == 'T' && buffer[3] == 'A') {
startExecution();
}
if (buffer[0] == '$' && buffer[1] == 'E' && buffer[2] == 'N' && buffer[3] == 'D') {
printMemory();
}
s = br.readLine();
buffer = s.toCharArray();
}
}
}