Advertisement
Margoshinka

пока всё что есть

Feb 28th, 2022
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.40 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 = 00000000000000000000000000000001;
  117.         public override int Maxi
  118.         {
  119.             get { return maxi; }
  120.             set { maxi = value; }
  121.  
  122.         }
  123.         public override void AddItem(ref int a)
  124.         {
  125.  
  126.             int idx = a / 32;
  127.             mask = 1 << (a % 32);
  128.             Console.WriteLine(  mask);
  129.         }
  130.         public override void DeleteItem(ref int a)
  131.         {
  132.             return;
  133.  
  134.         }
  135.         public override bool CheckItem(ref int a)
  136.         {
  137.             return false;
  138.         }
  139.         public BitSet(int maxi)
  140.         {
  141.             Maxi = maxi;
  142.             bitset = new int[maxi + 1];
  143.         }
  144.        /* public static BitSet operator +(BitSet a, BitSet b)
  145.         {
  146.            int maxi;
  147.             if (a.maxi > b.maxi) maxi = a.maxi;
  148.             else maxi = b.maxi;
  149.             BitSet c = new BitSet(maxi);
  150.             for (int i = 1; i <= a.maxi; i++)
  151.             {
  152.                 if (a.bitset[i] == true)
  153.                     c.bitset[i] = true;
  154.             }
  155.             for (int i = 1; i <= b.maxi; i++)
  156.             {
  157.                 if (b.bitset[i] == true)
  158.                     c.bitset[i] = true;
  159.             }
  160.             return c;
  161.         }
  162.         public static BitSet operator *(BitSet a, BitSet b)
  163.         {
  164.             int maxi;
  165.             if (a.maxi > b.maxi) maxi = b.maxi;
  166.             else maxi = a.maxi;
  167.             SimpleSet c = new SimpleSet(maxi);
  168.             for (int i = 1; i <= maxi; i++)
  169.             {
  170.                 if (a.bitset[i] == true && b.bitset[i] == true)
  171.                     c.bitset[i] = true;
  172.             }
  173.  
  174.             return c;
  175.         }*/
  176.  
  177.     }
  178.     class MultiSet : Set
  179.     {
  180.         int maxi;
  181.         int[] multiset;
  182.         public override int Maxi
  183.         {
  184.             get { return maxi; }
  185.             set { maxi = value; }
  186.  
  187.         }
  188.         public override void AddItem(ref int item)
  189.         {
  190.             if (item > maxi) throw new GoingBeyondTheSetException();
  191.             multiset[item]++;
  192.         }
  193.         public override void DeleteItem(ref int item)
  194.         { if (multiset[item]!=0) multiset[item]--;
  195.  
  196.         }
  197.         public override bool CheckItem(ref int item)
  198.         {
  199.             return (multiset[item] > 0);
  200.         }
  201.         public MultiSet(int maxi)
  202.         {
  203.             Maxi = maxi;
  204.             multiset = new int[maxi + 1];
  205.         }
  206.  
  207.     }
  208.     class Program
  209.     {   static void NewSet(Set s)
  210.         {
  211.            
  212.             int a = 2;
  213.             s.AddItem(ref a);
  214.             a = 5;
  215.             s.AddItem(ref a);
  216.             a = 10;
  217.             s.AddItem(ref a);
  218.             s.Show();
  219.  
  220.         }
  221.        /* static void Test()
  222.         {
  223.             Console.WriteLine("Введите первое множество");
  224.             buf = Console.ReadLine();
  225.             x = double.Parse(buf);
  226.             ok = true;
  227.         }*/
  228.         static void Main(string[] args)
  229.         {
  230.             SimpleSet s = new SimpleSet(10);
  231.             NewSet(s);
  232.             BitSet b = new BitSet(10);
  233.             int a = 5;
  234.             b.AddItem(ref a);
  235.         }
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement