diff options
| author | 2025-05-02 15:19:20 +0300 | |
|---|---|---|
| committer | 2025-05-02 15:19:20 +0300 | |
| commit | 6f6fda607134a1172893bffe127ad04c6234aa65 (patch) | |
| tree | 7f07500ea16944b7f538c9f6b556adb37dbcb7ae | |
| parent | fix: improve minimization algorithm (diff) | |
| download | logic-rust-6f6fda607134a1172893bffe127ad04c6234aa65.tar.gz logic-rust-6f6fda607134a1172893bffe127ad04c6234aa65.tar.bz2 logic-rust-6f6fda607134a1172893bffe127ad04c6234aa65.tar.lz logic-rust-6f6fda607134a1172893bffe127ad04c6234aa65.tar.xz logic-rust-6f6fda607134a1172893bffe127ad04c6234aa65.tar.zst logic-rust-6f6fda607134a1172893bffe127ad04c6234aa65.zip | |
feat: D to JK trigger truth table conversion
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/bin/d2jk.rs | 53 | 
2 files changed, 55 insertions, 0 deletions
| @@ -3,5 +3,7 @@ name = "logic-rust"  version = "0.1.0"  edition = "2024" +default-run = "logic-rust" +  [dependencies]  itertools = "0.14.0" diff --git a/src/bin/d2jk.rs b/src/bin/d2jk.rs new file mode 100644 index 0000000..e9202d2 --- /dev/null +++ b/src/bin/d2jk.rs @@ -0,0 +1,53 @@ +use std::collections::HashMap; + +use itertools::Itertools; + +fn main() { +    let truth_table_file_path = std::env::args().nth(1).unwrap(); +    let truth_table_file = std::fs::read_to_string(truth_table_file_path).unwrap(); + +    let mut lines = truth_table_file.lines(); +    let truth_table_inputs_line = lines.next().unwrap(); +    let truth_table_outputs_line = lines.next().unwrap(); + +    let truth_table_inputs: HashMap<&str, usize> = truth_table_inputs_line +        .split_whitespace() +        .enumerate() +        .map(|(i, input)| (input, i)) +        .collect(); +    let truth_table_outputs = truth_table_outputs_line.split_whitespace().collect_vec(); + +    let mut new_outputs = vec![]; +    for output in &truth_table_outputs { +        if truth_table_inputs.contains_key(output) { +            new_outputs.push(format!("{output}_J")); +            new_outputs.push(format!("{output}_K")); +        } else { +            new_outputs.push(output.to_string()); +        } +    } + +    println!("{truth_table_inputs_line}"); +    println!("{}", new_outputs.join(" ")); + +    for line in lines { +        let (input, output) = line.split_once(char::is_whitespace).unwrap(); +        print!("{input} "); + +        for (value, output_name) in output.chars().zip(&truth_table_outputs) { +            if let Some(&i) = truth_table_inputs.get(output_name) { +                match (&input[i..i + 1], value) { +                    ("0", '0') => print!("0-"), +                    ("0", '1') => print!("1-"), +                    ("1", '0') => print!("-1"), +                    ("1", '1') => print!("-0"), +                    _ => unreachable!(), +                } +            } else { +                print!("{value}"); +            } +        } + +        println!(); +    } +} | 
