Add conversion functionality to ferinhieght 🤮

This commit is contained in:
William Davis 2024-01-04 00:53:45 -05:00
parent a0e26495f1
commit e171cbe7dc
No known key found for this signature in database
2 changed files with 20 additions and 8 deletions

View File

@ -1,4 +1,4 @@
[package]
name = "ftoc"
version = "1.1.0"
version = "2.0.0"
edition = "2021"

View File

@ -1,6 +1,6 @@
use std::io;
fn convert(temp: f32) -> f32 {
fn convert_to_c(temp: f32) -> f32 {
//subtracting 32 from the input
let mut celsius = temp - 32_f32;
//Defining what 5/9 is, then multiplying the input-32
@ -9,21 +9,33 @@ fn convert(temp: f32) -> f32 {
return celsius;
}
fn convert_to_f(temp: f32) -> f32 {
const FIVE_DIV_NINE: f32 = 5.0 / 9.0;
let mut fahrenheit = temp / FIVE_DIV_NINE;
fahrenheit += 32_f32;
return fahrenheit;
}
fn main() {
println!("Fahrenheit to Celsius converter");
println!("Fahrenheit <--> Celsius converter");
println!("Press \"^C\" to end the program");
println!("Enter your number immidiatly followed by an F or C");
loop {
let mut input = String::new();
println!("Please input the temperature in Fahrenheit:");
println!("Please input the temperature");
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
let input: f32 = match input.trim().parse() {
let input = input.trim();
let (input,system) = input.split_at(input.len() - 1);
let input: f32 = match input.parse() {
Ok(num) => num,
Err(_) => continue,
};
match system {
"C" => println!("{}°C is {:.2}°F", input, convert_to_f(input)),
"F" => println!("{}°F is {:.2}°C", input, convert_to_c(input)),
_ => continue,
};
//Prints the conversion
//and rounds to 2 decimals
println!("{}°F is {:.2}°C", input, convert(input));
}
}