Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.IO;
- namespace SYATP2
- {
- class BeyondBordersException : Exception
- {
- public BeyondBordersException()
- {
- Console.WriteLine("Выход за пределы множества");
- }
- }
- abstract class Set
- {
- public int UpperItem;
- public abstract void AddItem(int item);
- public abstract void DeleteItem(int item);
- public abstract bool CheckItem(int item);
- public void validateItem(int item)
- {
- if (item > UpperItem) throw new BeyondBordersException();
- }
- public void Fill(string rowString)
- {
- int[] rowMassive = rowString.Split(' ').Select(x => int.Parse(x)).ToArray();
- Fill(rowMassive);
- }
- public void Fill(int[] rowMassive)
- {
- foreach (int rowItem in rowMassive) AddItem(rowItem);
- }
- public void Show()
- {
- for (int i = 0; i <= UpperItem; i++)
- if (CheckItem(i)) Console.Write(i + " ");
- Console.WriteLine();
- }
- }
- class SimpleSet : Set
- {
- bool[] setItems;
- public override void AddItem(int item)
- {
- validateItem(item);
- setItems[item] = true;
- }
- public override void DeleteItem(int item)
- {
- validateItem(item);
- setItems[item] = false;
- }
- public override bool CheckItem(int item)
- {
- validateItem(item);
- return (setItems[item]);
- }
- public SimpleSet(int max)
- {
- UpperItem = max;
- setItems = new bool[UpperItem + 1];
- }
- public static SimpleSet operator +(SimpleSet a, SimpleSet b)
- {
- SimpleSet c = new SimpleSet(Math.Max(a.UpperItem, b.UpperItem));
- for (int i = 1; i <= a.UpperItem; i++) c.setItems[i] = a.setItems[i];
- for (int i = 1; i <= b.UpperItem; i++) c.setItems[i] = b.setItems[i] || c.setItems[i];
- return c;
- }
- public static SimpleSet operator *(SimpleSet a, SimpleSet b)
- {
- SimpleSet c = new SimpleSet(Math.Max(a.UpperItem, b.UpperItem));
- for (int i = 1; i <= c.UpperItem; i++) c.setItems[i] = a.setItems[i] && b.setItems[i];
- return c;
- }
- }
- class BitSet : Set
- {
- int[] setItems;
- int mask = 0;
- public override void AddItem(int item)
- {
- validateItem(item);
- int idx = item / 32;
- int n = item % 32;
- mask = 1 << n;
- setItems[idx] = (setItems[idx] | mask);
- mask = 0;
- }
- public override void DeleteItem( int item)
- {
- validateItem(item);
- int idx = item / 32;
- int n = item % 32;
- mask = 1 << n;
- mask = ~mask;
- setItems[idx] = setItems[idx] & mask;
- }
- public override bool CheckItem(int item)
- {
- validateItem(item);
- int idx = item / 32;
- int n = item % 32;
- mask = 1 << n;
- return ((setItems[idx] & mask) != 0);
- }
- public BitSet(int max)
- {
- UpperItem = max;
- setItems = new int[(max / 32) + 1];
- }
- public static BitSet operator +(BitSet a, BitSet b)
- {
- BitSet c = new BitSet(Math.Max(a.UpperItem, b.UpperItem));
- for (int i = 0; i < c.setItems.Length; i++)
- if(i > a.setItems.Length) c.setItems[i] = b.setItems[i];
- else
- if (i > b.setItems.Length) c.setItems[i] = b.setItems[i];
- else
- c.setItems[i] = a.setItems[i] | b.setItems[i];
- return c;
- }
- public static BitSet operator *(BitSet a, BitSet b)
- {
- BitSet c = new BitSet(Math.Min(a.UpperItem, b.UpperItem) );
- for (int i = 0; i < c.setItems.Length; i++) c.setItems[i] = a.setItems[i] & b.setItems[i];
- return c;
- }
- }
- class MultiSet : Set
- {
- int[] setItems;
- public override void AddItem(int item)
- {
- validateItem(item);
- setItems[item]++;
- }
- public override void DeleteItem(int item)
- {
- validateItem(item);
- if (setItems[item] != 0) setItems[item]--;
- }
- public override bool CheckItem(int item)
- {
- validateItem(item);
- return (setItems[item] > 0);
- }
- public MultiSet(int max)
- {
- UpperItem = max;
- setItems = new int[UpperItem + 1];
- }
- }
- class Program
- {
- static void Test()
- {
- Console.WriteLine("Введите первое множество");
- string buf1 = Console.ReadLine();
- Console.WriteLine("Введите второе множество");
- string buf2 = Console.ReadLine();
- SimpleSet A = new SimpleSet(50);
- A.Fill(buf1);
- SimpleSet B = new SimpleSet(50);
- B.Fill(buf2);
- SimpleSet C = new SimpleSet(50);
- C = A * B;
- SimpleSet D = new SimpleSet(50);
- D = A + B;
- C.Show();
- D.Show();
- BitSet A_ = new BitSet(50);
- A_.Fill(buf1);
- BitSet B_ = new BitSet(50);
- B_.Fill(buf2);
- BitSet C_ = new BitSet(50);
- C_ = A_ * B_;
- BitSet D_ = new BitSet(50);
- D_ = A_ + B_;
- C_.Show();
- D_.Show();
- }
- static void Main(string[] args)
- {
- Set set = new SimpleSet(1);
- Console.WriteLine("Выберете тип множества:\n1-логический массив\n2-битовый массив\n3-мультимножество");
- string buf = Console.ReadLine();
- int typeSet = Int32.Parse(buf);
- switch (typeSet)
- {
- case 1:set = new SimpleSet(100);break;
- case 2:set = new BitSet(100);break;
- case 3:set = new MultiSet(100);break;
- }
- while (true)
- {
- try
- {
- Console.WriteLine("Введите строку с элементами");
- string rowItemsStr = Console.ReadLine();
- set.Fill(rowItemsStr);
- break;
- }
- catch (BeyondBordersException e)
- {
- Console.WriteLine(e.Message);
- }
- }
- int typeOperation = 0;
- while (typeOperation != 4)
- {
- try
- {
- set.Show();
- Console.WriteLine("\n1-добавить в множество элемент\n2-исключить элемент из множества\n3-проверить наличие элемента\n4-выход");
- buf = Console.ReadLine();
- typeOperation = Int32.Parse(buf);
- int item;
- switch (typeOperation)
- {
- case 1:
- Console.WriteLine("Введите элемент");
- buf = Console.ReadLine();
- item = Int32.Parse(buf);
- set.AddItem(item);
- break;
- case 2:
- Console.WriteLine("Введите элемент");
- buf = Console.ReadLine();
- item = Int32.Parse(buf);
- set.DeleteItem(item);
- break;
- case 3:
- Console.WriteLine("Введите элемент");
- buf = Console.ReadLine();
- item = Int32.Parse(buf);
- if (set.CheckItem(item)) Console.WriteLine("Элемент присутствует");
- else Console.WriteLine("Элемент отсутствует");
- break;
- }
- }
- catch (BeyondBordersException e)
- {
- Console.WriteLine(e.Message);
- }
- }
- Test();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement