Advertisement
Margoshinka

мур

Mar 2nd, 2022
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.28 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.             bitset = new int[maxi + 1];
  185.         }
  186.         public static BitSet operator +(BitSet a, BitSet b)
  187.         {
  188.            
  189.             if (a.maxi > b.maxi)
  190.             {
  191.                 BitSet c = new BitSet(a.maxi);
  192.                 for (int i = 0; i <= a.maxi; i++)
  193.                 {
  194.                     if (i <= b.maxi)
  195.                         c.bitset[i] = a.bitset[i] | b.bitset[i];
  196.                     else c.bitset[i] = a.bitset[i];
  197.                 }
  198.                 return c;
  199.             }
  200.             else
  201.             {
  202.                 BitSet c = new BitSet(b.maxi);
  203.                 for (int i = 0; i <= b.maxi; i++)
  204.                 {
  205.                     if (i <= a.maxi)
  206.                         c.bitset[i] = a.bitset[i] | b.bitset[i];
  207.                     else c.bitset[i] = b.bitset[i];
  208.                 }
  209.                 return c;
  210.             }
  211.  
  212.                
  213.         }
  214.         public static BitSet operator *(BitSet a, BitSet b)
  215.         {
  216.             int maxi;
  217.             if (a.maxi > b.maxi) maxi = b.maxi;
  218.             else maxi = a.maxi;
  219.             BitSet c = new BitSet(maxi);
  220.             for (int i = 0; i <= maxi; i++)
  221.             {
  222.                
  223.                     c.bitset[i] = a.bitset[i]&b.bitset[i];
  224.             }
  225.  
  226.             return c;
  227.         }
  228.  
  229.     }
  230.     class MultiSet : Set
  231.     {
  232.         int maxi;
  233.         int[] multiset;
  234.         public override int Maxi
  235.         {
  236.             get { return maxi; }
  237.             set { maxi = value; }
  238.  
  239.         }
  240.         public override void AddItem(ref int item)
  241.         {
  242.             if (item > maxi) throw new GoingBeyondTheSetException();
  243.             multiset[item]++;
  244.         }
  245.         public override void DeleteItem(ref int item)
  246.         { if (multiset[item]!=0) multiset[item]--;
  247.  
  248.         }
  249.         public override bool CheckItem(ref int item)
  250.         {
  251.             return (multiset[item] > 0);
  252.         }
  253.         public MultiSet(int maxi)
  254.         {
  255.             Maxi = maxi;
  256.             multiset = new int[maxi + 1];
  257.         }
  258.  
  259.     }
  260.    
  261.     class Program
  262.     {
  263.         static void Read_items(string name, List<int> l)
  264.         {
  265.  
  266.            
  267.            
  268.                 using (StreamReader f = new StreamReader(name))
  269.                 {
  270.  
  271.  
  272.                     string s;
  273.                    
  274.                     while ((s = f.ReadLine()) != null )
  275.                     {
  276.                         l.Add(int.Parse(s));
  277.  
  278.                    
  279.  
  280.  
  281.  
  282.                     }
  283.                
  284.  
  285.                 }
  286.            
  287.            
  288.         }
  289.         static void NewSet(Set s)
  290.         {
  291.            
  292.             int a = 2;
  293.             s.AddItem(ref a);
  294.             a = 5;
  295.             s.AddItem(ref a);
  296.             a = 10;
  297.             s.AddItem(ref a);
  298.             s.Show();
  299.  
  300.         }
  301.         static void Test()
  302.         {
  303.             Console.WriteLine("Введите первое множество");
  304.             string buf1 = Console.ReadLine();
  305.            
  306.             Console.WriteLine("Введите второе множество");
  307.             string buf2 = Console.ReadLine();
  308.             SimpleSet A = new SimpleSet(50);
  309.             A.Fill(buf1);
  310.             SimpleSet B = new SimpleSet(50);
  311.             B.Fill(buf2);
  312.             SimpleSet C = new SimpleSet(50);
  313.             C = A * B;
  314.             SimpleSet D = new SimpleSet(50);
  315.             D = A + B;
  316.             C.Show();
  317.             D.Show();
  318.             BitSet A_ = new BitSet(50);
  319.             A_.Fill(buf1);
  320.             BitSet B_ = new BitSet(50);
  321.             B_.Fill(buf2);
  322.             BitSet C_ = new BitSet(50);
  323.             C_ = A_ * B_;
  324.             BitSet D_ = new BitSet(50);
  325.             D_ = A_ + B_;
  326.             C_.Show();
  327.             D_.Show();
  328.         }
  329.         static void Main(string[] args)
  330.         {
  331.             var l = new List<int>();
  332.             Set set=new SimpleSet(1);
  333.             Console.WriteLine("Выберете тип множества:\n1-логический массив\n2-битовый массив\n3-мультимножество");
  334.             string buf = Console.ReadLine();
  335.             int x = Int32.Parse(buf);
  336.             Console.WriteLine("Введите строку с элементами, либо имя файла, откуда будут считаны элементы");
  337.             buf = Console.ReadLine();
  338.             Regex regex = new Regex(@"\w*.dat$");
  339.             MatchCollection matches = regex.Matches(buf);
  340.             int[] mas;
  341.            
  342.            
  343.            
  344.                 switch (x)
  345.             {
  346.                 case 1:
  347.                    
  348.                         set = new SimpleSet(100);
  349.                         break;
  350.                    
  351.                 case 2:
  352.                    
  353.                         set = new BitSet(100);
  354.                         break;
  355.                 case 3:
  356.                        set = new MultiSet(100);
  357.                        break;
  358.                    
  359.             }
  360.             try
  361.             {
  362.                 if (matches.Count > 0)
  363.                 {
  364.                     Read_items(buf, l);
  365.                     mas = new int[l.Count];
  366.                     mas = l.ToArray();
  367.                     set.Fill(mas);
  368.                 }
  369.                 else { set.Fill(buf); }
  370.             }
  371.             catch (GoingBeyondTheSetException e)
  372.             {
  373.                
  374.                 Console.WriteLine(e.Message);
  375.             }
  376.  
  377.             while (x != 4)
  378.             {
  379.                 set.Show();
  380.                 Console.WriteLine("\n1-добавить в множество элемент\n2-исключить элемент из множества\n3-проверить наличие элемента\n4-выход");
  381.                 buf = Console.ReadLine();
  382.                 x = Int32.Parse(buf);
  383.                 int item;
  384.                 switch (x)
  385.                 {
  386.                     case 1:
  387.                         try
  388.                         {
  389.                             Console.WriteLine("Введите элемент");
  390.                             buf = Console.ReadLine();
  391.                             item = Int32.Parse(buf);
  392.                             set.AddItem(ref item);
  393.                         }
  394.                         catch (GoingBeyondTheSetException e)
  395.                         {
  396.                          
  397.                             Console.WriteLine(e.Message);
  398.                         }
  399.  
  400.                         break;
  401.  
  402.                     case 2:
  403.                         Console.WriteLine("Введите элемент");
  404.                         buf = Console.ReadLine();
  405.                         item = Int32.Parse(buf);
  406.                         set.DeleteItem(ref item);
  407.                        
  408.                         break;
  409.                     case 3:
  410.                         Console.WriteLine("Введите элемент");
  411.                         buf = Console.ReadLine();
  412.                         item = Int32.Parse(buf);
  413.                         if (set.CheckItem(ref item)) Console.WriteLine("Элемент присутствует");
  414.                         else Console.WriteLine("Элемент отсутствует");
  415.                        
  416.                         break;
  417.  
  418.                 }
  419.             }
  420.             Test();
  421.         }
  422.     }
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement