mirror of
https://codeberg.org/unbeatable-101/FahrenheitToCelsius.git
synced 2024-06-18 14:57:08 +00:00
commit
a08f8ccd9a
|
@ -1,4 +1,4 @@
|
|||
[package]
|
||||
name = "ftoc"
|
||||
version = "1.1.0"
|
||||
version = "2.0.0"
|
||||
edition = "2021"
|
||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user