Created
June 12, 2020 07:15
-
-
Save cuongnguyen0527/aa349b08e2a7aa6e6162bf1e1b9e762b to your computer and use it in GitHub Desktop.
First program written in Rust to convert degree between Farenheit (F) and Celcius (C)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
fn main() { | |
println!("Please choose mode:"); | |
println!("1> C degree to F degree "); | |
println!("2> F degree to C degree "); | |
let mut mode = String::new(); | |
loop { | |
io::stdin().read_line(&mut mode).expect("Fail to read line"); | |
mode = mode.trim().to_string(); | |
if mode == "1" || mode == "2" { | |
break | |
} else { | |
mode = String::new(); | |
continue; | |
} | |
} | |
let mut input = String::new(); | |
if mode == "2" { | |
println!("Please input farenheit and let us convert it to celcius!"); | |
io::stdin().read_line(&mut input).expect("Input is not a number"); | |
let input: i32 = input.trim().parse().expect("Cannot convert to number"); | |
println!("The celcius is {}", f_to_c(input)); | |
} else { | |
println!("Please input celcius and let us convert it to farenheit!"); | |
io::stdin().read_line(&mut input).expect("Input is not a number"); | |
let input: i32 = input.trim().parse().expect("Cannot convert to number"); | |
println!("The farenheit is {}", c_to_f(input)); | |
} | |
} | |
fn f_to_c(f: i32) -> i32{ | |
(f - 32) * 5 / 9 | |
} | |
fn c_to_f(c: i32) -> i32{ | |
c * 9 / 5 + 32 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The binary file is here, download and give it a try!