Merge branch 'FandC'

Closes #1
Implements #1
This commit is contained in:
William Davis 2024-01-04 01:03:19 -05:00
commit a08f8ccd9a
No known key found for this signature in database
2 changed files with 26 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,39 @@ fn convert(temp: f32) -> f32 {
return celsius;
}
fn convert_to_f(temp: f32) -> f32 {
//Defining what 5/9 is, then deviding the input by it and adding 32
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();
//Split the input into to strings, input: Contains the number
//and system: Contains if it is celcius or fahrenheit
let (input,system) = input.split_at(input.len() - 1);
//If the input in a number, output it, else, restart loop
let input: f32 = match input.parse() {
Ok(num) => num,
Err(_) => continue,
};
//If the system is know, call the convert function, else,
//repeat loop
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));
}
}