NumberConversion

mail@pastecode.io avatar
unknown
csharp
a year ago
1.5 kB
2
Indexable
Never
using System.Collections.Generic;

namespace ConsoleApp3
{
    internal class Program
    {
        static void Main(string[] args)
        {

         var NumberConversion = new Dictionary<char, int>();
            NumberConversion['I'] = 1;
            NumberConversion['V'] = 5;
            NumberConversion['X'] = 10;
            NumberConversion['L'] = 50;
            NumberConversion['C'] = 100;
            NumberConversion['D'] = 500;
            NumberConversion['M'] = 1000;

            string number = Console.ReadLine();

            int value = 0;
            int value2 = 0;
            bool flag = true;
            foreach (var i in number)
            {
                if (NumberConversion.TryGetValue(i, out value))
                {
                    if (value > value2)
                    {
                        value -= value2;
                    }
                    else
                    {
                        value += value2;
                    }
                    value2 = value;
                }
                else
                {
                    Console.WriteLine($"Ошибка ввода, неверный символ {i}");
                    flag = false;
                    
                }
   
            }
            Console.WriteLine(flag ? $"Результат: {value}" : "Попробуйте еще раз");
        }
    }
}