2011-12-21 66 views
1

你有一個想法如何簡化這個簡單的「翻譯機制」?如何簡化這段代碼?

是一個哈希表有用嗎?

char translateChar(char strIn) 
    { 
     char strOut = '?'; 

     if (strIn == 'A') strOut = '1'; 
     else if (strIn == 'B') strOut = '2'; 
     else if (strIn == 'C') strOut = '3'; 
     else if (strIn == 'D') strOut = '4'; 
     else if (strIn == 'E') strOut = '5'; 
     else if (strIn == 'F') strOut = '6'; 
     else if (strIn == 'G') strOut = '7'; 
     else if (strIn == 'H') strOut = '8'; 
     else if (strIn == 'I') strOut = '9'; 
     else if (strIn == 'J') strOut = '@'; 
     else if (strIn == 'K') strOut = 'A'; 
     else if (strIn == 'L') strOut = 'B'; 
     else if (strIn == 'M') strOut = 'C'; 
     else if (strIn == 'N') strOut = 'D'; 
     else if (strIn == 'O') strOut = 'E'; 
     else if (strIn == 'P') strOut = 'F'; 
     else if (strIn == 'Q') strOut = 'G'; 
     else if (strIn == 'R') strOut = 'H'; 
     else if (strIn == 'S') strOut = 'I'; 
     else if (strIn == 'T') strOut = 'J'; 
     else if (strIn == 'U') strOut = 'K'; 
     else if (strIn == 'V') strOut = 'L'; 
     else if (strIn == 'W') strOut = 'M'; 
     else if (strIn == 'X') strOut = 'N'; 
     else if (strIn == 'Y') strOut = 'O'; 
     else if (strIn == 'Z') strOut = 'P'; 
     else if (strIn == '2') strOut = 'X'; 
     else if (strIn == '1') strOut = 'Y'; 
     else if (strIn == '_') strOut = '_'; 

     return strOut; 
    } 
+0

字典是有用這裏肯定。 – 2011-12-21 13:35:44

+0

就我所知,散列表肯定會完成這項工作,但會爲您節省一些代碼字符。另一方面,每個字符都必須在散列表中進行搜索。這意味着這種方法可能不太乾淨,但更好的性能(如果你用返回'x'替換strOut ='x'),因爲這會取消任何進一步的檢查。 – 2011-12-21 13:39:19

+0

http://codereview.stackexchange.com/ – 2011-12-21 14:01:06

回答

5

我認爲這對你有幫助...

char[] strIN = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '1', '_' }; 
     char[] strOut = { '2', '3', '4', '5', '6', '7', '8', '9', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'X', 'Y', '_' }; 


     char init = 'C'; 

     int index = Array.IndexOf(strIN, init); 
     char output = strOut[index]; 
1

創建Dictionary<char,char>,用你的翻譯填充它,然後簡單地說:

return (translationDictionary.ContainsKey(strIn))? translationDictionary[strIn] : null 
3

你可以使用一個Dictionary<char, char>這樣的:

private Dictionary<char, char> mTranslationMappings = new Dictionary<char, char>(); 

// ... in .ctor ... 
mTranslationMappings.Add('2', 'X'); 
// ... add other mappings ... 

char translateChar(char strIn) 
{ 
    return mTranslationMappings[strIn]; 
} 

這可能不是最好的方法,但它是一個解決方案。

4

使用Dictionary<char, char>將每個strIn映射到它的strOut值。您可能希望將該字典設置爲在該類的構造函數中初始化的私有字段。

public class MyClass 
{ 
    private Dictionary<char, char> dict = new Dictionary<char, char>(); 

    public MyClass() 
    { 
     dict.Add('A', '1'); 
     dict.Add('B', '2'); 
     // ... and so on ... 
    } 

    public char TranslateChar(char input) 
    { 
     char result; 
     if (dict.TryGetValue(input, out result)) 
     { 
      return result; 
     } 
     return '?'; 
    } 
} 

用法:

var myClass = new MyClass(); 
Console.WriteLine(myClass.TranslateChar('A')); 
Console.WriteLine(myClass.TranslateChar('@')); 

編輯:響應評論,有一個在方式來確定特定值的鍵沒有建造。要做到這一點,你可以用這個方法:

char value = '@'; 
foreach (var kvp in dict) 
{ 
    if (kvp.Value == value) 
    { 
     Console.WriteLine("Key found: " + kvp.Key); 
     break; 
    } 
} 

或者,您可以添加一個TryGetKey擴展方法,模仿TryGetValue

public static class MyExtensions 
{ 
    public static bool TryGetKey<TKey, TValue>(
     this IDictionary<TKey, TValue> dict, 
     TValue value, 
     out TKey key) 
    { 
     key = default(TKey); 

     bool isKeyFound = false; 
     foreach (var kvp in dict) 
     { 
      if (EqualityComparer<TValue>.Default.Equals(kvp.Value, value)) 
      { 
       isKeyFound = true; 
       key = kvp.Key; 
       break; 
      } 
     } 

     return isKeyFound; 
    } 
} 

TryGetKey擴展方法用法:

char value = '@'; 
char keyResult;  
if (dict.TryGetKey(value, out keyResult)) 
{ 
    Console.WriteLine("Key found: " + keyResult); 
} 
else 
{ 
    Console.WriteLine("Key doesn't exist for value: " + value); 
} 
1

如果您查看函數的輸入和輸出,您會看到一些清晰的範圍:A-I,J,K-Z,2,1和Y.如果在if語句中使用這些範圍,則代碼將更簡單。甚至比使用字典和填充它更小。

0

假設ASCII編碼:

char translateChar(char strIn) 
{ 
    if (strIn >= 'A' && strIn <= 'I') return strIn - 'A' + '1' ; 
    if (strIn >= 'J' && strIn <= 'Z') return strIn - 'J' + '@' ; 
    if (strIn == '2') return 'X'; 
    if (strIn == '1') return 'Y'; 
    if (strIn == '_') return '_'; 

    return '?'; 
}