ADDED: models and controllers for page "signal reducing"\n TODO: done table for page "signal reducing"

This commit is contained in:
2024-04-19 01:30:51 +04:00
parent 6e315907bc
commit b6d0eb0508
11 changed files with 228 additions and 97 deletions

View File

@@ -1,6 +1,9 @@
use std::collections::HashMap;
use crate::{controller::view_utils::input_utils::*, model::model::*};
use crate::{
controller::view_utils::{hamming_code_input_utils::*, input_utils::*},
model::model::*,
};
/// **Синдромы**
///
@@ -23,7 +26,8 @@ pub fn hamming(raw_input: String, mode: HammingMode) -> Result<String, String> {
let prepared_input: String = processing_input(&raw_input);
let (fc, sc) = check_correct_input(&raw_input, &prepared_input, length_of_code);
let (fc, sc): (bool, bool) =
check_correct_binary_code(&raw_input, &prepared_input, length_of_code);
if !fc || !sc {
Err("Ошибка. Проверьте корректность ввода.".to_string())
@@ -45,7 +49,7 @@ pub fn hamming_encrypt_data(data: &Vec<u8>, result_string: &mut String, length_o
let mut i: usize = length_of_code;
while i <= data.len() {
let data_bits = &data[i - length_of_code..i];
let data_bits: &[u8] = &data[i - length_of_code..i];
let (check_bit_1, check_bit_2, check_bit_3) = (
data_bits[0] ^ data_bits[1] ^ data_bits[3],
data_bits[0] ^ data_bits[2] ^ data_bits[3],
@@ -87,30 +91,32 @@ pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_o
general_length += length_of_code;
continue;
} else {
let error_position = syndromes
let error_position: &usize = 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 {
(*bit == 0u8).into()
} else {
*bit
}
})
.collect();
let uncorrect_code: String = from_vec_bits_to_string(&data_bits);
let correct_code: String = from_vec_bits_to_string(
data_bits
.iter()
.enumerate()
.map(|(index, &bit)| {
if index == error_position - 1 {
(bit == 0u8).into()
} else {
bit
}
})
.collect::<Vec<u8>>()
.as_slice(),
);
let error = format!(
"Ошибка в коде {} {:?}, позиция ошибки {}, корректный код: {:?}; \n",
"Ошибка в коде {} [{uncorrect_code}], позиция ошибки {}, корректный код: [{correct_code}]; \n",
general_length / 7,
&data_bits,
error_position,
correctly_code
error_position
);
errors.push_str(error.as_str());

View File

@@ -1 +1,3 @@
pub mod hamming_code_seven_four;
pub mod hamming_code_seven_four;
pub mod signal_reducer;

View File

@@ -0,0 +1,15 @@
pub fn reactive_resistance_of_capacitor(Cm: f64, L: f64, f: f64) -> f64 {
1f64 / (2f64 * 3.14f64 * f * Cm * L)
}
pub fn full_resistance_of_capacitor(Xc: f64, Rs: f64, Rm: f64, L: f64) -> f64 {
(Xc.powf(2f64) + (Rs + Rm * L).powf(2f64)).sqrt()
}
pub fn voltage_from_signal_source(Vs: f64, Xc: f64, Z: f64) -> f64 {
(Vs * Xc) / Z
}
pub fn coef_of_signal_reduce(Vs: f64, V: f64) -> f64 {
Vs / V
}

View File

@@ -0,0 +1,41 @@
use gtk4 as gtk;
use crate::{model::model::*, model_utils::hamming_code_seven_four::*};
use gtk::{prelude::*, *};
pub fn start_hamming_algorithm(input: &TextView, output: &TextView, mode: bool) -> () {
let (iter_start, iter_end) = input.buffer().bounds();
let parsed_input: String = input
.buffer()
.text(&iter_start, &iter_end, false)
.to_string()
.trim()
.parse()
.unwrap();
let operation = if mode == false {
HammingMode::Encrypt
} else {
HammingMode::Decrypt
};
match hamming(parsed_input, operation) {
Ok(res) => output.buffer().set_text(res.trim_end()),
Err(rej) => output.buffer().set_text(rej.as_str()),
}
}
pub fn check_correct_binary_code(
input: &String,
prepared_input: &String,
l: usize,
) -> (bool, bool) {
let first_condition: bool = input
.as_str()
.chars()
.all(|c| c == '1' || c == '0' || c == ' ');
let second_condition: bool = prepared_input.len() % l == 0;
(first_condition, second_condition)
}

View File

@@ -4,29 +4,7 @@ use bitvec::{order::Lsb0, view::AsBits};
use gtk::{prelude::*, *};
use std::ops::Deref;
use crate::{model::model::*, model_utils::hamming_code_seven_four::*};
pub fn parse_input(input: &TextView, output: &TextView, mode: bool) -> () {
let (iter_start, iter_end) = input.buffer().bounds();
let parsed_input: String = input
.buffer()
.text(&iter_start, &iter_end, false)
.to_string()
.trim()
.parse()
.unwrap();
let operation = if mode == false {
HammingMode::Encrypt
} else {
HammingMode::Decrypt
};
match hamming(parsed_input, operation) {
Ok(res) => output.buffer().set_text(res.trim_end()),
Err(rej) => output.buffer().set_text(rej.as_str()),
}
}
const ASCII_ZERO_CHAR_POSITION: u8 = 48;
pub fn processing_input(input: &String) -> String {
input
@@ -35,15 +13,11 @@ pub fn processing_input(input: &String) -> String {
.fold(String::new(), |c: String, n: &str| c + n)
}
pub fn check_correct_input(input: &String, prepared_input: &String, l: usize) -> (bool, bool) {
let first_condition = input
.as_str()
.chars()
.all(|c| c == '1' || c == '0' || c == ' ');
let second_condition = prepared_input.len() % l == 0;
(first_condition, second_condition)
pub fn from_vec_bits_to_string(raw_data: &[u8]) -> String {
raw_data
.iter()
.map(|bit| -> char { (bit + ASCII_ZERO_CHAR_POSITION).into() })
.collect()
}
pub fn from_string_to_vec_bits(raw_data: String) -> Vec<u8> {

View File

@@ -1 +1,4 @@
pub mod input_utils;
pub mod hamming_code_input_utils;
pub mod input_utils;
pub mod signal_reduce_input_utils;

View File

@@ -0,0 +1,63 @@
use gtk4 as gtk;
use crate::model::model::SchemeCharacteristics;
use gtk::{prelude::*, *};
pub fn check_characteristics(
raw_characteristics: &Vec<TextView>,
all_inputs_data: &mut Vec<f64>,
output: &mut String,
) -> bool {
for input in raw_characteristics {
let (iter_start, iter_end) = input.buffer().bounds();
let parsed_input: Result<f64, _> = input
.buffer()
.text(&iter_start, &iter_end, false)
.to_string()
.trim()
.parse();
match parsed_input {
Ok(int) => {
all_inputs_data.push(int);
println!("{:?}", int);
}
Err(err) => {
if output.len() == 0 {
output.push_str("Введите пожалуйста числа в полях");
println!("{:?}", err);
}
}
}
}
if output.len() != 0 {
false
} else {
true
}
}
pub fn start_algorithm(error_log_label: &Label, raw_characteristics: &Vec<TextView>) {
let mut all_inputs_data: Vec<f64> = Vec::new();
let mut output: String = String::new();
if let false = check_characteristics(raw_characteristics, &mut all_inputs_data, &mut output) {
error_log_label.set_text(output.as_str());
} else {
error_log_label.set_text("");
let (L, Rm, Cm, Vs, Rs, f): SchemeCharacteristics = (
all_inputs_data[0],
all_inputs_data[1],
all_inputs_data[2],
all_inputs_data[3],
all_inputs_data[4],
all_inputs_data[5],
);
let mut frequencies: Vec<usize> = vec![f as usize, 1usize];
frequencies.append(&mut (0..100).step_by(5).collect());
}
}