Advertisement
Gillito

Untitled

Jun 17th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication44
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             DynamicArray dynArray = new DynamicArray();
  14.  
  15.             dynArray.AddToArray(1);
  16.             dynArray.AddToArray(2);
  17.             dynArray.AddToArray(3);
  18.             dynArray.AddToArray(4);
  19.             Console.WriteLine(dynArray.CurrentCount);
  20.             Console.WriteLine(dynArray.CurrentLength = 30);
  21.             dynArray.AddToArray(5);
  22.             Console.WriteLine(dynArray.CurrentCount);
  23.             Console.WriteLine(dynArray.CurrentLength = 100);
  24.             dynArray.AddToArray(6);
  25.             dynArray.AddToArray(7);
  26.             dynArray.AddToArray(8);
  27.  
  28.             //dynArray.ShowArray();
  29.  
  30.             Console.WriteLine(dynArray.CurrentCount);
  31.             Console.WriteLine(dynArray.CurrentLength);
  32.             dynArray.Compare(4);
  33.             //dynArray.ShowArray();
  34.  
  35.             //dynArray.AddIntsToElements(false, 2);
  36.             //dynArray.ShowArray();
  37.  
  38.             //dynArray.RemoveInterval(2, 4);
  39.             //dynArray.ShowArray();
  40.  
  41.             //dynArray.RemoveBefore(2);
  42.             //dynArray.ShowArray();
  43.  
  44.             //dynArray.RemoveAfter(4);
  45.             //dynArray.ShowArray();
  46.         }
  47.     }
  48.  
  49.     class DynamicArray
  50.     {
  51.         private int count = 0;
  52.  
  53.         private int[] array = new int[1];
  54.  
  55.         public int CurrentCount
  56.         {
  57.             get
  58.             {
  59.                 return count;
  60.             }
  61.         }
  62.  
  63.         public int CurrentLength
  64.         {
  65.             get
  66.             {
  67.                 return array.Length;
  68.             }
  69.  
  70.             set
  71.             {
  72.                 if (array.Length < value)
  73.                 {
  74.                     int[] tempArray = new int[value];
  75.                     for (int i = 0; i < array.Length; i++)
  76.                         tempArray[i] = array[i];
  77.                     array = tempArray;
  78.                 }
  79.             }
  80.         }
  81.  
  82.         public void AddToArray(int element)
  83.         {
  84.             if (count < array.Length)
  85.                 array[count] = element;
  86.             else
  87.             {
  88.                 int[] tempArray = new int[array.Length * 2];
  89.                 for (int i = 0; i < array.Length; i++)
  90.                     tempArray[i] = array[i];
  91.                 tempArray[count] = element;
  92.                 array = tempArray;
  93.             }
  94.             count++;
  95.         }
  96.  
  97.         public void ShowArray()
  98.         {
  99.             for (int i = 0; i < count; i++)
  100.                 Console.WriteLine(array[i]);
  101.             Console.WriteLine("array.Length " + array.Length);
  102.             Console.WriteLine("Count " + count);
  103.         }
  104.  
  105.         public int[] PublicArray()
  106.         {
  107.             int[] arrayCopy = new int[count];
  108.             for (int i = 0; i < count; i++)
  109.             {
  110.                 arrayCopy[i] = array[i];
  111.             }
  112.             return arrayCopy;
  113.         }
  114.  
  115.         public void ClearArray()
  116.         {
  117.             array = new int[1];
  118.             count = 0;
  119.         }
  120.  
  121.         public void Destroyer(int x)
  122.         {
  123.             for (; x + 1 < count; x++)
  124.                 array[x] = array[x + 1];
  125.             count--;
  126.         }
  127.  
  128.         public void Raverse()
  129.         {
  130.             for (int i = 0; i < count / 2; i++)
  131.             {
  132.                 int x = array[i];
  133.                 array[i] = array[count - 1 - i];
  134.                 array[count - 1 - i] = x;
  135.             }
  136.         }
  137.  
  138.         public void RemoveBefore(int x)
  139.         {
  140.             for (int i = 0; i < count - x; i++)
  141.                 array[i] = array[x + i];
  142.             count -= x;
  143.         }
  144.  
  145.         public void RemoveAfter(int x)
  146.         {
  147.             count = x;
  148.         }
  149.  
  150.         public void RemoveInterval(int startIndex, int endIndex)
  151.         {
  152.             int interval = endIndex - startIndex;
  153.  
  154.             for (int i = startIndex; i < count - startIndex; i++)
  155.                 array[i] = array[i + interval];
  156.             count -= startIndex;
  157.         }
  158.  
  159.  
  160.         public void AddIntsToElements(bool choise, int x)
  161.         {
  162.             if (choise == true)
  163.                 for (int i = 0; i < count; i++)
  164.                     array[i] = array[i] + 3;
  165.             else
  166.                 return;
  167.         }
  168.  
  169.         public void Compare(int x)
  170.         {
  171.             for (int i = 0; i < count; i++)
  172.             {
  173.                 if (x < array[i])
  174.                     array[i] -= 5;
  175.                 else if (x > array[i])
  176.                     array[i] += 5;
  177.             }
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement