Advertisement
Margoshinka

множества

Mar 7th, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.IO;
  6.  
  7. namespace SYATP2
  8. {   class GoingBeyondTheSetException : Exception
  9.     {
  10.         public GoingBeyondTheSetException()
  11.         {
  12.             Console.WriteLine("Выход за пределы множества");
  13.         }
  14.     }
  15.  
  16.     abstract class Set
  17.     {   public abstract int Maxi { get; set; }
  18.  
  19.         public abstract void AddItem(ref int item);
  20.         public abstract void DeleteItem(ref int item);
  21.         public abstract bool CheckItem(ref int item);
  22.         public void Fill(string s)
  23.         {
  24.            
  25.             int[] a = s.Split(' ').Select(x => int.Parse(x)).ToArray();
  26.             for (int i = 0; i < a.Length; i++)
  27.             {  if (a[i] > Maxi) throw new GoingBeyondTheSetException();
  28.                 AddItem(ref a[i]);
  29.             }
  30.         }
  31.  
  32.         public  void Fill(int [] a)
  33.         {
  34.             for (int i = 0; i < a.Length; i++)
  35.             {
  36.                 if (a[i] > Maxi) throw new GoingBeyondTheSetException();
  37.                 AddItem(ref a[i]);
  38.             }
  39.  
  40.         }
  41.         public void Show()
  42.         {
  43.             for (int i =0 ; i <= Maxi; i++)
  44.             {
  45.                 if (CheckItem(ref i)) Console.Write(i+" ");
  46.  
  47.             }
  48.             Console.WriteLine();
  49.         }
  50.  
  51.     }
  52.     class SimpleSet :Set
  53.     {
  54.         int maxi;
  55.         bool[] simpleset;
  56.         public override int Maxi
  57.         {
  58.             get { return maxi; }
  59.             set { maxi = value; }
  60.  
  61.         }
  62.         public override void AddItem(ref int item)
  63.         {
  64.             if (item > maxi) throw new GoingBeyondTheSetException();
  65.             simpleset[item] = true;
  66.         }
  67.         public override void DeleteItem(ref int item)
  68.         {
  69.             simpleset[item] = false;
  70.         }
  71.         public override bool CheckItem(ref int item)
  72.         {
  73.             return (simpleset[item]);
  74.         }
  75.         public SimpleSet(int maxi)
  76.         {
  77.             Maxi = maxi;
  78.             simpleset = new bool[maxi+1];
  79.         }
  80.         public static SimpleSet operator +(SimpleSet a, SimpleSet b)
  81.         {
  82.             int maxi;
  83.             if (a.maxi > b.maxi) maxi = a.maxi;
  84.             else maxi = b.maxi;
  85.             SimpleSet c = new SimpleSet(maxi);
  86.             for (int i = 1; i <= a.maxi; i++)
  87.             {
  88.                 if (a.simpleset[i] == true)
  89.                     c.simpleset[i] = true;
  90.             }
  91.             for (int i = 1; i <= b.maxi; i++)
  92.             {
  93.                 if (b.simpleset[i] == true)
  94.                     c.simpleset[i] = true;
  95.             }
  96.             return c;
  97.         }
  98.         public static SimpleSet operator *(SimpleSet a, SimpleSet b)
  99.         {
  100.             int maxi;
  101.             if (a.maxi > b.maxi) maxi = b.maxi;
  102.             else maxi = a.maxi;
  103.             SimpleSet c = new SimpleSet(maxi);
  104.             for (int i = 1; i <= maxi; i++)
  105.             {
  106.                 if (a.simpleset[i] == true && b.simpleset[i]==true)
  107.                     c.simpleset[i] = true;
  108.             }
  109.            
  110.             return c;
  111.         }
  112.     }
  113.    class BitSet : Set
  114.     {
  115.         int maxi;
  116.         int[] bitset;
  117.        
  118.         int mask = 0;
  119.         public override int Maxi
  120.         {
  121.             get { return maxi; }
  122.             set { maxi = value; }
  123.  
  124.         }
  125.        /* public string BitString(int idx)
  126.         {
  127.             string s = bitset[idx].ToString();
  128.             string item ="";
  129.            // var l = new List<int>();
  130.             for (int i = 0; i < 32; i++)
  131.             {    if (i < s.Length)
  132.                     item += s[i];
  133.                 else item += "0";
  134.                
  135.             }
  136.             item = new string(s.Reverse().ToArray());
  137.  
  138.             return item;
  139.         }
  140.       /*  public void Offset(int idx)
  141.         {
  142.             for (int i = 31; i >= 0; i--)
  143.             {   if (i == idx)
  144.                     mask += "1";
  145.                 else mask += "0";
  146.             }
  147.            // mask = new string(mask.Reverse().ToArray());
  148.         }*/
  149.         public override void AddItem(ref int item)
  150.         {
  151.             if (item > maxi) throw new GoingBeyondTheSetException();
  152.             int idx = item / 32;
  153.          
  154.             int n = item % 32;
  155.          
  156.             mask =1 << n;
  157.          
  158.             bitset[idx] = (bitset[idx] | mask);
  159.            
  160.            
  161.             mask = 0;
  162.  
  163.         }
  164.         public override void DeleteItem(ref int item)
  165.         {
  166.             int idx = item / 32;
  167.             int n = item % 32;
  168.             mask = 1 << n;
  169.             mask = ~mask;
  170.             bitset[idx] = bitset[idx] & mask;
  171.  
  172.         }
  173.         public override bool CheckItem(ref int item)
  174.         {
  175.             int idx = item / 32;
  176.             int n = item % 32;
  177.             mask = 1 << n;
  178.             return ((bitset[idx] & mask) != 0);
  179.  
  180.         }
  181.         public BitSet(int maxi)
  182.         {
  183.             Maxi = maxi;
  184.             int n = maxi / 32;
  185.             bitset = new int[n+1];
  186.         }
  187.         public static BitSet operator +(BitSet a, BitSet b)
  188.         {
  189.            
  190.             if (a.maxi > b.maxi)
  191.             {
  192.                 BitSet c = new BitSet(a.maxi);
  193.                 for (int i = 0; i <= a.maxi; i++)
  194.                 {
  195.                     if (i <= b.maxi)
  196.                         c.bitset[i] = a.bitset[i] | b.bitset[i];
  197.                     else c.bitset[i] = a.bitset[i];
  198.                 }
  199.                 return c;
  200.             }
  201.             else
  202.             {
  203.                 BitSet c = new BitSet(b.maxi);
  204.                 for (int i = 0; i <= b.maxi; i++)
  205.                 {
  206.                     if (i <= a.maxi)
  207.                         c.bitset[i] = a.bitset[i] | b.bitset[i];
  208.                     else c.bitset[i] = b.bitset[i];
  209.                 }
  210.                 return c;
  211.             }
  212.  
  213.                
  214.         }
  215.         public static BitSet operator *(BitSet a, BitSet b)
  216.         {
  217.             int maxi;
  218.             if (a.maxi > b.maxi) maxi = b.maxi;
  219.             else maxi = a.maxi;
  220.             BitSet c = new BitSet(maxi);
  221.             for (int i = 0; i <= maxi; i++)
  222.             {
  223.                
  224.                     c.bitset[i] = a.bitset[i]&b.bitset[i];
  225.             }
  226.  
  227.             return c;
  228.         }
  229.  
  230.     }
  231.     class MultiSet : Set
  232.     {
  233.         int maxi;
  234.         int[] multiset;
  235.         public override int Maxi
  236.         {
  237.             get { return maxi; }
  238.             set { maxi = value; }
  239.  
  240.         }
  241.         public override void AddItem(ref int item)
  242.         {
  243.             if (item > maxi) throw new GoingBeyondTheSetException();
  244.             multiset[item]++;
  245.         }
  246.         public override void DeleteItem(ref int item)
  247.         { if (multiset[item]!=0) multiset[item]--;
  248.  
  249.         }
  250.         public override bool CheckItem(ref int item)
  251.         {
  252.             return (multiset[item] > 0);
  253.         }
  254.         public MultiSet(int maxi)
  255.         {
  256.             Maxi = maxi;
  257.             multiset = new int[maxi + 1];
  258.         }
  259.  
  260.     }
  261.    
  262.     class Program
  263.     {
  264.         static void Read_items(string name, List<int> l)
  265.         {
  266.  
  267.            
  268.            
  269.                 using (StreamReader f = new StreamReader(name))
  270.                 {
  271.  
  272.  
  273.                     string s;
  274.                    
  275.                     while ((s = f.ReadLine()) != null )
  276.                     {
  277.                         l.Add(int.Parse(s));
  278.  
  279.                    
  280.  
  281.  
  282.  
  283.                     }
  284.                
  285.  
  286.                 }
  287.            
  288.            
  289.         }
  290.        /* static void NewSet(Set s)
  291.         {
  292.            
  293.             int a = 2;
  294.             s.AddItem(ref a);
  295.             a = 5;
  296.             s.AddItem(ref a);
  297.             a = 10;
  298.             s.AddItem(ref a);
  299.             s.Show();
  300.  
  301.         }*/
  302.         static void Test()
  303.         {
  304.             Console.WriteLine("Введите первое множество");
  305.             string buf1 = Console.ReadLine();
  306.            
  307.             Console.WriteLine("Введите второе множество");
  308.             string buf2 = Console.ReadLine();
  309.             SimpleSet A = new SimpleSet(50);
  310.             A.Fill(buf1);
  311.             SimpleSet B = new SimpleSet(50);
  312.             B.Fill(buf2);
  313.             SimpleSet C = new SimpleSet(50);
  314.             C = A * B;
  315.             SimpleSet D = new SimpleSet(50);
  316.             D = A + B;
  317.             C.Show();
  318.             D.Show();
  319.             BitSet A_ = new BitSet(50);
  320.             A_.Fill(buf1);
  321.             BitSet B_ = new BitSet(50);
  322.             B_.Fill(buf2);
  323.             BitSet C_ = new BitSet(50);
  324.             C_ = A_ * B_;
  325.             BitSet D_ = new BitSet(50);
  326.             D_ = A_ + B_;
  327.             C_.Show();
  328.             D_.Show();
  329.         }
  330.         static void Main(string[] args)
  331.         {
  332.             var l = new List<int>();
  333.             Set set=new SimpleSet(1);
  334.             Console.WriteLine("Выберете тип множества:\n1-логический массив\n2-битовый массив\n3-мультимножество");
  335.             string buf = Console.ReadLine();
  336.             int x = Int32.Parse(buf);
  337.             Console.WriteLine("Введите строку с элементами, либо имя файла, откуда будут считаны элементы");
  338.             buf = Console.ReadLine();
  339.             Regex regex = new Regex(@"\w*.dat$");
  340.             MatchCollection matches = regex.Matches(buf);
  341.             int[] mas;
  342.            
  343.            
  344.            
  345.                 switch (x)
  346.             {
  347.                 case 1:
  348.                    
  349.                         set = new SimpleSet(100);
  350.                         break;
  351.                    
  352.                 case 2:
  353.                    
  354.                         set = new BitSet(100);
  355.                         break;
  356.                 case 3:
  357.                        set = new MultiSet(100);
  358.                        break;
  359.                    
  360.             }
  361.             try
  362.             {
  363.                 if (matches.Count > 0)
  364.                 {
  365.                     Read_items(buf, l);
  366.                     mas = new int[l.Count];
  367.                     mas = l.ToArray();
  368.                     set.Fill(mas);
  369.                 }
  370.                 else { set.Fill(buf); }
  371.             }
  372.             catch (GoingBeyondTheSetException e)
  373.             {
  374.                
  375.                 Console.WriteLine(e.Message);
  376.             }
  377.  
  378.             while (x != 4)
  379.             {
  380.                 set.Show();
  381.                 Console.WriteLine("\n1-добавить в множество элемент\n2-исключить элемент из множества\n3-проверить наличие элемента\n4-выход");
  382.                 buf = Console.ReadLine();
  383.                 x = Int32.Parse(buf);
  384.                 int item;
  385.                 switch (x)
  386.                 {
  387.                     case 1:
  388.                         try
  389.                         {
  390.                             Console.WriteLine("Введите элемент");
  391.                             buf = Console.ReadLine();
  392.                             item = Int32.Parse(buf);
  393.                             set.AddItem(ref item);
  394.                         }
  395.                         catch (GoingBeyondTheSetException e)
  396.                         {
  397.                          
  398.                             Console.WriteLine(e.Message);
  399.                         }
  400.  
  401.                         break;
  402.  
  403.                     case 2:
  404.                         Console.WriteLine("Введите элемент");
  405.                         buf = Console.ReadLine();
  406.                         item = Int32.Parse(buf);
  407.                         set.DeleteItem(ref item);
  408.                        
  409.                         break;
  410.                     case 3:
  411.                         Console.WriteLine("Введите элемент");
  412.                         buf = Console.ReadLine();
  413.                         item = Int32.Parse(buf);
  414.                         if (set.CheckItem(ref item)) Console.WriteLine("Элемент присутствует");
  415.                         else Console.WriteLine("Элемент отсутствует");
  416.                        
  417.                         break;
  418.  
  419.                 }
  420.             }
  421.             Test();
  422.         }
  423.     }
  424. }
  425.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement