Объединение, пересечение и разность двух множеств - C#
Формулировка задачи:
Помогите пожалуйста сделать таки операции над множеством как объединение, пересечение и разность
Вот часть моего кода
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myset
{
public class MySortedSet<T> : ICloneable, IEquatable<MySortedSet<T>>, IEnumerable<T> where T : IComparable
{
int count;
T[] data;
public int Count { get { return count; } }
int capacity;
public MySortedSet(int capacity)
{
this.capacity = capacity;
data = new T[capacity];
}
public void Add(T value)
{
if (capacity <= count)
throw new Exception("Set overflow");
for (int i = 0; i < count; i++)
{
if (value.CompareTo(data[i]) == 0)
return;
if (value.CompareTo(data[i]) < 0)
{
for (int j = count - 1; j >= i; j--)
{
data[j + 1] = data[j];
}
data[i] = value;
count++;
return;
}
}
data[count] = value;
count++;
}
public void Delet(T value)
{
for (int i = 0; i < count; i++)
{
if (data[i].Equals(value))
{
for (int j = i; j < count; j++)
{
data[j] = data[j + 1];
}
count -= 1;
}
else throw new Exception("No this element in set");
}
}
public void Search(T value)
{
for (int i = 0; i < count; i++)
{
if (data[i].Equals(value))
{
return true;
}
else throw new Exception("No this element in set");
}
}
public static operator *(MySortedSet<T> data, MySortedSet<T> array_2)
{
int k;
k=0;
s3.size=0;
for(int i=0;i<count;i++)
for(int j=0;j<s2.size;j++)
if(s2.a[j]==a[i])
{
s3.a[k]=a[i];
k++;
s3.size+=1;
}
return s3;
}
public bool Equals(MySortedSet<T> other)
{
if (other == null)
return false;
if (this.data == other.data)
return true;
else
return false;
}
public object Clone()
{
throw new NotImplementedException();
}
public int CompareTo(MySortedSet<T> other)
{
throw new NotImplementedException();
}
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < count; i++ )
{
yield return data[i];
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
var set = new MySortedSet<int>(10);
set.Add(2);
set.Add(1);
set.Add(2);
set.Add(3);
set.Add(10);
set.Delet(1);
set.Search(10);
Console.WriteLine("Count = {0}", set.Count);
Console.WriteLine("Items:");
foreach(var item in set)
Console.WriteLine(item);
}
}
}Решение задачи: «Объединение, пересечение и разность двух множеств»
textual
Листинг программы
static void Show(SortedSet<char> ss, string s)
{
Console.WriteLine(s);
foreach (char ch in ss)
Console.Write(ch + " ");
Console.WriteLine("\n");
}
static void Main(string[] args)
{
SortedSet<char> m1 = new SortedSet<char>();
SortedSet<char> m2 = new SortedSet<char>();
m1.Add('A');
m1.Add('B');
m1.Add('C');
m1.Add('Z');
m2.Add('X');
m2.Add('Y');
m2.Add('Z');
m1.SymmetricExceptWith(m2);
Show(m1,"пересечение");
m1.UnionWith(m2);
Show(m1, "объединение");
m1.ExceptWith(m2);
Show(m1, "вычитание");
Console.ReadKey();
}
}