From 27b2603b3c11e9072b8b9eb80deae8082d823302 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Fri, 30 Aug 2024 13:40:55 +0200 Subject: [PATCH] Wrote program for Day 36 --- README.md | 2 +- Week-06/Day-36_LCD-Display/day36/Cargo.toml | 6 ++ Week-06/Day-36_LCD-Display/day36/src/lib.rs | 87 ++++++++++++++++++++ Week-06/Day-36_LCD-Display/day36/src/main.rs | 41 +++++++++ 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 Week-06/Day-36_LCD-Display/day36/Cargo.toml create mode 100644 Week-06/Day-36_LCD-Display/day36/src/lib.rs create mode 100644 Week-06/Day-36_LCD-Display/day36/src/main.rs diff --git a/README.md b/README.md index 4804932..7c5a406 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #33 | [WERTYU](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-33_WERTYU) | :white_check_mark: | | Day #34 | [Primary Arithmetic](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-34_Primary-Arithmetic) | :white_check_mark: | | Day #35 | [Dog And Gopher](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-35_Dog-And-Gopher) | :white_check_mark: | -| Day #36 | [LCD Display](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-36_LCD-Display) | :white_large_square: | +| Day #36 | [LCD Display](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-36_LCD-Display) | :white_check_mark: | | Day #37 | [Breaking The Records](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-37_Breaking-The-Records) | :white_large_square: | | Day #38 | [Electronics Shop](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-38_Electronics-Shop) | :white_large_square: | | Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_large_square: | diff --git a/Week-06/Day-36_LCD-Display/day36/Cargo.toml b/Week-06/Day-36_LCD-Display/day36/Cargo.toml new file mode 100644 index 0000000..9fdf3bf --- /dev/null +++ b/Week-06/Day-36_LCD-Display/day36/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day36" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-06/Day-36_LCD-Display/day36/src/lib.rs b/Week-06/Day-36_LCD-Display/day36/src/lib.rs new file mode 100644 index 0000000..ec0d870 --- /dev/null +++ b/Week-06/Day-36_LCD-Display/day36/src/lib.rs @@ -0,0 +1,87 @@ +pub fn to_lcd(mut n: usize, s: usize) -> String { + let height = 2 * s + 3; + + let mut digits: Vec> = Vec::new(); + let mut result = String::new(); + + let lcd_digits: [Digit; 10] = [ + Digit::new(s, [true, true, true, false, true, true, true]), + Digit::new(s, [false, false, true, false, false, true, false]), + Digit::new(s, [true, false, true, true, true, false, true]), + Digit::new(s, [true, false, true, true, false, true, true]), + Digit::new(s, [false, true, true, true, false, true, false]), + Digit::new(s, [true, true, false, true, false, true, true]), + Digit::new(s, [true, true, false, true, true, true, true]), + Digit::new(s, [true, false, true, false, false, true, false]), + Digit::new(s, [true; 7]), + Digit::new(s, [true, true, true, true, false, true, true]), + ]; + + while n > 0 { + let num = n % 10; + + digits.push(lcd_digits.get(num).unwrap().to_string_array()); + + n /= 10; + } + + digits.reverse(); + + for i in 0..height { + for d in digits.iter() { + result.push_str(d.get(i).unwrap()); + } + result.push('\n'); + } + + result +} + +struct Digit { + size: usize, + segments: [bool; 7], +} + +impl Digit { + fn new(size: usize, segments: [bool; 7]) -> Self { + Digit { size, segments } + } + + fn to_string_array(&self) -> Vec { + let vertical = '|'; + let horizontal = "-"; + + let mut result = Vec::new(); + + result.push(format!( + " {} ", + (if self.segments[0] { horizontal } else { " " }).repeat(self.size) + )); + for _ in 0..self.size { + result.push(format!( + "{}{}{}", + if self.segments[1] { vertical } else { ' ' }, + " ".repeat(self.size), + if self.segments[2] { vertical } else { ' ' }, + )); + } + result.push(format!( + " {} ", + (if self.segments[3] { horizontal } else { " " }).repeat(self.size) + )); + for _ in 0..self.size { + result.push(format!( + "{}{}{}", + if self.segments[4] { vertical } else { ' ' }, + " ".repeat(self.size), + if self.segments[5] { vertical } else { ' ' }, + )); + } + result.push(format!( + " {} ", + (if self.segments[6] { horizontal } else { " " }).repeat(self.size) + )); + + result + } +} diff --git a/Week-06/Day-36_LCD-Display/day36/src/main.rs b/Week-06/Day-36_LCD-Display/day36/src/main.rs new file mode 100644 index 0000000..1beee24 --- /dev/null +++ b/Week-06/Day-36_LCD-Display/day36/src/main.rs @@ -0,0 +1,41 @@ +use std::io::{self, Write}; +use std::process::exit; + +use day36::to_lcd; + +fn main() { + print!("Enter the number you want to display: "); + io::stdout().flush().expect("Failed to flush stdout"); + + let mut buffer = String::new(); + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let n = match buffer.trim().parse() { + Ok(n) => n, + Err(_) => { + eprintln!("The input is not a valid number"); + exit(1); + } + }; + + print!("Enter the font size: "); + io::stdout().flush().expect("Failed to flush stdout"); + + buffer.clear(); + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let s = match buffer.trim().parse() { + Ok(s) => s, + Err(_) => { + eprintln!("Invalid input"); + exit(1); + } + }; + + let result = to_lcd(n, s); + println!("{}", result); +}