diff options
author | garhve <git@garhve.com> | 2022-12-22 18:03:28 +0800 |
---|---|---|
committer | garhve <git@garhve.com> | 2022-12-22 18:03:28 +0800 |
commit | 36920adbaa85bd1be5ad37d7a22212231179930f (patch) | |
tree | 4cdc0aaaff479b6f41b39d68e7ba49d53867b096 /rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs | |
parent | b5aa889f7fced8ba2cc1698ae9696d7bd0ca8ab5 (diff) |
finish chapter 3
Diffstat (limited to 'rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs')
-rw-r--r-- | rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs b/rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs new file mode 100644 index 0000000..971811e --- /dev/null +++ b/rust/theBook/chapter-3-variables-and-mutability/practice/convert-temp/src/main.rs @@ -0,0 +1,59 @@ +use std::io; +use std::process; + +fn read_input() -> i32 { + let mut input = String::new(); + + io::stdin() + .read_line(&mut input) + .expect("failed to read line"); + + let input = match input.trim().parse() { + Ok(input) => input, + Err(_) => { + println!("not a number!"); + process::exit(1); + }, + }; + input +} + +fn main() { + println!("Convert temperatures between Fahrenheit and Celsius"); + println!(""); + + loop { + println!("Please choose converting method:"); + println!("1) Fahrenheit to Celsius 2) Celsius to Fahrenheit"); + println!("3) exit"); + + let choice = read_input(); + + if choice == 0 { + let _choice = read_input(); + } else if choice == 1 { + fah_to_cel(); + } else if choice == 2 { + cel_to_fah(); + } else if choice == 3 { + break + }; + } + println!("Thinks for using"); +} + +fn fah_to_cel() { + println!("Please enter a Fahrenheit temperature"); + let fah = read_input() as f32; + + println!("the Celsius of {fah} is {}", (fah-32.0)/1.8); + println!(""); +} + +fn cel_to_fah() { + println!("Please enter a Celsius temperature"); + let cel = read_input() as f32; + + println!("The Fahrenteit of {cel} is {}", cel*1.8+32.0); + println!(""); +} |