parser: parse roman numerals
This commit is contained in:
parent
6fbea2f9b3
commit
5714e72939
1 changed files with 26 additions and 2 deletions
28
src/lib.rs
28
src/lib.rs
|
@ -287,8 +287,32 @@ impl OrderedListNumbering {
|
|||
.map(|(d, w)| w * (d - d0 + 1))
|
||||
.sum()
|
||||
}
|
||||
Self::RomanLower => 1,
|
||||
Self::RomanUpper => 1,
|
||||
Self::RomanLower | Self::RomanUpper => {
|
||||
fn value(d: char) -> u32 {
|
||||
match d {
|
||||
'i' | 'I' => 1,
|
||||
'v' | 'V' => 5,
|
||||
'x' | 'X' => 10,
|
||||
'l' | 'L' => 50,
|
||||
'c' | 'C' => 100,
|
||||
'd' | 'D' => 500,
|
||||
'm' | 'M' => 1000,
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
let mut prev = 0;
|
||||
let mut sum = 0;
|
||||
for d in n.chars().rev() {
|
||||
let v = value(d);
|
||||
if v < prev {
|
||||
sum -= v;
|
||||
} else {
|
||||
sum += v;
|
||||
}
|
||||
prev = v;
|
||||
}
|
||||
sum
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue