Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Zbior
- {
- private Hashtable hashtable;
- public Zbior()
- {
- hashtable = new Hashtable();
- }
- public void Dodaj(object item)
- {
- hashtable.Add(item, 1);
- }
- public Zbior Union(Zbior union)
- {
- Zbior z = new Zbior();
- foreach (DictionaryEntry item in union.hashtable)
- {
- z.Dodaj(item.Key);
- }
- foreach (DictionaryEntry item in hashtable)
- {
- if (!z.hashtable.Contains(item.Key))
- {
- z.Dodaj(item.Key);
- }
- }
- return z;
- }
- public Zbior Intersection(Zbior zbior)
- {
- Zbior z1 = new Zbior();
- foreach (DictionaryEntry item in zbior.hashtable)
- {
- if (hashtable.Contains(item.Key))
- z1.Dodaj(item.Key);
- }
- return z1;
- }
- public Zbior Difference(Zbior zbior)
- {
- Zbior z2 = new Zbior();
- foreach (DictionaryEntry item in hashtable)
- {
- if (!zbior.hashtable.Contains(item.Key))
- z2.Dodaj(item.Key);
- }
- return z2;
- }
- public void Display()
- {
- foreach (DictionaryEntry item in hashtable)
- {
- Console.Write(item.Key+" ");
- }
- }
- }
- static void Main(string[] args)
- {
- Zbior z1 = new Zbior();
- Zbior z2 = new Zbior();
- int[] Tab1 = { 1, 3, 5, 7, 9 };
- int[] Tab2 = { 2, 4, 6, 8 };
- foreach (var item in Tab1)
- {
- z1.Dodaj(item);
- }
- foreach (var item in Tab2)
- {
- z2.Dodaj(item);
- }
- Console.Write("Zbiór A:");
- z1.Display();
- Console.Write("\nZbiór B:");
- z2.Display();
- Zbior z3 = z1.Union(z2);
- Console.Write("\nA + B: ");
- z3.Display();
- Zbior z4 = z3.Intersection(z1);
- Console.Write("\n(A+B)*A: ");
- z4.Display();
- Zbior z5 = z3.Difference(z1);
- Console.Write("\n(A+B)-A: ");
- z5.Display();
- Console.ReadKey();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement