如何简化这段代码

本文关键字:代码 段代码 何简化 | 更新日期: 2023-09-27 18:18:35

你知道如何简化这个简单的"翻译机制"吗?

哈希表有用吗?

    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;
    }

如何简化这段代码

我想这会对你有帮助。

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];

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

EDIT:作为对评论的回应,没有内置的方法来确定特定值的键。为此,您可以使用以下方法:

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

或者,您可以添加一个模仿TryGetValueTryGetKey扩展方法:

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);
}

您可以这样使用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];
}

这可能不是最好的方法,但它是一个解决方案。

创建一个Dictionary<char,char>,用你的翻译填充它,然后简单地:

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

如果您查看函数的输入和输出,您会看到一些明确的范围:A-I, J, K-Z, 2, 1和y。如果您在If语句中使用这些,您的代码将更加简单。甚至比用字典填充还要小。

假设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 '?';
}