2011-12-21 8 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

辞書はここに間違いなく便利です。 –

+0

私が知る限り、ハッシュテーブルは確かに仕事をするでしょうが、コードのいくつかの文字を保存します。一方、すべての文字をハッシュテーブルで検索する必要があります。つまり、このアプローチは、それ以上のチェックをキャンセルするので、(おそらくstrOut = 'x'をreturn 'x';に置き換えると)このアプローチは多少クリーンでパフォーマンスは向上します。 –

+0

http://codereview.stackexchange.com/ –

答えて

5

Iこれがあなたに役立つと思います...

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>を使用して、それぞれstrInstrOut値にマッピングします。辞書のプライベートフィールドをクラスのコンストラクタで初期化することができます。

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('@')); 

編集:コメントに反応して、何が特定の値のキーを決定する方法で構築ありません。そのためには、このアプローチを使用することができます:あなたはTryGetKey拡張メソッドを追加することができ、

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

をか、その模倣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 '?'; 
} 
関連する問題