2015-04-03 47 views
2

我试图构建一个Morse转换器,下面是我的c#代码的一部分,但是当我运行它时,它告诉我索引超出范围,可以任何人修复它?我是新编程:)索引超出了数组c#的范围,莫尔斯转换器

private void BTNconvert_Click(object sender, EventArgs e) 
{   
    string input = TBinput.Text; 
    string[] output=new string[input.Length]; 
    for (int index=0;index<input.Length;index++) 
    { 
     index=input.IndexOf('a',index); 
     output[index]=".-";            
    } 

    for (int index = 0; index < input.Length; index++) 
    { 
     index = input.IndexOf('b', index); 
     output[index] = "-..."; 
    } 

    LBcodes.Text = string.Join(" ",output); 
+0

索引是从零开始的。你应该重复输入。Leenght - 1. – MvdD 2015-04-03 03:36:22

+0

好吧。我想通过这种方式我可以在第一个元素中搜索原始字符串。 – 2015-04-03 03:38:23

+1

@ user18044:他检查它是否小于('<')input.Length,所以这不是问题。 – StriplingWarrior 2015-04-03 03:38:54

回答

0
private void BTNconvert_Click(object sender, EventArgs e) 
{  
    int index;  
    string input = TBinput.Text; 
    string[] output=new string[input.Length]; 
     index = -1; 
     do{ 
      index=input.IndexOf('a',index+1); 
      if(index==-1)break; 
      output[index]=".-";  
     }while(true); 
     index = -1; 
     do{ 
      index=input.IndexOf('b',index+1); 
      if(index==-1)break; 
      output[index]="-...";  
     }while(true); 
    } 

    LBcodes.Text = string.Join(" ",output); 

如果你还打算让这些循环对所有的字符我会建议您这样做:

private void BTNconvert_Click(object sender, EventArgs e) 
{ 

    int index; 
    char[] source1 = {'a','b','c',....,'z'} //replace ... with characters 
    string[] source2 = {".-","-...",....} //replace .... with Morse equivalents 
    string input = TBinput.Text; 
    string[] output=new string[input.Length]; 
     for(int i=0;i<26;i++){ 
     index = -1; 
     do{ 
      index=input.IndexOf(source1[i],index+1); 
      if(index==-1)break; 
      output[index]=source2[i];  
     }while(true); 
     } 
    } 

    LBcodes.Text = string.Join(" ",output); 
+0

与我的代码我试图键入一个字符串只有一个和B,它也不工作,同样的错误发生,如果我取下第二个搜索'B'的循环,而我只能输入'a',它是有效的。 – 2015-04-03 03:57:44

+0

我是新来的堆栈流程,我检查了左边的按钮到您的答案,是否标记? – 2015-04-03 04:03:50

+0

@RobbieNirvanaLiu是的。检查我的答案的第二部分我认为它会帮助你更多。 – 2015-04-03 04:05:49

4

你看到这个错误的原因是,IndexOf()将返回-1值,如果给定的搜索词是不是在字符串中发现的,所以当你尝试设置output[-1]你最终会得到一个无效的索引。

+0

这可能是一个原因,它不工作。我试图用a和b键入一个字符串,但它不起作用,并且发生同样的错误,如果我取下搜索'b'的第二个循环,并且只输入'a',它就会起作用。 – 2015-04-03 03:49:41

+0

谢谢,我想现在我明白了 – 2015-04-03 04:05:00

0

如果是我,我会保持每个字符的字典映射它是相应的莫尔斯字符串。这将使来回转换变得容易。

例如:

private static Dictionary<char, string> MorseMap = 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', "--.."},{'1', ".----"}, {'2', "..---"}, 
    {'3', "...--"}, {'4', "....-"},{'5', "....."}, {'6', "-...."}, 
    {'7', "--..."}, {'8', "---.."},{'9', "----."}, {'0', "-----"}, 
    {'.', ".-.-.-"}, {',', "--..--"},{'?', "..--.."}, {'\'', ".----."}, 
    {'!', "-.-.--"}, {'/', "-..-."},{'(', "-.--."}, {')', "-.--.-"}, 
    {'&', ".-..."}, {':', "---..."},{';', "-.-.-."}, {'=', "-...-"}, 
    {'+', ".-.-."}, {'-', "-....-"},{'_', "..--.-"}, {'"', ".-..-."}, 
    {'$', "...-..-"}, {'@', ".--.-."} 
}; 

现在,使用从该映射中的键和值,它很容易进行编码和解码,以莫尔斯电:

private static string ConvertToMorse(string input) 
{ 
    var morse = new StringBuilder(); 

    foreach (var character in input) 
    { 
     var upperCaseChar = char.ToUpper(character); 

     if (MorseMap.ContainsKey(upperCaseChar)) 
     { 
      morse.Append(MorseMap[upperCaseChar]); 
     } 
     else 
     { 
      // If there's no mapping for this character, just add it 
      morse.Append(character); 
     } 

     // Add a space between each morse string. 
     morse.Append(' '); 
    } 

    return morse.ToString().Trim(); 
} 

private static string ConvertToAlpha(string morse) 
{ 
    var alpha = new StringBuilder(); 

    // Split words on double-spaces so we can add single spaces back where needed 
    var morseCodeWords = morse.Split(new[] {" "}, StringSplitOptions.None); 

    foreach (var morseCodeWord in morseCodeWords) 
    { 
     var morseCodeStrings = morseCodeWord.Split(' '); 

     foreach (var morseCodeString in morseCodeStrings) 
     { 
      if (MorseMap.ContainsValue(morseCodeString)) 
      { 
       alpha.Append(MorseMap.First(item => item.Value == morseCodeString).Key); 
      } 
      else 
      { 
       // If there's no mapping for the string, just add it 
       alpha.Append(morseCodeString); 
      } 
     } 

     // Add a space between each word 
     alpha.Append(' '); 
    } 

    return alpha.ToString(); 
} 

使用例:

private static void Main() 
{ 
    var test = "This is my test string."; 

    var morseVersion = ConvertToMorse(test); 
    var alphaVersion = ConvertToAlpha(morseVersion); 

    Console.WriteLine("Original string ... {0}", test); 
    Console.WriteLine("Morse version ..... {0}", morseVersion); 
    Console.WriteLine("Alpha version ..... {0}", alphaVersion); 
} 
0

如果在输入字符串中找不到指定的文本,IndexOf()将返回-1。只有在文本框中输入指定的字符,您的代码才能正常工作。这就是为什么当你删除第二个循环并输入所有a到文本框时,你的代码工作正常。

除此之外,你需要使用2个循环。你在两个循环中都做同样的事情。