我试图按如下方式显示我的结果:
-.-|- [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);
}
答案 0 :(得分:3)
而且,要回答问题的其他部分......
Tabstops是固定的,用于控制台编写,所以最好使用像String.PadRight
这样的东西所以,你的代码可能是:
private static void Write(string rest)
{
string result = ConvertStringToMorse(rest);
Console.Write(result.PadRight(20));
Console.WriteLine(rest);
}
答案 1 :(得分: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;
}
方法,即不要执行两次查找:
{{1}}
答案 2 :(得分:1)
而是这段代码:
Console.Write(result+"\t");
Console.WriteLine(rest);
使用
Console.WriteLine("{0,-10}{1,-10}", result, rest);
然后你会看到左对齐的两列(每个最多10个字符)。如果您想要正确对齐,请删除“ - ”符号。