2

我正在尝试按如下方式显示我的结果:

-.-|-    [tab]  kt   
-.|--    [tab]  nm  
-.|-|-   [tab]  ntt

但这是我目前的输出

-.-|-|  kt  
-.|--|  nm  
-.|-|-|  [tab]ntt

每个莫尔斯电码的末尾都有一个|我想删除的,因为它在末尾。

还因为用户可以在点和破折号之间输入带有空格的摩尔斯电码 - 我注意到它会影响字符的对齐方式,并且并非所有字符都被正确标记。选项卡这个词不应该显示我只是写了它,因为我不知道如何放置一个真正的选项卡。

private static readonly IDictionary<char, string> morseCode_alpha = new Dictionary<char, string>
{
    {'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',"--.."}
};

private static string ConvertMorseToText(string symbolCode)
{
    var builder = new StringBuilder(4 * symbolCode.Length);

    foreach (char c in symbolCode)
        builder.Append(morseCode_alpha[c]);
    return builder.ToString();
}

private static string ConvertTextToMorse(char ch)
{
    if (morseCode_alpha.Keys.Contains(ch))
        return morseCode_alpha[ch];
    else
        return string.Empty;
}

private static string ConvertStringToMorse(string letters)
{
    StringBuilder sb = new StringBuilder();
    foreach (char ch in letters)
    {
        if (sb.Length != 0 && sb[sb.Length - 1] != ' ')
            sb.Append("|");
        sb.Append(ConvertTextToMorse(ch));
    }
    return sb.ToString();
}

private static IEnumerable<string> Permutations( string symbolCode)
{
    int n = symbolCode.Length;
    if (n == 0 || symbolCode.Length == 0)
        yield return " ";
    else
        foreach (var entry in morseCode_alpha)
            if (symbolCode.StartsWith(entry.Value))
                foreach (string next in Permutations(symbolCode.Substring(entry.Value.Length)))
                    yield return entry.Key + next;
}

private static void Write( string rest)
{
    string result = ConvertStringToMorse(rest);
    Console.Write(result+"\t");
    Console.WriteLine(rest);
}

static void Main(string[] args)
{
    string morseInput;
    string entered = "";
    do
    {
        Console.WriteLine("Enter Morse Code: \n");
        morseInput = Console.ReadLine().Replace(" ","");
        bool isValid = Regex.IsMatch(morseInput, @"^[-.]+$");
        if (isValid)
        {
            Console.WriteLine("\nAll permutations:\n");
            string morse = ConvertMorseToText(entered);
            string permutations = morseInput.Substring(morse.Length);
            Write(permutations);
            var nexts = new List<string>(Permutations(permutations));
            foreach (string next in nexts)
                Write(next);
        }
        else
        {
            Console.WriteLine("\nFormat of morse must be only dots and dashes.");
            Console.WriteLine("Parameter name: "+morseInput+"\n");
        }
    }
    while (morseInput.Length != 0);
}
4

3 回答 3

3

而且,要回答问题的另一部分...

Tabstops 是为控制台编写而固定的,所以最好使用类似 String.PadRight 的东西

所以,你的代码可能是:

private static void Write(string rest)
{
    string result = ConvertStringToMorse(rest);
    Console.Write(result.PadRight(20));
    Console.WriteLine(rest);
}
于 2013-01-28T21:53:55.310 回答
2

该方法的草稿版本:

private static string ConvertStringToMorse(string letters)
{
    var result = string.Join("|",
        letters
            .Select(ConvertTextToMorse)
            .Where(morse => !string.IsNullOrEmpty(morse)));
    return result;
}

更新:

请注意,该entered变量只使用一次:当定义时 -分配空字符串。然后ConvertMorseToText(entered)调用该方法:它总是为空字符串参数返回空字符串。在此分配之后string permutations = morseInput.Substring(morse.Length);permutations变量将存储与变量完全相同的值morse(因为morse.Length始终为 0)。

所以,entered变量和ConvertMorseToText()方法似乎没用(两者都可以安全地删除):

static void Main(string[] args)
{
    do
    {
        Console.WriteLine("Enter Morse Code: ");

        string morseInput = Console.ReadLine();
        if (string.IsNullOrWhiteSpace(morseInput))
        {
            // Empty or consists only of white-space characters
            break;
        }

        morseInput = morseInput.Replace(" ", "");

        bool isValid = Regex.IsMatch(morseInput, @"^[-.]+$");
        if (isValid)
        {
            Console.WriteLine("All permutations:");
            Console.WriteLine();
            var nexts = Permutations(morseInput).ToList();
            foreach (string next in nexts)
                Write(next);
        }
        else
        {
            Console.WriteLine();
            Console.WriteLine("Format of morse must be only dots and dashes.");
            Console.WriteLine("Parameter name: {0}", morseInput);
        }
    }
    while (true);
}

更新 2:考虑使用TryGetValue()方法Dictionary<TKey, TValue>而不是Keys.Contains[](索引器),即不要执行两次查找:

private static string ConvertTextToMorse(char ch)
{
    string result;
    return morseCode_alpha.TryGetValue(ch, out result) ? result : string.Empty;
}
于 2013-01-28T21:55:06.017 回答
1

相反,这段代码:

Console.Write(result+"\t");
Console.WriteLine(rest);

采用

Console.WriteLine("{0,-10}{1,-10}", result, rest);

然后您将看到左对齐的两列(每列最多 10 个字符)。或者如果您想要右对齐,请删除“-”符号。

于 2013-01-28T21:53:20.807 回答