Объединение, пересечение и разность двух множеств - C#

Узнай цену своей работы

Формулировка задачи:

Помогите пожалуйста сделать таки операции над множеством как объединение, пересечение и разность Вот часть моего кода
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Myset
  7. {
  8. public class MySortedSet<T> : ICloneable, IEquatable<MySortedSet<T>>, IEnumerable<T> where T : IComparable
  9. {
  10. int count;
  11. T[] data;
  12. public int Count { get { return count; } }
  13. int capacity;
  14. public MySortedSet(int capacity)
  15. {
  16. this.capacity = capacity;
  17. data = new T[capacity];
  18. }
  19. public void Add(T value)
  20. {
  21. if (capacity <= count)
  22. throw new Exception("Set overflow");
  23. for (int i = 0; i < count; i++)
  24. {
  25. if (value.CompareTo(data[i]) == 0)
  26. return;
  27. if (value.CompareTo(data[i]) < 0)
  28. {
  29. for (int j = count - 1; j >= i; j--)
  30. {
  31. data[j + 1] = data[j];
  32. }
  33. data[i] = value;
  34. count++;
  35. return;
  36. }
  37. }
  38. data[count] = value;
  39. count++;
  40. }
  41. public void Delet(T value)
  42. {
  43. for (int i = 0; i < count; i++)
  44. {
  45. if (data[i].Equals(value))
  46. {
  47. for (int j = i; j < count; j++)
  48. {
  49. data[j] = data[j + 1];
  50. }
  51. count -= 1;
  52. }
  53. else throw new Exception("No this element in set");
  54. }
  55. }
  56.  
  57. public void Search(T value)
  58. {
  59. for (int i = 0; i < count; i++)
  60. {
  61. if (data[i].Equals(value))
  62. {
  63. return true;
  64. }
  65. else throw new Exception("No this element in set");
  66. }
  67. }
  68. public static operator *(MySortedSet<T> data, MySortedSet<T> array_2)
  69. {
  70. int k;
  71. k=0;
  72. s3.size=0;
  73. for(int i=0;i<count;i++)
  74. for(int j=0;j<s2.size;j++)
  75. if(s2.a[j]==a[i])
  76. {
  77. s3.a[k]=a[i];
  78. k++;
  79. s3.size+=1;
  80. }
  81. return s3;
  82. }
  83.  
  84. public bool Equals(MySortedSet<T> other)
  85. {
  86. if (other == null)
  87. return false;
  88. if (this.data == other.data)
  89. return true;
  90. else
  91. return false;
  92. }
  93.  
  94. public object Clone()
  95. {
  96. throw new NotImplementedException();
  97. }
  98. public int CompareTo(MySortedSet<T> other)
  99. {
  100. throw new NotImplementedException();
  101. }
  102. public IEnumerator<T> GetEnumerator()
  103. {
  104. for (int i = 0; i < count; i++ )
  105. {
  106. yield return data[i];
  107. }
  108. }
  109. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  110. {
  111. throw new NotImplementedException();
  112. }
  113. }
  114. class Program
  115. {
  116. static void Main(string[] args)
  117. {
  118. var set = new MySortedSet<int>(10);
  119. set.Add(2);
  120. set.Add(1);
  121. set.Add(2);
  122. set.Add(3);
  123. set.Add(10);
  124. set.Delet(1);
  125. set.Search(10);
  126. Console.WriteLine("Count = {0}", set.Count);
  127. Console.WriteLine("Items:");
  128. foreach(var item in set)
  129. Console.WriteLine(item);
  130. }
  131. }
  132. }

Решение задачи: «Объединение, пересечение и разность двух множеств»

textual
Листинг программы
  1.  static void Show(SortedSet<char> ss, string s)
  2.         {
  3.             Console.WriteLine(s);
  4.             foreach (char ch in ss)
  5.                 Console.Write(ch + " ");
  6.             Console.WriteLine("\n");
  7.         }
  8.         static void Main(string[] args)
  9.         {
  10.             SortedSet<char> m1 = new SortedSet<char>();
  11.             SortedSet<char> m2 = new SortedSet<char>();
  12.             m1.Add('A');
  13.             m1.Add('B');
  14.             m1.Add('C');
  15.             m1.Add('Z');
  16.  
  17.             m2.Add('X');
  18.             m2.Add('Y');
  19.             m2.Add('Z');
  20.        
  21.            m1.SymmetricExceptWith(m2);
  22.             Show(m1,"пересечение");
  23.  
  24.             m1.UnionWith(m2);
  25.             Show(m1, "объединение");
  26.          
  27.             m1.ExceptWith(m2);
  28.             Show(m1, "вычитание");
  29.             Console.ReadKey();
  30.  
  31.      
  32.         }
  33.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4.2 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы