-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
373 lines (280 loc) · 10.9 KB
/
Copy pathForm1.cs
File metadata and controls
373 lines (280 loc) · 10.9 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CodigoDeHamming
{
public partial class Form1 : Form
{
public byte[,] matrizGeradora = new byte[7, 4];
public byte[,] matrizParidade = new byte[3,7];
public byte[] vetorBinario = new byte[4];
public byte[] vetorHaming = new byte[7];
public byte[] vetorParidade = new byte[3];
public byte[] vetorErro = new byte[7];
public byte[] vetorCorrecao = new byte[7];
public Form1()
{
InitializeComponent();
// inicializa a matriz geradora
matrizGeradora[0, 0] = 1; matrizGeradora[0, 1] = 1; matrizGeradora[0, 2] = 0; matrizGeradora[0, 3] = 1;
matrizGeradora[1, 0] = 1; matrizGeradora[1, 1] = 0; matrizGeradora[1, 2] = 1; matrizGeradora[1, 3] = 1;
matrizGeradora[2, 0] = 1; matrizGeradora[2, 1] = 0; matrizGeradora[2, 2] = 0; matrizGeradora[2, 3] = 0;
matrizGeradora[3, 0] = 0; matrizGeradora[3, 1] = 1; matrizGeradora[3, 2] = 1; matrizGeradora[3, 3] = 1;
matrizGeradora[4, 0] = 0; matrizGeradora[4, 1] = 1; matrizGeradora[4, 2] = 0; matrizGeradora[4, 3] = 0;
matrizGeradora[5, 0] = 0; matrizGeradora[5, 1] = 0; matrizGeradora[5, 2] = 1; matrizGeradora[5, 3] = 0;
matrizGeradora[6, 0] = 0; matrizGeradora[6, 1] = 0; matrizGeradora[6, 2] = 0; matrizGeradora[6, 3] = 1;
// inicializa a matriz de paridade
matrizParidade[0, 0] = 1; matrizParidade[0, 1] = 0; matrizParidade[0, 2] = 1; matrizParidade[0, 3] = 0; matrizParidade[0, 4] = 1; matrizParidade[0, 5] = 0; matrizParidade[0, 6] = 1;
matrizParidade[1, 0] = 0; matrizParidade[1, 1] = 1; matrizParidade[1, 2] = 1; matrizParidade[1, 3] = 0; matrizParidade[1, 4] = 0; matrizParidade[1, 5] = 1; matrizParidade[1, 6] = 1;
matrizParidade[2, 0] = 0; matrizParidade[2, 1] = 0; matrizParidade[2, 2] = 0; matrizParidade[2, 3] = 1; matrizParidade[2, 4] = 1; matrizParidade[2, 5] = 1; matrizParidade[2, 6] = 1;
}
private void btnGerarMatriz_Click_1(object sender, EventArgs e)
{
}
public void GerarMatrizGeradora()
{
for (int i = 0; i < 7; i++)
{
string linha = "";
for (int j = 0; j < 4; j++)
{
linha += " " + matrizGeradora[i, j] + " ";
}
listBox1.Items.Add(linha);
}
}
private void btnConverterBinario_Click(object sender, EventArgs e)
{
if(tbNumero.Text == "")
{
MessageBox.Show("Informe um valor válido!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int valor = Convert.ToInt32(tbNumero.Text);
if (valor > 15 || valor < 0)
{
MessageBox.Show("O sistema só aceita números de no máximo 4 bits, ou seja de 0 a 15!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
converterBinario(Convert.ToInt32(tbNumero.Text));
gerarMatrizes();
}
public void converterBinario( int numero)
{
for (int i = 0; i < 4; i++)
{
byte mod = Convert.ToByte(numero % 2);
vetorBinario[i] = mod;
numero = numero >> 1;
}
String valorConcatenado = vetorBinario[3] + "" + vetorBinario[2] + "" + vetorBinario[1] + "" + vetorBinario[0];
MessageBox.Show("Número em binario:" + valorConcatenado ,"Informação", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
tbBinario.Text = valorConcatenado.ToString();
}
private void btnVerVetor_Click(object sender, EventArgs e)
{
}
public void verVetorBinario()
{
for (int i = 3; i >= 0; i--)
{
listBox2.Items.Add(vetorBinario[i]);
}
}
private void btnGerarHamming_Click(object sender, EventArgs e)
{
}
public void gerarHaming()
{
int posicaoVetor = 3;
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 7; i++)
{
vetorHaming[i] = Convert.ToByte((Convert.ToInt32(vetorHaming[i]) + matrizGeradora[i, j] * vetorBinario[posicaoVetor]) & 1);
}
posicaoVetor = posicaoVetor - 1;
}
for (int i = 0; i < vetorHaming.Length; i++)
{
listBox3.Items.Add(vetorHaming[i]);
tbHaming.Text += vetorHaming[i];
}
}
private void btnConverterBinario_MouseClick(object sender, MouseEventArgs e)
{
gerarMatrizes();
}
private void btnGerarParidade_Click(object sender, EventArgs e)
{
}
public void gerarParidade()
{
for (int i = 0; i < 3; i++)
{
string linha = "";
for (int j = 0; j < 7; j++)
{
linha += " " + matrizParidade[i, j] + " ";
}
listBox4.Items.Add(linha);
}
}
private void button1_Click(object sender, EventArgs e)
{
}
public void gerarVetorParidade()
{
int posicaoVetor = 6;
for (int j = 0; j < 7; j++)
{
for (int i = 0; i < 3; i++)
{
vetorParidade[i] += (Convert.ToByte(matrizParidade[i, j] * vetorHaming[j]));
}
posicaoVetor = posicaoVetor - 1;
}
for (int i = 0; i < vetorParidade.Length; i++)
{
listBox5.Items.Add(vetorParidade[i] ); ///// %2
}
}
private void btnEditar_Click(object sender, EventArgs e)
{
}
private void btnAtualiza_Click(object sender, EventArgs e)
{
//limpar
listBox3.Items.Clear();
listBox5.Items.Clear();
listBox6.Items.Clear();
for (int i = 0; i < vetorHaming.Length; i++)
{
vetorHaming[i] = 0;
}
for (int i = 0; i < vetorParidade.Length; i++)
{
vetorParidade[i] = 0;
}
if (tbHaming.Text.Length != 0)
{
var lista = new List<Int32>();
var x = tbHaming.Text;
for (int i = 0; i < x.Length; i++)
{
Int32 num = 0;
if (Int32.TryParse(x.Substring(i, 1), out num))
lista.Add(num);
listBox3.Items.Add(lista[i]);
vetorHaming[i] = Convert.ToByte(lista[i]);
}
gerarVetorParidade();
}
string soma = ((vetorParidade[2]) % 2).ToString() + ((vetorParidade[1]) % 2).ToString() + ((vetorParidade[0]) % 2).ToString();
MessageBox.Show("O erro encontra-se no bit: " + Convert.ToInt32(soma, 2) , "Informação", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
if (tbHaming.Text.Length != 0)
{
var lista = new List<Int32>();
var x = tbHaming.Text;
for (int i = 0; i < x.Length; i++)
{
Int32 num = 0;
if (Int32.TryParse(x.Substring(i, 1), out num))
lista.Add(num);
vetorCorrecao[i] = Convert.ToByte(lista[i]);
}
int bitAlterado = Convert.ToInt32(soma, 2);
for ( int i = 0; i < x.Length; i++)
{
if(vetorCorrecao[bitAlterado -1] == 0 )
{
vetorCorrecao[bitAlterado -1] = 1;
}
else
{
vetorCorrecao[bitAlterado -1] = 0;
}
}
for (int i = 0; i < x.Length; i++)
{
listBox6.Items.Add(vetorCorrecao[i]);
}
}
}
private void tbNumero_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && !(e.KeyChar == (char)8))//&& !(e.KeyChar == (char)44)
{
e.Handled = true;
}
}
private void tbBinario_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && !(e.KeyChar == (char)8))//&& !(e.KeyChar == (char)44)
{
e.Handled = true;
}
}
private void tbHaming_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && !(e.KeyChar == (char)8))//&& !(e.KeyChar == (char)44)
{
e.Handled = true;
}
}
private void btnConverterBinario_KeyDown(object sender, KeyEventArgs e)
{
}
private void btnConverterBinario_Enter(object sender, EventArgs e)
{
}
private void btnConverterBinario_KeyPress(object sender, KeyPressEventArgs e)
{
}
public void gerarMatrizes()
{
if (tbNumero.Text == "")
{
return;
}
int valor = Convert.ToInt32(tbNumero.Text);
if (valor > 15 || valor < 0)
{
return;
}
//limpa dados do vetor e das list box
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
listBox4.Items.Clear();
listBox5.Items.Clear();
listBox6.Items.Clear();
tbHaming.Text = "";
for (int i = 0; i < vetorHaming.Length; i++)
{
vetorHaming[i] = 0;
}
for (int i = 0; i < vetorParidade.Length; i++)
{
vetorParidade[i] = 0;
}
GerarMatrizGeradora();
verVetorBinario();
gerarHaming();
gerarParidade();
gerarVetorParidade();
}
private void tbHaming_KeyDown(object sender, KeyEventArgs e)
{
}
private void tbHaming_TextChanged(object sender, EventArgs e)
{
}
}
}