Advertisement
heinrich23

Bogus Roman Number

Oct 12th, 2023 (edited)
2,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | Fixit | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5.  
  6. namespace Work.Darkstar.Framework.Fun
  7. {
  8.     [DefaultValue(ROMANSYMBOL.I)]
  9.     internal enum ROMANSYMBOL
  10.     {
  11.         I = 1,
  12.         V = 5,
  13.         X = 10,
  14.         L = 50,
  15.         C = 100,
  16.         D = 500,
  17.         M = 1000
  18.     }
  19.  
  20.     /// <summary>
  21.     /// Bogus RomanNumber implementation from zen@darkstar.work is under GPL 3
  22.     /// RomanNumber is under GPL 3
  23.     /// See the GNU Library General Public License for more details.
  24.     /// </summary>
  25.     internal class RomanNumber
  26.     {
  27.  
  28.         private static readonly string RomanSymbols = "IVXLCDM";
  29.         private List<ROMANSYMBOL> romanString;
  30.        
  31.         internal List<ROMANSYMBOL> RomanString
  32.         {
  33.             get
  34.             {
  35.                 return romanString;
  36.             }
  37.             set
  38.             {
  39.                 // TODO: implement correct parsing here and throw exception
  40.                 romanString = value;
  41.             }
  42.         }
  43.  
  44.         internal RomanNumber()
  45.         {
  46.             RomanString = new List<ROMANSYMBOL>();
  47.         }
  48.  
  49.         internal List<ROMANSYMBOL> Parse(string rString)
  50.         {
  51.             List<ROMANSYMBOL> rList = new List<ROMANSYMBOL>();
  52.  
  53.             foreach (char ch in rString)
  54.             {
  55.                 if (!RomanSymbols.Contains(ch))
  56.                 {
  57.                     throw new InvalidOperationException(
  58.                         String.Format("Invalid Symbol {0} in string {1}", ch, rString));
  59.                 }
  60.             }
  61.  
  62.             // TODO: Parse correct concatenations of Roman Symbols
  63.  
  64.             return rList;
  65.         }
  66.  
  67.         public override String ToString()
  68.         {
  69.             string s = string.Empty;
  70.             foreach (ROMANSYMBOL r in RomanString)
  71.             {
  72.                 s += Enum.GetName(typeof(ROMANSYMBOL), r);
  73.             }
  74.             return s;
  75.         }
  76.  
  77.         public int ToInt()
  78.         {
  79.             int ir = 0;
  80.  
  81.             string s = this.ToString();
  82.             while (s.Contains("IV"))
  83.             {
  84.                 ir += 4;
  85.                 s.Replace("IV", "");
  86.             }
  87.             while (s.Contains("IX"))
  88.             {
  89.                 ir += 9;
  90.                 s.Replace("IX", "");
  91.             }
  92.             while (s.Contains("XL"))
  93.             {
  94.                 ir += 40;
  95.                 s.Replace("XL", "");
  96.             }
  97.             while (s.Contains("XC"))
  98.             {
  99.                 ir += 90;
  100.                 s.Replace("XC", "");
  101.             }
  102.             while (s.Contains("CD"))
  103.             {
  104.                 ir += 400;
  105.                 s.Replace("CD", "");
  106.             }
  107.             while (s.Contains("CM"))
  108.             {
  109.                 ir += 900;
  110.                 s.Replace("CM", "");
  111.             }
  112.  
  113.             while (s.Contains("I"))
  114.             {
  115.                 ir += 1;
  116.                 s.Replace("I", "");
  117.             }
  118.             while (s.Contains("V"))
  119.             {
  120.                 ir += 5;
  121.                 s.Replace("V", "");
  122.             }
  123.             while (s.Contains("X"))
  124.             {
  125.                 ir += 10;
  126.                 s.Replace("X", "");
  127.             }
  128.             while (s.Contains("L"))
  129.             {
  130.                 ir += 50;
  131.                 s.Replace("L", "");
  132.             }
  133.             while (s.Contains("C"))
  134.             {
  135.                 ir += 100;
  136.                 s.Replace("C", "");
  137.             }
  138.             while (s.Contains("D"))
  139.             {
  140.                 ir += 500;
  141.                 s.Replace("D", "");
  142.             }
  143.             while (s.Contains("M"))
  144.             {
  145.                 ir += 1000;
  146.                 s.Replace("M", "");
  147.             }
  148.  
  149.             if (ir <= 0)
  150.             {
  151.                 throw new InvalidOperationException("Roman Numbers must be greater than 0");
  152.             }
  153.  
  154.             return ir;
  155.         }
  156.     }
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement