Advertisement
Margoshinka

уже лучше

Feb 28th, 2022
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SYATP2
  6. {   class GoingBeyondTheSetException : Exception
  7.     {
  8.         public GoingBeyondTheSetException()
  9.         {
  10.             Console.WriteLine("Выход за пределы множества");
  11.         }
  12.     }
  13.  
  14.     abstract class Set
  15.     {   public abstract int Maxi { get; set; }
  16.  
  17.         public abstract void AddItem(ref int item);
  18.         public abstract void DeleteItem(ref int item);
  19.         public abstract bool CheckItem(ref int item);
  20.         public void Fill(string s)
  21.         {
  22.             //Console.WriteLine("Введите строку с элементами множества");
  23.             //string buf = Console.ReadLine();
  24.             int[] a = s.Split(' ').Select(x => int.Parse(x)).ToArray();
  25.             for (int i = 0; i < a.Length; i++)
  26.             {  if (a[i] > Maxi) throw new GoingBeyondTheSetException();
  27.                 AddItem(ref a[i]);
  28.             }
  29.         }
  30.  
  31.         public  void Fill(int [] a)
  32.         {
  33.             for (int i = 0; i < a.Length; i++)
  34.             {
  35.                 if (a[i] > Maxi) throw new GoingBeyondTheSetException();
  36.                 AddItem(ref a[i]);
  37.             }
  38.  
  39.         }
  40.         public void Show()
  41.         {
  42.             for (int i =1 ; i <= Maxi; i++)
  43.             {
  44.                 if (CheckItem(ref i)) Console.Write(i+" ");
  45.  
  46.             }
  47.  
  48.         }
  49.  
  50.     }
  51.     class SimpleSet :Set
  52.     {
  53.         int maxi;
  54.         bool[] simpleset;
  55.         public override int Maxi
  56.         {
  57.             get { return maxi; }
  58.             set { maxi = value; }
  59.  
  60.         }
  61.         public override void AddItem(ref int item)
  62.         {
  63.             if (item > maxi) throw new GoingBeyondTheSetException();
  64.             simpleset[item] = true;
  65.         }
  66.         public override void DeleteItem(ref int item)
  67.         {
  68.             simpleset[item] = false;
  69.         }
  70.         public override bool CheckItem(ref int item)
  71.         {
  72.             return (simpleset[item]);
  73.         }
  74.         public SimpleSet(int maxi)
  75.         {
  76.             Maxi = maxi;
  77.             simpleset = new bool[maxi+1];
  78.         }
  79.         public static SimpleSet operator +(SimpleSet a, SimpleSet b)
  80.         {
  81.             int maxi;
  82.             if (a.maxi > b.maxi) maxi = a.maxi;
  83.             else maxi = b.maxi;
  84.             SimpleSet c = new SimpleSet(maxi);
  85.             for (int i = 1; i <= a.maxi; i++)
  86.             {
  87.                 if (a.simpleset[i] == true)
  88.                     c.simpleset[i] = true;
  89.             }
  90.             for (int i = 1; i <= b.maxi; i++)
  91.             {
  92.                 if (b.simpleset[i] == true)
  93.                     c.simpleset[i] = true;
  94.             }
  95.             return c;
  96.         }
  97.         public static SimpleSet operator *(SimpleSet a, SimpleSet b)
  98.         {
  99.             int maxi;
  100.             if (a.maxi > b.maxi) maxi = b.maxi;
  101.             else maxi = a.maxi;
  102.             SimpleSet c = new SimpleSet(maxi);
  103.             for (int i = 1; i <= maxi; i++)
  104.             {
  105.                 if (a.simpleset[i] == true && b.simpleset[i]==true)
  106.                     c.simpleset[i] = true;
  107.             }
  108.            
  109.             return c;
  110.         }
  111.     }
  112.    class BitSet : Set
  113.     {
  114.         int maxi;
  115.         int[] bitset;
  116.       //  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 };
  117.         string mask = "";
  118.         public override int Maxi
  119.         {
  120.             get { return maxi; }
  121.             set { maxi = value; }
  122.  
  123.         }
  124.         public string BitString(int idx)
  125.         {
  126.             string s = bitset[idx].ToString();
  127.             string item ="";
  128.            // var l = new List<int>();
  129.             for (int i = 0; i < 32; i++)
  130.             {    if (i < s.Length)
  131.                     item += s[i];
  132.                 else item += "0";
  133.                
  134.             }
  135.             item = new string(s.Reverse().ToArray());
  136.  
  137.             return item;
  138.         }
  139.         public void Offset(int idx)
  140.         {
  141.             for (int i = 31; i >= 0; i--)
  142.             {   if (i == idx)
  143.                     mask += "1";
  144.                 else mask += "0";
  145.             }
  146.            // mask = new string(mask.Reverse().ToArray());
  147.         }
  148.         public override void AddItem(ref int a)
  149.         {
  150.  
  151.             int idx = a / 32;
  152.             string item = BitString(idx);
  153.             int n = a % 32;
  154.             Offset(n);
  155.             int a1 = Convert.ToInt32(item, 2);
  156.             int a2 =  Convert.ToInt32(mask, 2);
  157.             int res = (a1 | a2);
  158.             string result = Convert.ToString(res, 2);
  159.              while (result.Length != 32) result = "0" + result;
  160.             Console.WriteLine(result);
  161.            
  162.             mask = "";
  163.  
  164.         }
  165.         public override void DeleteItem(ref int a)
  166.         {
  167.             return;
  168.  
  169.         }
  170.         public override bool CheckItem(ref int a)
  171.         {
  172.             return false;
  173.         }
  174.         public BitSet(int maxi)
  175.         {
  176.             Maxi = maxi;
  177.             bitset = new int[maxi + 1];
  178.         }
  179.        /* public static BitSet operator +(BitSet a, BitSet b)
  180.         {
  181.            int maxi;
  182.             if (a.maxi > b.maxi) maxi = a.maxi;
  183.             else maxi = b.maxi;
  184.             BitSet c = new BitSet(maxi);
  185.             for (int i = 1; i <= a.maxi; i++)
  186.             {
  187.                 if (a.bitset[i] == true)
  188.                     c.bitset[i] = true;
  189.             }
  190.             for (int i = 1; i <= b.maxi; i++)
  191.             {
  192.                 if (b.bitset[i] == true)
  193.                     c.bitset[i] = true;
  194.             }
  195.             return c;
  196.         }
  197.         public static BitSet operator *(BitSet a, BitSet b)
  198.         {
  199.             int maxi;
  200.             if (a.maxi > b.maxi) maxi = b.maxi;
  201.             else maxi = a.maxi;
  202.             SimpleSet c = new SimpleSet(maxi);
  203.             for (int i = 1; i <= maxi; i++)
  204.             {
  205.                 if (a.bitset[i] == true && b.bitset[i] == true)
  206.                     c.bitset[i] = true;
  207.             }
  208.  
  209.             return c;
  210.         }*/
  211.  
  212.     }
  213.     class MultiSet : Set
  214.     {
  215.         int maxi;
  216.         int[] multiset;
  217.         public override int Maxi
  218.         {
  219.             get { return maxi; }
  220.             set { maxi = value; }
  221.  
  222.         }
  223.         public override void AddItem(ref int item)
  224.         {
  225.             if (item > maxi) throw new GoingBeyondTheSetException();
  226.             multiset[item]++;
  227.         }
  228.         public override void DeleteItem(ref int item)
  229.         { if (multiset[item]!=0) multiset[item]--;
  230.  
  231.         }
  232.         public override bool CheckItem(ref int item)
  233.         {
  234.             return (multiset[item] > 0);
  235.         }
  236.         public MultiSet(int maxi)
  237.         {
  238.             Maxi = maxi;
  239.             multiset = new int[maxi + 1];
  240.         }
  241.  
  242.     }
  243.     class Program
  244.     {   static void NewSet(Set s)
  245.         {
  246.            
  247.             int a = 2;
  248.             s.AddItem(ref a);
  249.             a = 5;
  250.             s.AddItem(ref a);
  251.             a = 10;
  252.             s.AddItem(ref a);
  253.             s.Show();
  254.  
  255.         }
  256.        /* static void Test()
  257.         {
  258.             Console.WriteLine("Введите первое множество");
  259.             buf = Console.ReadLine();
  260.             x = double.Parse(buf);
  261.             ok = true;
  262.         }*/
  263.         static void Main(string[] args)
  264.         {
  265.             SimpleSet s = new SimpleSet(10);
  266.             NewSet(s);
  267.             BitSet b = new BitSet(10);
  268.             int a = 5;
  269.             b.AddItem(ref a);
  270.         }
  271.     }
  272. }
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement