Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# Roman Numerals solution - Victor Lee

By: a guest on May 23rd, 2013  |  syntax: C#  |  size: 2.38 KB  |  views: 29  |  expires: Never
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. class RomanNumber
2.     {
3.         /// <summary>
4.         /// The possible characters used to represent a single digit in a Roman number.
5.         /// Example: For tens digit, Ten = C (100), Five = L (50), One = X (10)
6.         /// </summary>
7.         class RomanDigit
8.         {
9.             public char Ten { get; set; }
10.             public char Five { get; set; }
11.             public char One { get; set; }
12.         }
13.
14.         #region Constants
15.         static List<RomanDigit> RomanDigits = new List<RomanDigit>()
16.         {
17.             new RomanDigit() {Ten = 'X', Five = 'V', One = 'I'},    // Ones place
18.             new RomanDigit() {Ten = 'C', Five = 'L', One = 'X'},    // Tens place
19.             new RomanDigit() {Ten = 'M', Five = 'D', One = 'C'},    // Hundreds place
20.             new RomanDigit() {One = 'M'},                           // Thousands place
21.         };
22.         static int MAX = 3999;
23.         static int MIN = 0;
24.         #endregion
25.
26.         /// <summary>
27.         /// Converts an Arabic number to a Roman number by converting one digit at a time.
28.         /// </summary>
29.         /// <param name="arabic"></param>
30.         /// <returns></returns>
31.         public static string ToRoman(int arabic)
32.         {
33.             if (arabic > MAX || arabic < MIN) throw new ArgumentOutOfRangeException("Arabic number must between 0 and 3999.");
34.             string roman = "";
35.
36.             int decimalPlace = 0;   // Start at 10^0 (ones place)
37.             while (arabic > 0)
38.             {
39.                 // Get lowest digit
40.                 int currentDigit = arabic % 10;
41.
42.                 // Add converted string to output
43.                 RomanDigit digit = RomanDigits[decimalPlace];
44.                 if (currentDigit % 5 == 4)  // Special case for 4 and 9, print "One" followed by "Five" or "Ten".
45.                     roman = string.Format("{0}{1}", digit.One, (currentDigit == 9) ? digit.Ten : digit.Five) + roman;
46.                 else // If currentDigit > 5, print "Five". Then print "One" several times until done.
47.                     roman = string.Format("{0}{1}", (currentDigit >= 5) ? digit.Five.ToString() : "", new String(digit.One, currentDigit % 5)) + roman;
48.
49.                 // Next loop iteration
50.                 arabic /= 10;    // Divide by 10 to get next digit
51.                 decimalPlace++;  // Go to next RomanDigit
52.             }
53.
54.             return roman;
55.         }
56.     }
clone this paste RAW Paste Data
Top