Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[5046] Number Systems

Last post 09-11-2007 4:29 AM by Nimpo. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [5046] Number Systems

    This thread is for discussions of Number Systems.

    • Post Points: 10
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 05-24-2007 9:10 PM In reply to

    • pmccombs
    • Not Ranked
    • Joined on 05-24-2007
    • United States
    • New Member
    • Points 10

    Re: [5046] Number Systems

    Thanks for the Roman Numeral example! I made some equivalent functions in C# using Enum's static methods. This results in more concise versions of the two primary methods described in the article. They have been named a little bit differently to suit my tastes, but are the same methods in terms of the algorithm used. This code also uses the exact same RomanNumerals enum (which I haven't included in this snippet). Feel free to suggest further improvements, or to translate this back to VB.NET:

          
            private int ParseRomanNumeral(string numeral)
            {
                numeral = numeral.ToUpper();

                char[] characters = numeral.ToCharArray();
                int[] parseBuf = new int[numeral.Length];
                int result = 0;
                bool bSkip = false;
                for (int i = numeral.Length - 1; i >= 0; i--)
                {
                    if (bSkip)
                    {
                        bSkip = false; // Skip this digit because it is part of a 2-digit numeral
                        continue;
                    }
                    parseBuf[i] = (int)Enum.Parse(typeof(RomanNumerals), characters[i].ToString());
                    if (parseBuf[i] % 5 == 0 && i > 0)
                    {
                        string extended = characters[i-1].ToString() + characters[i].ToString();
                        try
                        {
                            parseBuf[i] = (int)Enum.Parse(typeof(RomanNumerals), extended);
                            bSkip = true;
                        }
                        catch(ArgumentException)
                        {
                            // Ignore this exception. It just means we didn't find an extended numeral
                        }
                    }
                }

                for (int i = 0; i < numeral.Length; i++)
                {
                    result += parseBuf[i];
                }
                return result;
            }

            private string ConvertToRomanNumeral(int num)
            {
                string sResult = "";

                sResult = Enum.GetName(typeof(RomanNumerals), num);
                if (sResult == null)
                {
                    string[] names = Enum.GetNames(typeof(RomanNumerals));
                    int[] vals = (int[])Enum.GetValues(typeof(RomanNumerals));
                    for (int i = names.Length - 1; i > 0; i--) // Don't process the 0th element, since it is 0 and results in bad math
                    {
                        int temp = num / vals[i];
                        num -= temp * vals[i];
                        for (int j = 0; j < temp; j++)
                        {
                            sResult += names[i];
                        }
                    }
                }

                return sResult;
            }





























































    • Post Points: 10
  • 09-11-2007 4:29 AM In reply to

    • Nimpo
    • Top 25 Contributor
    • Joined on 03-16-2004
    • United States
    • Guru
    • Points 7,520

    Re: [5046] Number Systems

    I have just returned from an extended absence from this forum so I realize this is rather late.

    It looks good, the only thing I can spot is the line where you catch the Exception.  Not finding an extended numeral is expected (and common at that), so an Exception shouldn't be used to control the flow of code.

    Although I can't think of an easy way around it since they removed Enum.TryParse (2.0 and later I think).








    If you want to know more about me, check out my MySpace.

    Tech & Philosophy - Philosophy in Technology Blog
    Caution, bmp in Road - GDI+ Blog
    • Post Points: 5
Page 1 of 1 (3 items)