REFACTOR: clean code and some bug fixed
This commit is contained in:
@@ -83,52 +83,49 @@ pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_o
|
||||
(data_bits[3] ^ data_bits[5] ^ data_bits[6]) == data_bits[4],
|
||||
);
|
||||
|
||||
match checked_bits {
|
||||
(true, true, true) => {
|
||||
general_length += length_of_code;
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
let error_position = syndromes
|
||||
.iter()
|
||||
.find(move |&(&_error_position, &error)| error == checked_bits)
|
||||
.unwrap()
|
||||
.0;
|
||||
if let (true, true, true) = checked_bits {
|
||||
general_length += length_of_code;
|
||||
continue;
|
||||
} else {
|
||||
let error_position = syndromes
|
||||
.iter()
|
||||
.find(move |&(&_error_position, &error)| error == checked_bits)
|
||||
.unwrap()
|
||||
.0;
|
||||
|
||||
let correctly_code: Vec<u8> = data_bits
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, bit)| {
|
||||
if index == error_position - 1 {
|
||||
if *bit == 1u8 {
|
||||
0u8
|
||||
} else {
|
||||
1u8
|
||||
}
|
||||
} else {
|
||||
*bit
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let correctly_code: Vec<u8> = data_bits
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, bit)| {
|
||||
if index == error_position - 1 {
|
||||
(*bit == 0u8).into()
|
||||
} else {
|
||||
*bit
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let error = format!(
|
||||
"Ошибка в коде {} {:?}, позиция ошибки {}, корректный код: {:?}; \n",
|
||||
general_length / 7,
|
||||
&data_bits,
|
||||
error_position,
|
||||
correctly_code
|
||||
);
|
||||
let error = format!(
|
||||
"Ошибка в коде {} {:?}, позиция ошибки {}, корректный код: {:?}; \n",
|
||||
general_length / 7,
|
||||
&data_bits,
|
||||
error_position,
|
||||
correctly_code
|
||||
);
|
||||
|
||||
errors.push_str(error.as_str());
|
||||
errors.push_str(error.as_str());
|
||||
|
||||
general_length += length_of_code;
|
||||
}
|
||||
general_length += length_of_code;
|
||||
}
|
||||
}
|
||||
|
||||
if errors.len() == 0 {
|
||||
result_string.push_str("Все коды корректны.");
|
||||
if data.len() > 0 {
|
||||
if errors.len() == 0 {
|
||||
result_string.push_str("Все коды корректны.");
|
||||
} else {
|
||||
result_string.push_str(errors.as_str());
|
||||
}
|
||||
} else {
|
||||
result_string.push_str(errors.as_str())
|
||||
result_string.push_str("Введите код для проверки.")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user