From c496d12aa216f8166b6eb461366ad0a57e553600 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Thu, 6 Oct 2022 13:29:41 -0700 Subject: [PATCH] Initial commit --- .gitignore | 1 + LICENSE | 24 ++++++++++++++++++++++++ bmi-calc/Cargo.lock | 7 +++++++ bmi-calc/Cargo.toml | 8 ++++++++ bmi-calc/src/main.rs | 29 +++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 bmi-calc/Cargo.lock create mode 100644 bmi-calc/Cargo.toml create mode 100644 bmi-calc/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68a49da --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/bmi-calc/Cargo.lock b/bmi-calc/Cargo.lock new file mode 100644 index 0000000..aba4a98 --- /dev/null +++ b/bmi-calc/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bmi-calc" +version = "0.1.0" diff --git a/bmi-calc/Cargo.toml b/bmi-calc/Cargo.toml new file mode 100644 index 0000000..066da18 --- /dev/null +++ b/bmi-calc/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "bmi-calc" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/bmi-calc/src/main.rs b/bmi-calc/src/main.rs new file mode 100644 index 0000000..157f148 --- /dev/null +++ b/bmi-calc/src/main.rs @@ -0,0 +1,29 @@ +use std::io::{self, Write}; + +fn read_float(prompt: &str) -> f32 { + print!("{}", prompt); + let _ = io::stdout().flush(); + let mut line = String::new(); + let _ = io::stdin().read_line(&mut line); + let flt = line[..line.len() - 1].parse::(); + if flt.is_err() { + println!("Invalid number: '{}'", line); + return read_float(prompt); + } + return flt.unwrap(); +} + +fn calculate_bmi(kg_weight: f32, cm_height: f32) -> f32 { + let m_height = cm_height / 100.0; + return kg_weight / (m_height * m_height); +} + +fn main() { + let in_height = read_float("Enter height (inches): "); + let lb_weight = read_float("Enter weight (pounds): "); + let cm_height = in_height * 2.54; + let kg_weight = lb_weight / 2.20462; + println!("Your metric height: {}cm", cm_height); + println!("Your metric weight: {}kg", kg_weight); + println!("Your BMI: {}", calculate_bmi(kg_weight, cm_height)); +}