From 838e802f79c54a17ba7545ca2c06223cacfd816d Mon Sep 17 00:00:00 2001 From: Emily J Date: Thu, 9 Jul 2020 18:43:11 +1000 Subject: [PATCH] temp converter thing i actually made myself! --- converter/.gitignore | 11 +++++++++++ converter/Cargo.toml | 9 +++++++++ converter/src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 converter/.gitignore create mode 100644 converter/Cargo.toml create mode 100644 converter/src/main.rs diff --git a/converter/.gitignore b/converter/.gitignore new file mode 100644 index 0000000..0eec03d --- /dev/null +++ b/converter/.gitignore @@ -0,0 +1,11 @@ +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk \ No newline at end of file diff --git a/converter/Cargo.toml b/converter/Cargo.toml new file mode 100644 index 0000000..78f41a3 --- /dev/null +++ b/converter/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "converter" +version = "0.1.0" +authors = ["Emily J. "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/converter/src/main.rs b/converter/src/main.rs new file mode 100644 index 0000000..4e59e2e --- /dev/null +++ b/converter/src/main.rs @@ -0,0 +1,45 @@ +use std::io; + +fn main() { + println!("This program can convert between celcius and farenheight."); + println!("Which unit would you like to convert to?"); + + let mut unit = String::new(); + let mut temp = String::new(); + + io::stdin() + .read_line(&mut unit) + .expect("Failed to read line."); + + unit = unit.trim().to_lowercase(); + + println!("Please type in the temperature:"); + + io::stdin() + .read_line(&mut temp) + .expect("Failed to read line."); + + let temp_trimmed = temp.trim(); + let temp_f64: f64 = temp_trimmed.parse() + .expect("Invalid input, please enter an valid integer!"); + + println!("{}", temp_f64); + + if unit == "fahrenheit" || unit == "f" { + let output = to_f(temp_f64); + println!("{}C is equal to {}F", temp_f64, output); + } else if unit == "celcius" || unit == "c" { + let output = to_c(temp_f64); + println!("{}F is equal to {}C", temp_f64, output); + } else { + println!("Input is not a valid temperature unit, please try again."); + } +} + +fn to_f(input: f64) -> f64 { + (input * 9.0/5.0) + 32.0 +} + +fn to_c(input: f64) -> f64 { + (input - 32.0) * 5.0/9.0 +}