Advertisement
Pearlfromsu

s6l1

Feb 19th, 2024
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.23 KB | None | 0 0
  1. using ClassLibrary1;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace C_sharp_olympic
  9. {
  10.     class Program
  11.     {
  12.         static void psh(OwnQueue<int> q, int val)
  13.         {
  14.             Console.WriteLine($"+++++ {val}");
  15.             q.Enqueue(val);
  16.         }
  17.         static void rmf(OwnQueue<int> q)
  18.         {
  19.             int val = q.Dequeue();
  20.             Console.WriteLine($"----- {val}");
  21.         }
  22.         static void Main(string[] args)
  23.         {
  24.             Random rnd = new Random();
  25.             List<int> ls = new List<int>();
  26.             for(int i = 0; i < 100000; i++)
  27.                 ls.Add(i+1);
  28.            
  29.             OwnQueue<int> q = new OwnQueue<int>();
  30.             for(int i = 0; i < ls.Count; i++)
  31.                 q.Enqueue(ls[i]);
  32.  
  33.             for(int i = 0; i < ls.Count/2; i++)
  34.                 q.Dequeue();
  35.             //Console.WriteLine(q._head);
  36.             //Console.WriteLine(q._tail);
  37.             //Console.WriteLine($"Cap {q._capacity}");
  38.  
  39.             for (int i = 0; i < ls.Count/2; i++)
  40.                 if(q.Contains(ls[i]))
  41.                     Console.WriteLine($"UVAGA FOUND {ls[i]}");
  42.            
  43.             for (int i = ls.Count / 2; i < ls.Count; i++)
  44.                 if (!q.Contains(ls[i]))
  45.                     Console.WriteLine($"UVAGA NOT FOUND {ls[i]}");
  46.  
  47.             Console.WriteLine("Done");
  48.             /*
  49.             var testList = new List<int>() { 1, 2, 3, 4, 5 };
  50.             for(int i = 0; i < testList.Count; i++)
  51.                 psh(q, testList[i]);
  52.             Console.WriteLine();
  53.             for (int i = 0; i < testList.Count; i++)
  54.                 rmf(q);
  55.             Console.WriteLine();
  56.             psh(q, 5);
  57.             psh(q, 6);
  58.             psh(q, 7);
  59.             psh(q, 8);
  60.             psh(q, 9);
  61.             rmf(q);
  62.             rmf(q);
  63.             rmf(q);
  64.             psh(q, 10);
  65.             rmf(q);
  66.             psh(q, 11);
  67.             psh(q, 12);
  68.             rmf(q);
  69.             psh(q, 13);
  70.             psh(q, 14);
  71.             psh(q, 15);
  72.             psh(q, 16);
  73.             psh(q, 17);
  74.             psh(q, 18);
  75.             psh(q, 19);
  76.             psh(q, 20);
  77.             psh(q, 21);
  78.             psh(q, 22);
  79.             psh(q, 23);
  80.             psh(q, 24);
  81.             psh(q, 25);
  82.             psh(q, 26);
  83.             psh(q, 27);
  84.             psh(q, 28);
  85.             psh(q, 29);
  86.             rmf(q);
  87.             rmf(q);
  88.             psh(q, 30);
  89.             while (q.Count > 0)
  90.                 rmf(q);
  91.             */
  92.             Console.ReadKey();
  93.         }
  94.     }
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. using System;
  115. using System.Collections.Generic;
  116. using System.Linq;
  117. using System.Text;
  118. using System.Threading.Tasks;
  119.  
  120. namespace ClassLibrary1
  121. {
  122.     public class OwnQueue<T>
  123.     {
  124.         private T[] mass = new T[4];
  125.         private int _count = 0, _capacity = 4, _head = 0, _tail = 0;
  126.         public OwnQueue()
  127.         {
  128.             _count = 0;
  129.             _capacity = 4;
  130.         }
  131.         public OwnQueue(int capacity)
  132.         {
  133.             Count = 0;
  134.             setCapacity(capacity);
  135.         }
  136.         public int Count
  137.         {
  138.             get
  139.             {
  140.                 return _count;
  141.             }
  142.             private set
  143.             {
  144.                
  145.                 if (value > _capacity)
  146.                 {
  147.                     int newCapacity = _capacity;
  148.                     while (newCapacity < value)
  149.                         newCapacity *= 2;
  150.                     setCapacity(newCapacity);
  151.                 }
  152.                 _count = value;
  153.             }
  154.         }
  155.         public T Peek()
  156.         {
  157.             if (Count == 0)
  158.                 throw new InvalidOperationException();
  159.             return mass[_head];
  160.         }
  161.         public bool Contains(T item)
  162.         {
  163.             if (_head <= _tail)
  164.                 for (int i = _head; i <= _tail; i++)
  165.                     if (mass[i].Equals(item))
  166.                         return true;
  167.             if (_head > _tail)
  168.             {
  169.                 for (int i = _head; i < mass.Length; i++)
  170.                     if (mass[i].Equals(item))
  171.                         return true;
  172.                 for (int i = 0; i <= _tail; i++)
  173.                     if (mass[i].Equals(item))
  174.                         return true;
  175.             }
  176.             return false;
  177.         }
  178.         private void setCapacity(int value)
  179.         {
  180.             if (value < 0)
  181.                 throw new ArgumentOutOfRangeException();
  182.             _capacity = value;
  183.             T[] newArr = new T[_capacity];
  184.             int j = 0;
  185.             if (_head <= _tail)
  186.                 for (int i = _head; j < newArr.Length && i <= _tail; i++)
  187.                     newArr[j++] = mass[i];
  188.             if (_head > _tail)
  189.             {
  190.                 for (int i = _head; j < newArr.Length && i < mass.Length; i++)
  191.                     newArr[j++] = mass[i];
  192.                 for (int i = 0; j < newArr.Length && i <= _tail; i++)
  193.                     newArr[j++] = mass[i];
  194.             }
  195.             mass = newArr;
  196.             _head = 0;
  197.             _tail = _count - 1;
  198.         }
  199.         public void Enqueue(T elem)
  200.         {
  201.             this.Count++;
  202.             if (this.Count > 1)
  203.             {
  204.                 if (_tail + 1 >= _capacity)
  205.                     _tail = 0;
  206.                 else
  207.                     _tail++;
  208.             }
  209.             mass[_tail] = elem;
  210.             //Console.WriteLine($" to {_tail} ({_capacity} and {_count}) ({_head} and {_tail})");
  211.         }
  212.         public T Dequeue()
  213.         {
  214.             if(this.Count <= 0)
  215.                 throw new InvalidOperationException();
  216.             this.Count--;
  217.             T firstElement = mass[_head];
  218.             //Console.WriteLine($" from {_head} ({_capacity} and {_count}) ({_head} and {_tail})");
  219.             if (this.Count != 0)
  220.             {
  221.                 if (_head + 1 >= _capacity)
  222.                     _head = 0;
  223.                 else
  224.                     _head++;
  225.             }
  226.  
  227.             return firstElement;
  228.         }
  229.  
  230.     }
  231. }
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. using Microsoft.VisualStudio.TestTools.UnitTesting;
  249. using System;
  250. using ClassLibrary1;
  251.  
  252. namespace UnitTestProject1
  253. {
  254.     [TestClass]
  255.     public class UnitTest1
  256.     {
  257.         [TestMethod]
  258.         public void ZeroCountAtCreation()
  259.         {
  260.             int expected = 0;
  261.             OwnQueue<int> queue = new OwnQueue<int>();
  262.             Assert.AreEqual(expected, queue.Count);
  263.         }
  264.  
  265.         [TestMethod]
  266.         [ExpectedException(typeof(ArgumentOutOfRangeException))]
  267.         public void OutOfRangeException()
  268.         {
  269.             OwnQueue<int> queue = new OwnQueue<int>(-5);
  270.         }
  271.  
  272.         [TestMethod]
  273.         public void CountGrowingAfterAdd()
  274.         {
  275.             int n = 100;
  276.             OwnQueue<int> queue = new OwnQueue<int>();
  277.             int expected = n;
  278.             for (int i = 0; i < n; i++)
  279.                 queue.Enqueue(i);
  280.             Assert.AreEqual(expected, queue.Count);
  281.         }
  282.  
  283.         [TestMethod]
  284.         public void CountFallingAfterRemove()
  285.         {
  286.             int n = 100;
  287.             OwnQueue<int> queue = new OwnQueue<int>();
  288.             int expected = n - n/2;
  289.             for (int i = 0; i < n; i++)
  290.                 queue.Enqueue(i);
  291.            
  292.             for (int i = 0; i < n/2; i++)
  293.                 queue.Dequeue();
  294.            
  295.             Assert.AreEqual(expected, queue.Count);
  296.         }
  297.  
  298.         [TestMethod]
  299.         [ExpectedException(typeof(InvalidOperationException))]
  300.         public void EmptyDequeueException()
  301.         {
  302.             OwnQueue<int> queue = new OwnQueue<int>();
  303.             queue.Dequeue();
  304.         }
  305.  
  306.         [TestMethod]
  307.         [ExpectedException(typeof(InvalidOperationException))]
  308.         public void EmptyPeekException()
  309.         {
  310.             OwnQueue<int> items = new OwnQueue<int>();
  311.             items.Peek();
  312.         }
  313.  
  314.         [TestMethod]
  315.         public void PeekTest()
  316.         {
  317.             int n = 100;
  318.             OwnQueue<int> items = new OwnQueue<int>();
  319.             int expected = n/2+1;
  320.             for (int i = 0; i < n; i++)
  321.                 items.Enqueue(i+1);
  322.             for (int i = 0; i < n/2; i++)
  323.                 items.Dequeue();
  324.             Assert.AreEqual(expected, items.Peek());
  325.         }
  326.         [TestMethod]
  327.         public void ContainsTest()
  328.         {
  329.             int n = 100;
  330.             OwnQueue<int> items = new OwnQueue<int>();
  331.             for (int i = 0; i < n; i++)
  332.                 items.Enqueue(i);
  333.             for (int i = 0; i < n/2; i++)
  334.                 items.Dequeue();
  335.             for (int i = 0; i < n/2; i++)
  336.                 Assert.IsFalse(items.Contains(i));
  337.             for (int i = n-n/2; i < n; i++)
  338.                 Assert.IsTrue(items.Contains(i));
  339.         }
  340.  
  341.         [TestMethod]
  342.         public void DequeueTest()
  343.         {
  344.             int n = 100;
  345.             OwnQueue<int> items = new OwnQueue<int>();
  346.             for (int i = 0; i < n; i++)
  347.                 items.Enqueue(i);
  348.             for (int i = 0; i < n; i++)
  349.                 Assert.AreEqual(i, items.Dequeue());
  350.         }
  351.     }
  352. }
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement