控制台中结果的显示格式

本文关键字:显示格式 结果 控制台 | 更新日期: 2023-09-27 17:56:36

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

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

控制台中结果的显示格式

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

Tab 位是固定的控制台写入,因此最好使用 String.PadRight 之类的东西

因此,您的代码可能是:

private static void Write(string rest)
{
    string result = ConvertStringToMorse(rest);
    Console.Write(result.PadRight(20));
    Console.WriteLine(rest);
}

该方法的草稿版本:

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

相反,这段代码:

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

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

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