7

我想弄清楚如何将罗马数字转换为整数。这是我的代码的一部分。当我提示用户输入 M 时,它显示 1000,但是当我提示用户输入一个罗马数字(如 VM)时,它没有给我 995 而是 1005。这是因为我告诉我的程序这样做。

我想弄清楚的是如何向前看并让它知道它何时添加或减去罗马数字。

我该如何开始做这件事?

class Roman
{

    public int inprogress = 0;
    public Roman(string roman)
    {

        char temp = 'Z';
        int length;

        length = roman.Length;

        for (int i = 0; i < length; i++)
        {
            temp = roman[i];
            if (temp == 'M')
            {
                inprogress = inprogress + 1000;
            }
            if (temp == 'D')
            {
                inprogress = inprogress + 500;
            }
            if (temp == 'C')
            {
                inprogress = inprogress + 100;
            }
            if (temp == 'L')
            {
                inprogress = inprogress + 50;
            }
            if (temp == 'X')
            {
                inprogress = inprogress + 10;
            }
            if (temp == 'V')
            {
                inprogress = inprogress + 5;
            }
            if (temp == 'I')
            {
                inprogress = inprogress + 1;
            }
        }
    }
}
4

3 回答 3

14

转换罗马数字的技巧是向后(从字符串的末尾)而不是向前工作,这使它更容易。

例如,如果您有 IX

  • 你从 X 开始,= 10
  • 后退 1.... 现在它的 I,I 小于 X 所以现在减去 1 = 9

参考解决方案......

public class RomanNumeral
    {
        public static int ToInt(string s)
        {
              var last = 0;
              return s.Reverse().Select(NumeralValue).Sum(v =>
              {                    
                var r = (v >= last)? v : -v;
                last = v;
                return r;
              });
        }

        private static int NumeralValue(char c)
        {
            switch (c)
            {
                case 'I': return 1;
                case 'V': return 5;
                case 'X': return 10;
                case 'L': return 50;
                case 'C': return 100;
                case 'D': return 500;
                case 'M': return 1000;                    
            }
            return 0;
        }
    }

注意:这不会验证罗马数字,只需转换已经有效的数字。

于 2013-07-17T23:04:14.153 回答
1
 List<Level> levels = new List<Level>();
    int[] val = new int[255];
    private void Form1_Load(object sender, EventArgs e)
    {

        val[(byte)'I'] = 1;
        val[(byte)'V'] = 5;
        val[(byte)'X'] = 10;
        val[(byte)'L'] = 50;
        val[(byte)'C'] = 100;
        val[(byte)'D'] = 500;
        val[(byte)'M'] = 1000;
        levels.Clear();
        levels.Add(new Level('I', 'V', 'X'));
        levels.Add(new Level('X', 'L', 'C'));
        levels.Add(new Level('C', 'D', 'M'));
    }
    int fromRoman(string n)
    {
        n = n.ToUpper();

        var result = 0;
        var lastDigit = 0;
        for (var pos = n.Length - 1; pos >= 0; pos--)
        {
            var curDigit = val[(byte)n[pos]];


            if (curDigit >= lastDigit)
                result += curDigit;
            else
                result -= curDigit;

            lastDigit = curDigit;
        }

        return result;
    }
    public class Level
    {
        public Level(char i, char v, char x)
        {
            this.i = i;
            this.x = x;
            this.v = v;
        }
        public char i;
        public char v;
        public char x;
    }

然后运行

int Result =  fromRoman("X");
于 2013-07-17T23:37:23.120 回答
0

您需要添加基本上说如果 V 在 M 之前的逻辑,则将其减去。基于这里的这条线:

if (temp == 'V') { inprogress = inprogress + 5;

于 2013-07-17T23:07:40.970 回答