2016-11-10 2 views
2

私はC#でコードを作成しています。そこで、作成するセットの数をユーザーに尋ね、そのセットに要素を入力します。それらのセットから、彼は2セットを選択し、選択されたセットの和集合を表示する。以下のコードで C#で2組のUnionを表示

は、セット内の要素は_itemsに追加取得されていないと連合は表示されません。

ありがとうございました。

namespace Union 
{ 

    class Program 
    { 
    static List<SortedSet<string>> _items = new List<SortedSet<string>>(); 
    static SortedSet<string> set = new SortedSet<string>(); 

    static void Main(string[] args) 
    { 
     int i, j, a, b; 
     string k; 
     Console.WriteLine("\n Enter the number of set to be used: "); 
     i = Convert.ToInt32(Console.ReadLine()); 
     for (j = 1; j <= i; j++) 
     { 
      SortedSet<string> set = new SortedSet<string>(); 
      do 
      { 
       Console.WriteLine("Enter first element in set {0}:", j); 
       k = Console.ReadLine(); 
       if (k != "stop") 
        set.Add(k); 
      } while (k != "stop"); 
      _items.Add(set); 
     } 
     Console.WriteLine("Enter index of 1st set of union:{0}"); 
     b = Convert.ToInt32(Console.ReadLine()); 
     Console.WriteLine("Enter index of 2nd set of union:{0}"); 
     c = Convert.ToInt32(Console.ReadLine()); 
     DisplayUnion(a, b); 
    } 

    public static void DisplayUnion(int a, int b) 
    { 
     SortedSet<string> set1 = _items[a]; 
     SortedSet<string> set2 = _items[b]; 
     set1.UnionWith(set2); 
     Console.WriteLine(set1); 
    } 
    } 
} 

答えて

1

は完全に、より良い表現を実現し、国境のケースのシナリオを含めるようにMain()DisplayUnion(int a, int b)方法をmodifiingことで私の答えを編集しました。 Main()方法:

static void Main(string[] args) 
{ 
    int i, j, a, b; 
    string k; 
    Console.WriteLine("Enter the number of sets to be used: "); 
    i = Convert.ToInt32(Console.ReadLine()); 
    for (j = 1; j <= i; j++) 
    { 
     SortedSet<string> set = new SortedSet<string>(); 
     var index = 0; 
     do 
     { 
      index++; 
      Console.WriteLine($"Enter {index} element in set {j}:"); 
      k = Console.ReadLine(); 
      if (k != "stop") 
       set.Add(k); 
     } while (k != "stop"); 
     _items.Add(set); 
    } 

    if (_items.Count == 0) 
    { 
     Console.WriteLine("You have no sets to union."); 
     return; 
    } 

    if (_items.Count == 1) 
    { 
     Console.WriteLine("Union of only set is: " + string.Join("", _items[0])); 

     return; 
    } 

    while (true) 
    { 
     Console.WriteLine("Enter index of 1st set of union:{0}"); 
     a = Convert.ToInt32(Console.ReadLine()); 
     if (a < _items.Count) 
     { 
      break; 
     } 

     Console.WriteLine($"Set {a} does not exists."); 
    } 

    while (true) 
    { 
     Console.WriteLine("Enter index of 2nd set of union:{0}"); 
     b = Convert.ToInt32(Console.ReadLine()); 
     if (b < _items.Count) 
     { 
      break; 
     } 

     Console.WriteLine($"Set {b} does not exists."); 
    } 

}

DisplayUnion(int a, int b)方法:

public static void DisplayUnion(int a, int b) 
{ 

    SortedSet<string> set1 = _items[a]; 
    SortedSet<string> set2 = _items[b]; 
    set1.UnionWith(set2); 
    Console.WriteLine($"Union of set {a + 1} with set {b + 1} is: " + string.Join("", set1)); 
} 

あなたはそれが実際にinvalid.Iを追加することによってそれを解決していますことをどこでもチェックせずにセットのための無効なインデックスを入力するので、あなたはoutOfRangeException受け取ります2つのwhile cyclesを追加します。 また、セットが0または1である場合に、国境の場合に2つのif statementsを追加しました。

希望します。

+0

しかし、私はまだ_itemsのエラーを取得しています[A]の範囲の例外のうち、その引数は、未処理のだった...。このため –

+0

理由は、世界中どこででも無効なインデックスをチェックしないことです。編集を参照してください。 –

関連する問題