|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+use std::fmt;
|
|
|
2
|
+#[derive(Debug)]
|
|
|
3
|
+pub struct Roman(String);
|
|
|
4
|
+
|
|
|
5
|
+impl fmt::Display for Roman {
|
|
|
6
|
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
7
|
+ write!(f, "{}", self.0)
|
|
|
8
|
+ }
|
|
|
9
|
+}
|
|
|
10
|
+
|
|
|
11
|
+impl PartialEq<Roman> for &'static str {
|
|
|
12
|
+ fn eq(&self, roman: &Roman) -> bool {
|
|
|
13
|
+ self.to_string() == roman.0
|
|
|
14
|
+ }
|
|
|
15
|
+}
|
|
|
16
|
+
|
|
|
17
|
+impl From<i32> for Roman {
|
|
|
18
|
+ fn from(arabic_number: i32) -> Self {
|
|
|
19
|
+ let stages = vec![1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
|
|
|
20
|
+ let mut an = arabic_number;
|
|
|
21
|
+ let mut value = String::new();
|
|
|
22
|
+ for s in stages {
|
|
|
23
|
+ while an >= s {
|
|
|
24
|
+ value.push_str(&Roman::to_roman(s));
|
|
|
25
|
+ an -= s;
|
|
|
26
|
+ }
|
|
|
27
|
+ }
|
|
|
28
|
+ Roman::new(value)
|
|
|
29
|
+ }
|
|
|
30
|
+}
|
|
|
31
|
+
|
|
|
32
|
+impl Roman {
|
|
|
33
|
+ fn to_roman(number: i32) -> String {
|
|
|
34
|
+ match number {
|
|
|
35
|
+ 1 => "I",
|
|
|
36
|
+ 4 => "IV",
|
|
|
37
|
+ 5 => "V",
|
|
|
38
|
+ 9 => "IX",
|
|
|
39
|
+ 10 => "X",
|
|
|
40
|
+ 40 => "XL",
|
|
|
41
|
+ 50 => "L",
|
|
|
42
|
+ 90 => "XC",
|
|
|
43
|
+ 100 => "C",
|
|
|
44
|
+ 400 => "CD",
|
|
|
45
|
+ 500 => "D",
|
|
|
46
|
+ 900 => "CM",
|
|
|
47
|
+ 1000 => "M",
|
|
|
48
|
+ _ => "",
|
|
|
49
|
+ }.to_string()
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ fn new(string: String) -> Self {
|
|
|
53
|
+ Roman(string)
|
|
|
54
|
+ }
|
|
|
55
|
+}
|