Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- namespace Work.Darkstar.Framework.Fun
- {
- [DefaultValue(ROMANSYMBOL.I)]
- internal enum ROMANSYMBOL
- {
- I = 1,
- V = 5,
- X = 10,
- L = 50,
- C = 100,
- D = 500,
- M = 1000
- }
- /// <summary>
- /// Bogus RomanNumber implementation from [email protected] is under GPL 3
- /// RomanNumber is under GPL 3
- /// See the GNU Library General Public License for more details.
- /// </summary>
- internal class RomanNumber
- {
- private static readonly string RomanSymbols = "IVXLCDM";
- private List<ROMANSYMBOL> romanString;
- internal List<ROMANSYMBOL> RomanString
- {
- get
- {
- return romanString;
- }
- set
- {
- // TODO: implement correct parsing here and throw exception
- romanString = value;
- }
- }
- internal RomanNumber()
- {
- RomanString = new List<ROMANSYMBOL>();
- }
- internal List<ROMANSYMBOL> Parse(string rString)
- {
- List<ROMANSYMBOL> rList = new List<ROMANSYMBOL>();
- foreach (char ch in rString)
- {
- if (!RomanSymbols.Contains(ch))
- {
- throw new InvalidOperationException(
- String.Format("Invalid Symbol {0} in string {1}", ch, rString));
- }
- }
- // TODO: Parse correct concatenations of Roman Symbols
- return rList;
- }
- public override String ToString()
- {
- string s = string.Empty;
- foreach (ROMANSYMBOL r in RomanString)
- {
- s += Enum.GetName(typeof(ROMANSYMBOL), r);
- }
- return s;
- }
- public int ToInt()
- {
- int ir = 0;
- string s = this.ToString();
- while (s.Contains("IV"))
- {
- ir += 4;
- s.Replace("IV", "");
- }
- while (s.Contains("IX"))
- {
- ir += 9;
- s.Replace("IX", "");
- }
- while (s.Contains("XL"))
- {
- ir += 40;
- s.Replace("XL", "");
- }
- while (s.Contains("XC"))
- {
- ir += 90;
- s.Replace("XC", "");
- }
- while (s.Contains("CD"))
- {
- ir += 400;
- s.Replace("CD", "");
- }
- while (s.Contains("CM"))
- {
- ir += 900;
- s.Replace("CM", "");
- }
- while (s.Contains("I"))
- {
- ir += 1;
- s.Replace("I", "");
- }
- while (s.Contains("V"))
- {
- ir += 5;
- s.Replace("V", "");
- }
- while (s.Contains("X"))
- {
- ir += 10;
- s.Replace("X", "");
- }
- while (s.Contains("L"))
- {
- ir += 50;
- s.Replace("L", "");
- }
- while (s.Contains("C"))
- {
- ir += 100;
- s.Replace("C", "");
- }
- while (s.Contains("D"))
- {
- ir += 500;
- s.Replace("D", "");
- }
- while (s.Contains("M"))
- {
- ir += 1000;
- s.Replace("M", "");
- }
- if (ir <= 0)
- {
- throw new InvalidOperationException("Roman Numbers must be greater than 0");
- }
- return ir;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement