Wrote program for Day 36

This commit is contained in:
Mariano Riefolo 2024-08-30 13:40:55 +02:00
parent e8941f5411
commit 27b2603b3c
4 changed files with 135 additions and 1 deletions

View File

@ -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 #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 #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 #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 #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 #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: | | Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_large_square: |

View File

@ -0,0 +1,6 @@
[package]
name = "day36"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,87 @@
pub fn to_lcd(mut n: usize, s: usize) -> String {
let height = 2 * s + 3;
let mut digits: Vec<Vec<String>> = 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<String> {
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
}
}

View File

@ -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);
}