罗马数字加法实现Roman Calculator

开发一款程序教小孩子认识罗马数字,小孩子已经对罗马数字很熟悉了,可是不明确罗马数字的加法。在面试技能六中已经对罗马数字的构成做了具体描写叙述。

如今做的程序也是在面试技能六的约束下。加法的求和最大限定在3000以内。

        /// <summary>
        /// 罗马数字转十进制数
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static int ConvertRomanToDecimal(string number)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            dic.Add("M", 1000);
            dic.Add("CM", 900);
            dic.Add("D", 500);
            dic.Add("CD", 400);
            dic.Add("C", 100);
            dic.Add("XC", 90);
            dic.Add("L", 50);
            dic.Add("XL", 40);
            dic.Add("X", 10);
            dic.Add("IX", 9);
            dic.Add("V", 5);
            dic.Add("IV", 4);
            dic.Add("I", 1);

            int len = number.Length;
            if (len == 1)
            {
                return dic[number];
            }

            if (len > 1)
            {
                int i = 0;
                int sum = 0;
                while (i < len)
                {
                    int step = 1;
                    if (len - i > 1)
                    {
                        step = 2;
                    }

                    string cnum = number.Substring(i, step);
                    if (dic.ContainsKey(cnum))
                    {
                        sum += dic[cnum];
                        i = i + step;
                    }
                    else
                    {
                        sum += dic[number.Substring(i, 1)];
                        i = i + 1;
                    }
                }

                return sum;
            }

            return -1;
        }
    }

        /// <summary>
        /// 十进制转罗马数字
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string ConvertDecimalToRoman(int number)
        {
            int[] decArray = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
            string[] romAarry = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
            int i = 0;
            string output = "";

            while (number > 0)
            {
                while (number >= decArray[i])
                {
                    number = number - decArray[i];
                    output = output + romAarry[i];
                }
                i++;
            }
            return output;
        }
        /// <summary>
        /// 加法器
        /// </summary>
        /// <param name="s">输入罗马加法公式</param>
        /// <returns></returns>
        public static string RomanCalculator(string s)
        {
            string[] array = s.Split('+');
            int sum = ConvertRomanToDecimal(array[0].Trim()) + ConvertRomanToDecimal(array[1].Trim());
            return ConvertDecimalToRoman(sum);
        }
调用RomanCalculator()函数,输入例如以下的測试数据进行測试。

Input                            Output

XX + II                           XXII

I + V                              VI

II + II                             IV

CCC + CCC               DC

D + D                           M


posted on 2017-05-01 12:55  wgwyanfs  阅读(463)  评论(0编辑  收藏  举报

导航