2007-11-20 20:26:30 +00:00

55 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace Volian.Controls.Library
{
public static class RomanNumeral
{
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private enum RomanOffset : int
{
Hundreds = 2,
Tens = 4,
Units = 6
}
private static string _Romans = "MDCLXVI";
private static string RomanPart(RomanOffset offset, int value)
{
int iFive = value / 5;
int iUnits = value % 5;
int iFour = iUnits / 4;
return _Romans.Substring(((int)offset), iFour) +
_Romans.Substring(((int)offset) - iFive - iFour, iFive | iFour) +
"".PadRight(iUnits % 4, _Romans[((int)offset)]);
}
public static string Convert(int number)
{
int thousands = number / 1000;
int hundreds = (number % 1000) / 100;
int tens = (number % 100) / 10;
int units = number % 10;
return "".PadRight(thousands, _Romans[0]) +
RomanPart(RomanOffset.Hundreds, hundreds) +
RomanPart(RomanOffset.Tens, tens) +
RomanPart(RomanOffset.Units, units);
}
// private static int[] _TestRomans = new int[] {
// 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,25,30,40,50,60,69,70,80,90,99,
// 100,200,400,500,600,666,700,800,900,1000,1444,1666,1945,1997,1999,2000,2007,3000
// };
// public static void ShowRomans()
// {
// for (int i = 0; i < _TestRomans.Length; i++)
// {
// if(_MyLog.IsInfoEnabled)_MyLog.InfoFormat("{0},{1}", _TestRomans[i], Convert(_TestRomans[i]));
// }
// //if(_MyLog.IsInfoEnabled)_MyLog.InfoFormat("{0},{1}", 4, Convert(4));
// //if(_MyLog.IsInfoEnabled)_MyLog.InfoFormat("{0},{1}", 5, Convert(5));
// //if(_MyLog.IsInfoEnabled)_MyLog.InfoFormat("{0},{1}", 6, Convert(6));
// //if(_MyLog.IsInfoEnabled)_MyLog.InfoFormat("{0},{1}", 8, Convert(8));
// //if(_MyLog.IsInfoEnabled)_MyLog.InfoFormat("{0},{1}", 9, Convert(9));
// }
}
}