-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperaciones.java
More file actions
227 lines (179 loc) · 6.77 KB
/
Operaciones.java
File metadata and controls
227 lines (179 loc) · 6.77 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package src;
import static java.lang.Integer.max;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Operaciones
{
private static final Pattern SUMAR = Pattern.compile("(LD [A-EHL],)\\(([0-9A-F]{1,2})\\+([0-9A-F]{1,2})\\)");
private static final Pattern SUMARR = Pattern.compile("(LD (?:BC|DE|HL),)\\(([0-9A-F]{1,4})H?\\+([0-9A-F]{1,4})H?\\)");
private static final Pattern RESTAR = Pattern.compile("(LD [A-EHL],)\\(([0-9A-F]{1,2})\\-([0-9A-F]{1,2})\\)");
private static final Pattern RESTARR = Pattern.compile("(LD (?:BC|DE|HL),)\\(([0-9A-F]{1,4})H?\\-([0-9A-F]{1,4})H?\\)");
private static final Pattern MULT= Pattern.compile("(LD [A-EHL],)\\(([0-9A-F]{1,2})\\*([0-9A-F]{1,2})\\)");
private static final Pattern MULTT = Pattern.compile("(LD (?:BC|DE|HL),)\\(([0-9A-F]{1,4})\\*([0-9A-F]{1,4})\\)");
private static final Pattern CPS = Pattern.compile("(CP )([0-9A-F]{1,2})\\+([0-9A-F]{1,2})");
private static final Pattern CPR = Pattern.compile("(CP )([0-9A-F]{1,2})-([0-9A-F]{1,2})");
private static final Pattern CPM = Pattern.compile("(CP )([0-9A-F]{1,2})\\*([0-9A-F]{1,2})");
public Operaciones(){
}
public static boolean cps(String line)
{
Matcher match=CPS.matcher(line);
return match.find();
}
public static boolean cpr(String line)
{
Matcher match=CPR.matcher(line);
return match.find();
}
public static boolean cpm(String line)
{
Matcher match=CPM.matcher(line);
return match.find();
}
public static boolean sum8(String line)
{
Matcher match=SUMAR.matcher(line);
return match.find();
}
public static boolean sum16(String line)
{
Matcher match=SUMARR.matcher(line);
return match.find();
}
public static boolean res8(String line)
{
Matcher match=RESTAR.matcher(line);
return match.find();
}
public static boolean res16(String line)
{
Matcher match=RESTARR.matcher(line);
return match.find();
}
public static boolean mult8(String line)
{
Matcher match=MULT.matcher(line);
return match.find();
}
public static boolean mult16(String line)
{
Matcher match=MULTT.matcher(line);
return match.find();
}
public static String obtener(String line){
if(sum8(line)){
Matcher obj=SUMAR.matcher(line);
if(obj.find()){
String hex1= obj.group(2);
String hex2= obj.group(3);
String res = sumar(hex1,hex2);
line=line.replace(obj.group(0),obj.group(1)+res);
return line;
}
}
if(cps(line)){
Matcher obj=CPS.matcher(line);
if(obj.find()){
String hex1= obj.group(2);
String hex2= obj.group(3);
String res = sumar(hex1,hex2);
line=line.replace(obj.group(0),obj.group(1)+res);
}
}
if(sum16(line)){
Matcher obj=SUMARR.matcher(line);
if(obj.find()){
String hex1= obj.group(2);
String hex2= obj.group(3);
String res = sumar(hex1,hex2);
line=line.replace(obj.group(0),obj.group(1)+res);
}
}
if(res8(line)){
Matcher obj=RESTAR.matcher(line);
if(obj.find()){
String hex1= obj.group(2);
String hex2= obj.group(3);
String res = restar(hex1,hex2);
line=line.replace(obj.group(0),obj.group(1)+res);
}
}
if(cpr(line)){
Matcher obj=CPR.matcher(line);
if(obj.find()){
String hex1= obj.group(2);
String hex2= obj.group(3);
String res = restar(hex1,hex2);
line=line.replace(obj.group(0),obj.group(1)+res);
}
}
if(res16(line)){
Matcher obj=RESTARR.matcher(line);
if(obj.find()){
String hex1= obj.group(2);
String hex2= obj.group(3);
String res = restar(hex1,hex2);
line=line.replace(obj.group(0),obj.group(1)+res);
}
}
if(mult8(line)){
Matcher obj=MULT.matcher(line);
if(obj.find()){
String hex1= obj.group(2);
String hex2= obj.group(3);
String res = mult(hex1,hex2);
line=line.replace(obj.group(0),obj.group(1)+res);
}
}
if(cpm(line)){
Matcher obj=CPM.matcher(line);
if(obj.find()){
String hex1= obj.group(2);
String hex2= obj.group(3);
String res = mult(hex1,hex2);
line=line.replace(obj.group(0),obj.group(1)+res);
}
}
if(mult16(line)){
Matcher obj=MULTT.matcher(line);
if(obj.find()){
String hex1= obj.group(2);
String hex2= obj.group(3);
String res = mult(hex1,hex2);
line=line.replace(obj.group(0),obj.group(1)+res);
}
}
return line;
}
public static String sumar(String hex1, String hex2){
// Convertir los números hexadecimales a enteros
int num1 = Integer.parseInt(hex1, 16);
int num2 = Integer.parseInt(hex2, 16);
// Sumar los números
int sum = num1 + num2;
// Convertir el resultado a hexadecimal
String hexSum = Integer.toHexString(sum).toUpperCase();
return hexSum;
}
public static String restar(String hex1, String hex2){
// Convertir los números hexadecimales a enteros
int num1 = Integer.parseInt(hex1, 16);
int num2 = Integer.parseInt(hex2, 16);
// Sumar los números
int res = num1 - num2;
// Convertir el resultado a hexadecimal
String hexRes = Integer.toHexString(res).toUpperCase();
int size=hexRes.length();
return hexRes.substring(max(size-4,0), size);
}
public static String mult(String hex1, String hex2){
// Convertir los números hexadecimales a enteros
int num1 = Integer.parseInt(hex1, 16);
int num2 = Integer.parseInt(hex2, 16);
//multiplicar numeros
int mult = num1*num2;
// Convertir el resultado a hexadecimal
String hexMult = Integer.toHexString(mult).toUpperCase();
return hexMult;
}
}