Wrote program for Day 53

This commit is contained in:
Mariano Riefolo 2024-09-16 10:45:03 +02:00
parent c967ea8a12
commit 2e22b2400b
5 changed files with 58 additions and 1 deletions

View File

@ -99,7 +99,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
| Day #50 | [Tic Tac Toe](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-50_Tic-Tac-Toe) | :white_check_mark: | | Day #50 | [Tic Tac Toe](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-50_Tic-Tac-Toe) | :white_check_mark: |
| Day #51 | [Asteroid Collision](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-51_Asteroid-Collision) | :white_check_mark: | | Day #51 | [Asteroid Collision](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-51_Asteroid-Collision) | :white_check_mark: |
| Day #52 | [Switch On The Gravity](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-52_Switch-On-The-Gravity) | :white_check_mark: | | Day #52 | [Switch On The Gravity](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-52_Switch-On-The-Gravity) | :white_check_mark: |
| Day #53 | [Javelin Parabolic Throw](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-53_Javelin-Parabolic-Throw) | :white_large_square: | | Day #53 | [Javelin Parabolic Throw](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-53_Javelin-Parabolic-Throw) | :white_check_mark: |
| Day #54 | [RGB To Hex Color Convertor](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-54_RGB-To-Hex-Color-Converter) | :white_large_square: | | Day #54 | [RGB To Hex Color Convertor](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-54_RGB-To-Hex-Color-Converter) | :white_large_square: |
| Day #55 | [Filter Repeating Character Strings](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-55_Filter_Repeating-Character-Strings) | :white_large_square: | | Day #55 | [Filter Repeating Character Strings](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-55_Filter_Repeating-Character-Strings) | :white_large_square: |
| Day #56 | [Convert To Hex](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-56_Convert-To-Hex) | :white_large_square: | | Day #56 | [Convert To Hex](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-56_Convert-To-Hex) | :white_large_square: |

View File

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

View File

@ -0,0 +1,8 @@
pub fn javelin_throw(initial_speed: f32, angle: u8) -> (u16, u16) {
const G: f32 = 9.81;
let angle = angle as f32;
let h_max = initial_speed * initial_speed * f32::sin(angle.to_radians()).powi(2) / (2f32 * G);
let d = initial_speed * initial_speed * f32::sin(2f32 * angle.to_radians()) / G;
(h_max.round() as u16, d.round() as u16)
}

View File

@ -0,0 +1,24 @@
use std::io::{self, Write};
use day53::javelin_throw;
fn read_number<T>() -> T
where
T: std::str::FromStr,
<T as std::str::FromStr>::Err: std::fmt::Debug,
{
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
input.trim().parse().unwrap()
}
fn main() {
print!("Enter the initial speed: ");
io::stdout().flush().unwrap();
let initial_speed: f32 = read_number();
print!("Enter the throw angle: ");
io::stdout().flush().unwrap();
let angle: u8 = read_number();
let (h_max, d) = javelin_throw(initial_speed, angle);
println!("Ymax={}, Xmax={}", h_max, d);
}

View File

@ -0,0 +1,19 @@
#[cfg(test)]
mod examples {
use day53::javelin_throw;
#[test]
fn example1() {
assert_eq!(javelin_throw(36.7, 45), (34, 137));
}
#[test]
fn example2() {
assert_eq!(javelin_throw(51.3, 20), (16, 172));
}
#[test]
fn example3() {
assert_eq!(javelin_throw(100.1, 89), (511, 36));
}
}