Skip to content

Instantly share code, notes, and snippets.

@gabeio
Last active March 3, 2016 20:42
Show Gist options
  • Select an option

  • Save gabeio/8914f5a191fd7d07083c to your computer and use it in GitHub Desktop.

Select an option

Save gabeio/8914f5a191fd7d07083c to your computer and use it in GitHub Desktop.
use std::io::{BufReader,BufRead};
use std::fs::File; // File::open
use std::env; // argv_os
fn main() {
let fd = BufReader::new(File::open(env::args_os().nth(1).unwrap()).unwrap());
let arr: Vec<Vec<f64>> = fd.lines()
.map(|l| l.unwrap().split(';')
.map(|number| number.parse().unwrap())
.collect())
.collect();
for x in arr {
if x[0] > x[1] {
println!("ERROR");
} else if x[0] == x[1] {
println!("ZERO");
} else {
let mut nx = x[1] - x[0];
let mut comma = false;
'inf: loop {
if nx <= 0.001 {
break;
}
if comma {
print!(",");
}
// giant denomination if-else
if nx >= 100.0 {
nx -= 100.0;
print!("ONE HUNDRED");
} else if nx >= 50.0 {
nx -= 50.0;
print!("FIFTY");
} else if nx >= 20.0 {
nx -= 20.0;
print!("TWENTY");
} else if nx >= 10.0 {
nx -= 10.0;
print!("TEN");
} else if nx >= 5.0 {
nx -= 5.0;
print!("FIVE");
} else if nx >= 2.0 {
nx -= 2.0;
print!("TWO");
} else if nx >= 1.0 {
nx -= 1.0;
print!("ONE");
} else if nx >= 0.5 {
nx -= 0.5;
print!("HALF DOLLAR");
} else if nx >= 0.25 {
nx -= 0.25;
print!("QUARTER");
} else if nx >= 0.10 {
nx -= 0.10;
print!("DIME");
} else if nx >= 0.05 {
nx -= 0.05;
print!("NICKEL");
} else if nx >= 0.01 {
nx -= 0.01;
print!("PENNY");
}
comma = true; // from now on print a comma
}
println!(""); // new line
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment