Converting integers to roman numerals

前端 未结 29 2545
走了就别回头了
走了就别回头了 2020-12-02 09:16

I\'m trying to write a function that converts numbers to roman numerals. This is my code so far; however, it only works with numbers that are less than 400. Is there a quick

29条回答
  •  忘掉有多难
    2020-12-02 09:40

    The solution is lengthy, but easy to understand for a beginner. Process up to 3000

    namespace RomansTranslator
    {
        using System;
        using System.Collections.Generic;
        /// 
        /// Accepts a number (between 1 and 3000) and prints its Roman equivalent.
        /// 
        class Program
        {
            static void Main(string[] args)
            {
                string number = string.Empty;
                Console.Write("Enter the Numeric number : ");
                number = Console.ReadLine();
                if (IsValid(number)) // Validates the input
                {
                    string roman = ConvertToRoman(number);
                    Console.WriteLine("Roman Number is " + roman);
                }
                else
                {
                    Console.WriteLine("Invalid Number");
                }
                Console.ReadKey();
            }
    
            private static string ConvertToRoman(string numberString)
            {
                string romanValue = string.Empty;
                int number = Convert.ToInt32(numberString);
                if (number >= 1)
                {
                    // Loop through each roman character from highest 
                    foreach (int item in RomanDictionary().Keys)
                    {
                        while (number >= item)
                        {
                            romanValue = romanValue + RomanString(item);
                            number -= item;
                        }
                    }
                }
                return romanValue;
            }
    
            /// 
            /// Returns Roman Equvalent
            /// 
            /// 
            /// 
            private static string RomanString(int n)
            {
                string romanString = string.Empty;
                romanString = RomanDictionary()[n].ToString();
                return romanString;
            }
    
            /// 
            /// List of Roman Characters 
            /// 
            /// 
            private static Dictionary RomanDictionary()
            {
    
                Dictionary romanDic = new Dictionary();
                romanDic.Add(1000, "M");
                romanDic.Add(900, "CM");
                romanDic.Add(500, "D");
                romanDic.Add(400, "CD");
                romanDic.Add(100, "C");
                romanDic.Add(90, "XC");
                romanDic.Add(50, "L");
                romanDic.Add(40, "XL");
                romanDic.Add(10, "X");
                romanDic.Add(9, "IX");
                romanDic.Add(5, "V");
                romanDic.Add(4, "IV");
                romanDic.Add(1, "I");
                return romanDic;
            }
    
    
            /// 
            /// Validates the Input
            /// 
            /// 
            /// 
            private static bool IsValid(string input)
            {
                int value = 0;
                bool isValid = false;
                if (int.TryParse(input, out value))
                {
                    if (value <= 3000)
                    {
                        isValid = true;
                    }
                }
                return isValid;
            }
        }
    }
    

提交回复
热议问题