Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- 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)
- {
- //Console.WriteLine("Введите строку с элементами множества");
- //string buf = Console.ReadLine();
- 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 =1 ; i <= Maxi; i++)
- {
- if (CheckItem(ref i)) Console.Write(i+" ");
- }
- }
- }
- 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
- string mask = "";
- 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 a)
- {
- int idx = a / 32;
- string item = BitString(idx);
- int n = a % 32;
- Offset(n);
- int a1 = Convert.ToInt32(item, 2);
- int a2 = Convert.ToInt32(mask, 2);
- int res = (a1 | a2);
- string result = Convert.ToString(res, 2);
- while (result.Length != 32) result = "0" + result;
- Console.WriteLine(result);
- mask = "";
- }
- public override void DeleteItem(ref int a)
- {
- return;
- }
- public override bool CheckItem(ref int a)
- {
- return false;
- }
- public BitSet(int maxi)
- {
- Maxi = maxi;
- bitset = new int[maxi + 1];
- }
- /* public static BitSet operator +(BitSet a, BitSet b)
- {
- int maxi;
- if (a.maxi > b.maxi) maxi = a.maxi;
- else maxi = b.maxi;
- BitSet c = new BitSet(maxi);
- for (int i = 1; i <= a.maxi; i++)
- {
- if (a.bitset[i] == true)
- c.bitset[i] = true;
- }
- for (int i = 1; i <= b.maxi; i++)
- {
- if (b.bitset[i] == true)
- c.bitset[i] = true;
- }
- return c;
- }
- public static BitSet operator *(BitSet a, BitSet 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.bitset[i] == true && b.bitset[i] == true)
- c.bitset[i] = true;
- }
- 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 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("Введите первое множество");
- buf = Console.ReadLine();
- x = double.Parse(buf);
- ok = true;
- }*/
- static void Main(string[] args)
- {
- SimpleSet s = new SimpleSet(10);
- NewSet(s);
- BitSet b = new BitSet(10);
- int a = 5;
- b.AddItem(ref a);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement