Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. use std::str::{FromStr};
  2.  
  3. fn main() {
  4. let weight = input::<i64>("Patient's weight (kg):");
  5. let time_passed = input::<i64>("How much time has passed from the previous dose (full hours): ");
  6. let total_dose = input::<i64>("The total dose for the last 24 hours (mg): ");
  7. if total_dose >= 4000{
  8. println!("You can't take another dose this day")
  9. } else {
  10. let next_dose = calculate_dose( weight, time_passed, total_dose);
  11. println!("The amount of Parasetamol to give to the patient: {}", next_dose)
  12. }
  13. }
  14.  
  15. fn calculate_dose(weight: i64, time_passed: i64, total_dose: i64) -> i64{
  16. if time_passed < 6 {
  17. 0
  18. } else {
  19. let next_dose = weight * 15;
  20. if next_dose + total_dose > 4000 {
  21. 4000 - total_dose
  22. } else {
  23. next_dose
  24. }
  25. }
  26. }
  27.  
  28. fn input<F>(prompt: &str) -> F where
  29. F: FromStr {
  30. use std::io::{stdin, self, Write};
  31. print!("{}", prompt);
  32. io::stdout().flush().unwrap();
  33. let mut guess = String::new();
  34. let _= stdin().read_line(&mut guess);
  35. let a = guess.to_string().trim().parse::<F>();
  36. match a {
  37. Ok(v) => v,
  38. Err(_) => panic!("Parse failed!")
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement