Write a function to convert a number in Roman numeral form to a decimal representation.
Sigiloso
I wrote this function recursively but I realized after it would have been easier to write it iteratively.
Sigiloso
Sorry here goes the correct One using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.Write(" enter the number :"); int num = int.Parse(Console.ReadLine()); Rome p = new Rome(num); Console.WriteLine(" the roman number of " + num + " is :: "+p.getroman() ); Console.ReadKey(); } } class Rome { public static Dictionary d; public static SortedDictionary spl; private int num; public Rome(int n) { this.num = n; d = new Dictionary(); d.Add(0, ""); d.Add(1, "I"); d.Add(2, "II"); d.Add(3, "III"); d.Add(4, "IV"); d.Add(5, "V"); d.Add(6, "VI"); d.Add(7, "VII"); d.Add(8, "VIII"); d.Add(9, "IX"); spl = new SortedDictionary(); spl.Add(1000, "M"); spl.Add(900, "CM"); spl.Add(500, "D"); spl.Add(400, "CD"); spl.Add(100, "C"); spl.Add(90, "XC"); spl.Add(50, "L"); spl.Add(40, "XL"); spl.Add(10, "X"); } public String getroman() { int n = this.num; String output = ""; LinkedList k = new LinkedList(); k = Rome.split(n); IEnumerator e = k.GetEnumerator(); while (e.MoveNext()) { if (d.ContainsKey(e.Current)) { output = output + d[e.Current]; } else if (spl.ContainsKey(e.Current)) { output = output + spl[e.Current]; } else { int b = e.Current; int val = nearval(b); int s = val; while (val l = spl.Keys; for (int m = 0; m di) diff = di; } int val = 0; if (diff > l.ElementAt(l.Count - 1)) val = l.Last(); else val = b - diff; return val; } public static LinkedList split(int b) { LinkedList h = new LinkedList(); int fact = 10; while (b!= 0) { int k = b % fact; h.AddFirst(k); b = b - k; fact = fact * 10; } return h; } } }
Sigiloso
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.Write(" enter the number :"); int num = int.Parse(Console.ReadLine()); Rome p = new Rome(num); Console.WriteLine(" the roman number of " + num + " is :: "+p.getroman() ); Console.ReadKey(); } } class Rome { public static Dictionary d; public static SortedDictionary spl; private int num; public Rome(int n) { this.num = n; d = new Dictionary(); d.Add(0, ""); d.Add(1, "I"); d.Add(2, "II"); d.Add(3, "III"); d.Add(4, "IV"); d.Add(5, "V"); d.Add(6, "VI"); d.Add(7, "VII"); d.Add(8, "VIII"); d.Add(9, "IX"); spl = new SortedDictionary(); spl.Add(1000, "M"); spl.Add(900, "CM"); spl.Add(500, "D"); spl.Add(400, "CD"); spl.Add(100, "C"); spl.Add(90, "XC"); spl.Add(50, "L"); spl.Add(40, "XL"); spl.Add(10, "X"); } public String getroman() { int n = this.num; String output = ""; LinkedList k = new LinkedList(); k = Rome.split(n); IEnumerator e = k.GetEnumerator(); while (e.MoveNext()) { if (d.ContainsKey(e.Current)) { output = output + d[e.Current]; } else if (spl.ContainsKey(e.Current)) { output = output + spl[e.Current]; } else { int b = e.Current; int diff = 23456789; ICollection l = spl.Keys; for (int m = 0; m di) diff = di; } int val =0; if(diff>l.ElementAt(l.Count-1)) val = l.Last(); else val = b - diff; int s = val; while (val split(int b) { LinkedList h = new LinkedList(); int fact = 10; while (b!= 0) { int k = b % fact; h.AddFirst(k); b = b - k; fact = fact * 10; } return h; } } }