Skip to content

Commit

Permalink
Rust version now works
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Johansson committed Aug 9, 2013
1 parent 69fbb79 commit 70fa92d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*~
*.o
*.exe
*.hi
Expand Down
3 changes: 2 additions & 1 deletion rust/before
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
rustc roman.rs
rustc -o roman.exe roman.rs
#rust test roman.rs

2 changes: 1 addition & 1 deletion rust/numeral_to_roman
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
./roman $1
./roman.exe $1
18 changes: 16 additions & 2 deletions rust/roman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ fn main() {
}

fn convert(num: int) {
println(fmt!("You entered: '%d'", num));
times(num / 1000, ~"M");
let mut romanNum = times(num / 1000, ~"M");
romanNum = romanNum + timesMany(num / 100 % 10, ~"C", ~"D", ~"M");
romanNum = romanNum + timesMany(num / 10 % 10, ~"X", ~"L", ~"C");
romanNum = romanNum + timesMany(num % 10, ~"I", ~"V", ~"X");

println(romanNum)
}

fn times(num: int, ch: ~str) -> ~str {
Expand All @@ -29,6 +33,16 @@ fn times(num: int, ch: ~str) -> ~str {
s
}

fn timesMany(num: int, ones: ~str, fives: ~str, tens: ~str) -> ~str {
match(num) {
1..3 => times(num,ones),
4 => ones + fives,
5..8 => fives + times(num -5,ones),
9 => ones + tens,
_ => ~"",
}
}

#[test]
fn times_with_program_input_1000_should_return_M() {
let m = times(1000/1000,~"M");
Expand Down

0 comments on commit 70fa92d

Please sign in to comment.