2016-04-08 5 views
-2

私はそれを踏んできましたが、それがまだクラッシュしている理由は分かりません。私がPrint()機能の終わりに達するまで、すべてが機能しています。あなたは下のスクリーンショットで見ることができるように`Print()`関数が実行された直後に私のプログラムがクラッシュするのはなぜですか?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace StringSet 
{ 

    class StringSet 
    { 

     private List<List<string>> _Buckets; 
     private int _numStrings; 

     public StringSet () 
     { 
      this._Buckets = new List<List<string>>(); 
      this._numStrings = 0; 
     } 

     public StringSet (string[] S) 
     { 
      // better way to do this? 
      this._Buckets = new List<List<string>>(); 
      foreach (string s in S) this._Buckets.Add(new List<string>()); 
      foreach (string s in S) { this.Insert(s); } 
     } 

     private int _GetBucketNumber (string s, List<List<string>> Buckets) 
     { 
      //  s: string whose index to look up 
      // Buckets: source buckets 

      // disallow empty or NULL strings 
      if (String.IsNullOrEmpty(s)) { throw new ArgumentException("Cannot add empty or NULL string to set"); } 
      if (Buckets.Count == 0) { throw new ArgumentException("Tried to call _GetBucketNumber on empty bucket list"); } 

      // XOR characters together and mod by length of buckets 
      char c = s[0]; 
      for (int i = 1; i < s.Length; ++i) { c ^= s[i]; } 
      return (int)c % Buckets.Count; 
     } 

     private void _RehashIfNecessary () 
     { 
      // if the number of strings in the set exceeds the number of buckets, 
      // increase the number of buckets to either double its current size 
      // or the largest number of buckets possible, whichever is smaller 
      if (this._numStrings > this._Buckets.Count) 
      { 
       List<List<string>> NewBuckets = new List<List<string>>(Math.Min(this._Buckets.Count * 2, Int32.MaxValue)); 
       foreach (List<string> Bucket in this._Buckets) 
       { 
        foreach (string s in Bucket) 
        { 
         NewBuckets[this._GetBucketNumber(s, NewBuckets)].Add(s); 
        } 
       } 
       this._Buckets = NewBuckets; 
      } 
     } 

     public void Insert (string s) 
     { 
      // disallow empty or NULL strings 
      if (String.IsNullOrEmpty(s)) { throw new ArgumentException("Cannot add empty or NULL string to set"); } 

      // Get bucket that string belongs in 
      List<string> Bucket = this._Buckets[this._GetBucketNumber(s,this._Buckets)]; 
      // Add if not already there 
      if (Bucket.IndexOf(s) == -1) { Bucket.Add(s); } 
      ++_numStrings; _RehashIfNecessary(); 
     } 

     public bool Contains (string s) 
     { 
      // returns true or false depending on whether s is a 
      // string currently in the set 

      return (this._Buckets[this._GetBucketNumber(s,this._Buckets)].IndexOf(s) != -1); 
     } 

     public void Print () 
     { 
      for (int i = 0; i < this._Buckets.Count; ++i) 
      { 
       Console.WriteLine("Bucket {0}: {1}", i, string.Join(",",this._Buckets[i].ToArray())); 
      } 
     } 

    } 

    class Program 
    { 
     static void Main (string[] args) 
     { 
      string[] strs = new string[] { "apple", "potato", "car", "cat", "dog", "sheep", "Trump" }; 
      try 
      { 
       StringSet TestSet = new StringSet(strs); 
       TestSet.Print(); 
      } 
      catch (Exception E) 
      { 
       Console.WriteLine("Exception occured: {0}", E.Message); 
      } 
     } 
    } 
} 

は、ハッシュセットの作成が正常に動作しますように、プログラム全体が見えます(理想的には、各バケットで1つの要素が存在することになる)

enter image description here

が、エラーでクラッシュします

'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Users\Me\Documents\Visual Studio 2013\Projects\StringSet\StringSet\bin\Debug\StringSet.vshost.exe'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
The thread 0x12ec has exited with code 259 (0x103). 
The thread 0x1524 has exited with code 259 (0x103). 
'StringSet.vshost.exe' (CLR v4.0.30319: StringSet.vshost.exe): Loaded 'C:\Users\Me\Documents\Visual Studio 2013\Projects\StringSet\StringSet\bin\Debug\StringSet.exe'. Symbols loaded. 
The thread 0x180c has exited with code 259 (0x103). 
The thread 0x1aa4 has exited with code 259 (0x103). 
The program '[2556] StringSet.vshost.exe' has exited with code 0 (0x0). 

何が間違っているのか分かりませんでした!

+0

をConsole.Read()の呼び出しを入れてくださいすべてがうまく走ったように見えることに合意しました。あなたのすべての文字列を出力し、コード0で終了しました。 –

+1

出力を読み返してみてください。最後の行: "プログラム '[2556] StringSet.vshost.exe'がコード0(0x0)で終了しました。"最後の100年ほどかけてコード0を終了してください(それほど大丈夫ではありません)。エラーはありませんでした。 Ergo:プログラムが存在しました。コンソールプログラムは、コードの終わりにそれを行います。 – TomTom

答えて

2

それがクラッシュしていない、コンソールウィンドウは、コードの完了後に終了しますcatchブロックの後

関連する問題