2013-07-17 37 views
6

我想弄清楚如何將羅馬數字轉換爲整數。這是我的代碼的一部分。當我提示用戶輸入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; 
      } 
     } 
    } 
} 
+3

你的主題爲「轉換整數成羅馬數字」,但你的代碼表明「轉換羅馬數字爲整數。」你究竟在問哪一個? (他們是對立的。)請[編輯]你的問題,並澄清你問的是什麼。 –

+0

一些建議:當編寫這樣的解決方案時,如果弄清楚你將如何解決問題,那麼將更容易,那麼編碼該解決方案。拿出一張紙並嘗試想出一種方法來正確計算某些樣品的羅馬數字(只是使用數字和東西,而不是在紙上寫代碼!)。然後,編碼解決方案。 –

+0

你能解釋一下你「展望未來」的情況嗎?我可能會幫助你解決問題嗎? PS。如果檢查結果不正確,您可以使用開關/大小寫或者更改所有if語句,但是首先改爲「else if」。 –

回答

11

轉換羅馬數字的技巧是向後工作(從字符串的末尾)不轉發,使它變得更容易。

例如,如果你有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; 
     } 
    } 

注意:這不驗證羅馬數字,只是轉換那些已是有效的。

+1

M不小於X .....所以你會加上M –

+0

好吧,「讓它容易得多」。我會接受的。 – user2246674

+0

也值得注意,這是一個經典的編碼kata(以及保齡球遊戲)http://codingdojo.org/cgi-bin/wiki.pl?KataRomanNumerals –

0

你需要添加邏輯基本上說,如果V在M之前,那麼減去它。根據這條線的位置:

如果(溫度== 'V') { INPROGRESS = INPROGRESS + 5;

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"); 
+0

爲什麼你將'char's轉換爲'byte's? – Guillaume

+0

因爲我必須給數組中的元素一個索引號。 – halit