temp converter thing i actually made myself!

This commit is contained in:
Emily 2020-07-09 18:43:11 +10:00
parent cfe2547809
commit 838e802f79
3 changed files with 65 additions and 0 deletions

11
converter/.gitignore vendored Normal file
View file

@ -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

9
converter/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "converter"
version = "0.1.0"
authors = ["Emily J. <me@emily.im>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

45
converter/src/main.rs Normal file
View file

@ -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
}