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 GoingBeyondTheSetException : Exception
- {
- public GoingBeyondTheSetException()
- {
- Console.WriteLine("Выход за пределы множества");
- }
- }
- abstract class Set
- { public abstract int Maxi { get; set; }
- public abstract void AddItem(ref int item);
- public abstract void DeleteItem(ref int item);
- public abstract bool CheckItem(ref int item);
- public void Fill(string s)
- {
- int[] a = s.Split(' ').Select(x => int.Parse(x)).ToArray();
- for (int i = 0; i < a.Length; i++)
- { if (a[i] > Maxi) throw new GoingBeyondTheSetException();
- AddItem(ref a[i]);
- }
- }
- public void Fill(int [] a)
- {
- for (int i = 0; i < a.Length; i++)
- {
- if (a[i] > Maxi) throw new GoingBeyondTheSetException();
- AddItem(ref a[i]);
- }
- }
- public void Show()
- {
- for (int i =0 ; i <= Maxi; i++)
- {
- if (CheckItem(ref i)) Console.Write(i+" ");
- }
- Console.WriteLine();
- }
- }
- class SimpleSet :Set
- {
- int maxi;
- bool[] simpleset;
- public override int Maxi
- {
- get { return maxi; }
- set { maxi = value; }
- }
- public override void AddItem(ref int item)
- {
- if (item > maxi) throw new GoingBeyondTheSetException();
- simpleset[item] = true;
- }
- public override void DeleteItem(ref int item)
- {
- simpleset[item] = false;
- }
- public override bool CheckItem(ref int item)
- {
- return (simpleset[item]);
- }
- public SimpleSet(int maxi)
- {
- Maxi = maxi;
- simpleset = new bool[maxi+1];
- }
- public static SimpleSet operator +(SimpleSet a, SimpleSet b)
- {
- int maxi;
- if (a.maxi > b.maxi) maxi = a.maxi;
- else maxi = b.maxi;
- SimpleSet c = new SimpleSet(maxi);
- for (int i = 1; i <= a.maxi; i++)
- {
- if (a.simpleset[i] == true)
- c.simpleset[i] = true;
- }
- for (int i = 1; i <= b.maxi; i++)
- {
- if (b.simpleset[i] == true)
- c.simpleset[i] = true;
- }
- return c;
- }
- public static SimpleSet operator *(SimpleSet a, SimpleSet b)
- {
- int maxi;
- if (a.maxi > b.maxi) maxi = b.maxi;
- else maxi = a.maxi;
- SimpleSet c = new SimpleSet(maxi);
- for (int i = 1; i <= maxi; i++)
- {
- if (a.simpleset[i] == true && b.simpleset[i]==true)
- c.simpleset[i] = true;
- }
- return c;
- }
- }
- class BitSet : Set
- {
- int maxi;
- int[] bitset;
- int mask = 0;
- public override int Maxi
- {
- get { return maxi; }
- set { maxi = value; }
- }
- /* public string BitString(int idx)
- {
- string s = bitset[idx].ToString();
- string item ="";
- // var l = new List<int>();
- for (int i = 0; i < 32; i++)
- { if (i < s.Length)
- item += s[i];
- else item += "0";
- }
- item = new string(s.Reverse().ToArray());
- return item;
- }
- /* public void Offset(int idx)
- {
- for (int i = 31; i >= 0; i--)
- { if (i == idx)
- mask += "1";
- else mask += "0";
- }
- // mask = new string(mask.Reverse().ToArray());
- }*/
- public override void AddItem(ref int item)
- {
- if (item > maxi) throw new GoingBeyondTheSetException();
- int idx = item / 32;
- int n = item % 32;
- mask =1 << n;
- bitset[idx] = (bitset[idx] | mask);
- mask = 0;
- }
- public override void DeleteItem(ref int item)
- {
- int idx = item / 32;
- int n = item % 32;
- mask = 1 << n;
- mask = ~mask;
- bitset[idx] = bitset[idx] & mask;
- }
- public override bool CheckItem(ref int item)
- {
- int idx = item / 32;
- int n = item % 32;
- mask = 1 << n;
- return ((bitset[idx] & mask) != 0);
- }
- public BitSet(int maxi)
- {
- Maxi = maxi;
- int n = maxi / 32;
- bitset = new int[n+1];
- }
- public static BitSet operator +(BitSet a, BitSet b)
- {
- if (a.maxi > b.maxi)
- {
- BitSet c = new BitSet(a.maxi);
- for (int i = 0; i <= a.maxi; i++)
- {
- if (i <= b.maxi)
- c.bitset[i] = a.bitset[i] | b.bitset[i];
- else c.bitset[i] = a.bitset[i];
- }
- return c;
- }
- else
- {
- BitSet c = new BitSet(b.maxi);
- for (int i = 0; i <= b.maxi; i++)
- {
- if (i <= a.maxi)
- c.bitset[i] = a.bitset[i] | b.bitset[i];
- else c.bitset[i] = b.bitset[i];
- }
- return c;
- }
- }
- public static BitSet operator *(BitSet a, BitSet b)
- {
- int maxi;
- if (a.maxi > b.maxi) maxi = b.maxi;
- else maxi = a.maxi;
- BitSet c = new BitSet(maxi);
- for (int i = 0; i <= maxi; i++)
- {
- c.bitset[i] = a.bitset[i]&b.bitset[i];
- }
- return c;
- }
- }
- class MultiSet : Set
- {
- int maxi;
- int[] multiset;
- public override int Maxi
- {
- get { return maxi; }
- set { maxi = value; }
- }
- public override void AddItem(ref int item)
- {
- if (item > maxi) throw new GoingBeyondTheSetException();
- multiset[item]++;
- }
- public override void DeleteItem(ref int item)
- { if (multiset[item]!=0) multiset[item]--;
- }
- public override bool CheckItem(ref int item)
- {
- return (multiset[item] > 0);
- }
- public MultiSet(int maxi)
- {
- Maxi = maxi;
- multiset = new int[maxi + 1];
- }
- }
- class Program
- {
- static void Read_items(string name, List<int> l)
- {
- using (StreamReader f = new StreamReader(name))
- {
- string s;
- while ((s = f.ReadLine()) != null )
- {
- l.Add(int.Parse(s));
- }
- }
- }
- /* static void NewSet(Set s)
- {
- int a = 2;
- s.AddItem(ref a);
- a = 5;
- s.AddItem(ref a);
- a = 10;
- s.AddItem(ref a);
- s.Show();
- }*/
- 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)
- {
- var l = new List<int>();
- Set set=new SimpleSet(1);
- Console.WriteLine("Выберете тип множества:\n1-логический массив\n2-битовый массив\n3-мультимножество");
- string buf = Console.ReadLine();
- int x = Int32.Parse(buf);
- Console.WriteLine("Введите строку с элементами, либо имя файла, откуда будут считаны элементы");
- buf = Console.ReadLine();
- Regex regex = new Regex(@"\w*.dat$");
- MatchCollection matches = regex.Matches(buf);
- int[] mas;
- switch (x)
- {
- case 1:
- set = new SimpleSet(100);
- break;
- case 2:
- set = new BitSet(100);
- break;
- case 3:
- set = new MultiSet(100);
- break;
- }
- try
- {
- if (matches.Count > 0)
- {
- Read_items(buf, l);
- mas = new int[l.Count];
- mas = l.ToArray();
- set.Fill(mas);
- }
- else { set.Fill(buf); }
- }
- catch (GoingBeyondTheSetException e)
- {
- Console.WriteLine(e.Message);
- }
- while (x != 4)
- {
- set.Show();
- Console.WriteLine("\n1-добавить в множество элемент\n2-исключить элемент из множества\n3-проверить наличие элемента\n4-выход");
- buf = Console.ReadLine();
- x = Int32.Parse(buf);
- int item;
- switch (x)
- {
- case 1:
- try
- {
- Console.WriteLine("Введите элемент");
- buf = Console.ReadLine();
- item = Int32.Parse(buf);
- set.AddItem(ref item);
- }
- catch (GoingBeyondTheSetException e)
- {
- Console.WriteLine(e.Message);
- }
- break;
- case 2:
- Console.WriteLine("Введите элемент");
- buf = Console.ReadLine();
- item = Int32.Parse(buf);
- set.DeleteItem(ref item);
- break;
- case 3:
- Console.WriteLine("Введите элемент");
- buf = Console.ReadLine();
- item = Int32.Parse(buf);
- if (set.CheckItem(ref item)) Console.WriteLine("Элемент присутствует");
- else Console.WriteLine("Элемент отсутствует");
- break;
- }
- }
- Test();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement