Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: Numeral Class

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Thu, Nov 19, 2009, 8:36 AM


This page is a Draft. Its content is not complete and might contain errors.

Source Code

Numeral Class

VB.NET

'TODO



C#

{copytext|NumeralCs}
using System;
using System.Diagnostics;

#region NumeralFormat enum
public enum NumeralFormat
{
    /// <summary>
    /// 1, 2, 3, 4, etc.
    /// </summary>
    Arabic,

    /// <summary>
    /// A, B, C, D, ... AA, BB, CC, DD, etc.
    /// </summary>
    Letter,

    /// <summary>
    /// I, II, III, IV, etc.
    /// </summary>
    Roman,

    /// <summary>
    /// One, Two, Three, etc.
    /// </summary>
    English,

    /// <summary>
    /// No numbering
    /// </summary>
    None
}
#endregion

public class Numeral
{
    //- Private Declarations ------------------------------------------------------------------
    private int _value = 0;

    //- Constructor and Public Members --------------------------------------------------------
    public Numeral(int value)
    {
        _value = value;
    }
    //-----------------------------------------------------------------------------------------
    public Numeral ChangeValue(int newValue)
    {
        _value = newValue;
        return this;
    }
    //-----------------------------------------------------------------------------------------
    public void DebugPrint(NumeralFormat format)
    {
        string result = "";
        try
        {
            result = this.ToString(format);
        }
        catch (Exception ex)
        {
            result = "ERROR: " + ex.Message;
        }
        Debug.Print("Conversion of [" + _value.ToString().PadLeft(5) + "] to ["
            + format.ToString().PadRight(13) + "] format = " + result);
    }
    //-----------------------------------------------------------------------------------------
    public string ToString(NumeralFormat format)
    {
        string result = "";
        switch (format)
        {
            case NumeralFormat.Arabic:
                result = _value.ToString();
                break;
            case NumeralFormat.Letter:
                result = ToLetter(_value);
                break;
            case NumeralFormat.Roman:
                result = ToRoman(_value);
                break;
            case NumeralFormat.None:
                //result = "";
                break;
        }
        return result;
    }
    //-----------------------------------------------------------------------------------------
    public int Value
    {
        get { return _value; }
        set { _value = value; }
    }
    //- Private Static Methods ----------------------------------------------------------------
    private static string ToLetter(int value)
    {
        try
        {
            string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            char letter = letters[value % 26 - 1];
            int count = (int)System.Math.Truncate((double)value / 26D) + 1;
            string result = letter.ToString().PadRight(count, letter);
            return result;
        }
        catch (Exception ex)
        {
            //Debug.Print(ex.Message);
            return "ERROR: " + ex.Message;
        }

    }
    //-----------------------------------------------------------------------------------------
    private static string ToRoman(int value)
    {
        if (value >= 10000 | value <= 0)
        {
            throw new OverflowException("Only positive values less than 10,000 can be " + 
                "converted to roman numerals.");
        }
        else
        {
            RomanNumeral[] numerals = {
                new RomanNumeral(1000, "M"),
                new RomanNumeral( 900, "CM"),
                new RomanNumeral( 500, "D"),
                new RomanNumeral( 400, "CD"),
                new RomanNumeral( 100, "C"),
                new RomanNumeral(  90, "XC"),
                new RomanNumeral(  50, "L"),
                new RomanNumeral(  40, "XL"),
                new RomanNumeral(  10, "X"),
                new RomanNumeral(   9, "IX"),
                new RomanNumeral(   5, "V"),
                new RomanNumeral(   4, "IV"),
                new RomanNumeral(   1, "I")

            };

            string result = "";
            foreach (RomanNumeral n in numerals)
            {
                if (value > n.Value)
                {
                    result += n.Numeral + ToRoman(value - n.Value);
                    break;
                }                    
                else if (value == n.Value)
                {
                    result += n.Numeral;
                    break;
                }                    
            }
            return result;
        }
    }
    //-----------------------------------------------------------------------------------------
    private static string ToEnglish(int value)
    {
        string result = "";




        return result;
    }
}

RomanNumeral Class

VB.NET

'TODO



C#

{copytext|RomanNumeralCs}
public class RomanNumeral
{
    int _value;
    string _numeral;

    public RomanNumeral(int value, string numeral)
    {
        _value = value;
        _numeral = numeral;
    }
    public int Value { get { return _value; }}
    public string Numeral { get { return _numeral; }}
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.