Skip to content

Commit

Permalink
sol(rust): pset1/cash
Browse files Browse the repository at this point in the history
  • Loading branch information
jfvillablanca committed Nov 5, 2023
1 parent 2fc897d commit bd9c2e7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
7 changes: 7 additions & 0 deletions rust/pset1/cash/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions rust/pset1/cash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "cash"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
1 change: 1 addition & 0 deletions rust/pset1/cash/README.md
17 changes: 17 additions & 0 deletions rust/pset1/cash/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::io::{self, Write};

pub fn get_int(prompt: &str) -> i32 {
loop {
print!("{prompt}");
io::stdout().flush().unwrap();

let mut input_text = String::new();
match io::stdin().read_line(&mut input_text) {
Ok(_) => match input_text.trim().parse() {
Ok(input_int) => return input_int,
Err(_) => continue,
},
Err(_) => continue,
}
}
}
39 changes: 39 additions & 0 deletions rust/pset1/cash/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use cash::get_int;

fn main() {
let mut cents = loop {
let x = get_int("Change owed: ");
if x >= 0 {
break x;
}
};

let quarters = calculate_quarters(cents);
cents -= quarters * 25;

let dimes = calculate_dimes(cents);
cents -= dimes * 10;

let nickels = calculate_nickels(cents);
cents -= nickels * 5;

let pennies = calculate_pennies(cents);

println!("{}", quarters + dimes + nickels + pennies);
}

fn calculate_quarters(cents: i32) -> i32 {
cents / 25
}

fn calculate_dimes(cents: i32) -> i32 {
cents / 10
}

fn calculate_nickels(cents: i32) -> i32 {
cents / 5
}

fn calculate_pennies(cents: i32) -> i32 {
cents
}

0 comments on commit bd9c2e7

Please sign in to comment.