Advertisement
Gillito

Untitled

Jun 17th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 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);
  21.             dynArray.AddToArray(5);
  22.             Console.WriteLine(dynArray.CurrentCount);
  23.             Console.WriteLine(dynArray.CurrentLength);
  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.  
  71.         public void AddToArray(int element)
  72.         {
  73.             if (count < array.Length)
  74.                 array[count] = element;
  75.             else
  76.             {
  77.                 int[] tempArray = new int[array.Length * 2];
  78.                 for (int i = 0; i < array.Length; i++)
  79.                     tempArray[i] = array[i];
  80.                 tempArray[count] = element;
  81.                 array = tempArray;
  82.             }
  83.             count++;
  84.         }
  85.  
  86.         public void ShowArray()
  87.         {
  88.             for (int i = 0; i < count; i++)
  89.                 Console.WriteLine(array[i]);
  90.             Console.WriteLine("array.Length " + array.Length);
  91.             Console.WriteLine("Count " + count);
  92.         }
  93.  
  94.         public int[] PublicArray()
  95.         {
  96.             int[] arrayCopy = new int[count];
  97.             for (int i = 0; i < count; i++)
  98.             {
  99.                 arrayCopy[i] = array[i];
  100.             }
  101.             return arrayCopy;
  102.         }
  103.  
  104.         public void ClearArray()
  105.         {
  106.             array = new int[1];
  107.             count = 0;
  108.         }
  109.  
  110.         public void Destroyer(int x)
  111.         {
  112.             for (; x + 1 < count; x++)
  113.                 array[x] = array[x + 1];
  114.             count--;
  115.         }
  116.  
  117.         public void Raverse()
  118.         {
  119.             for (int i = 0; i < count / 2; i++)
  120.             {
  121.                 int x = array[i];
  122.                 array[i] = array[count - 1 - i];
  123.                 array[count - 1 - i] = x;
  124.             }
  125.         }
  126.  
  127.         public void RemoveBefore(int x)
  128.         {
  129.             for (int i = 0; i < count - x; i++)
  130.                 array[i] = array[x + i];
  131.             count -= x;
  132.         }
  133.  
  134.         public void RemoveAfter(int x)
  135.         {
  136.             count = x;
  137.         }
  138.  
  139.         public void RemoveInterval(int startIndex, int endIndex)
  140.         {
  141.             int interval = endIndex - startIndex;
  142.  
  143.             for (int i = startIndex; i < count - startIndex; i++)
  144.                 array[i] = array[i + interval];
  145.             count -= startIndex;
  146.         }
  147.  
  148.  
  149.         public void AddIntsToElements(bool choise, int x)
  150.         {
  151.             if (choise == true)
  152.                 for (int i = 0; i < count; i++)
  153.                     array[i] = array[i] + 3;
  154.             else
  155.                 return;
  156.         }
  157.  
  158.         public void Compare(int x)
  159.         {
  160.             for (int i = 0; i < count; i++)
  161.             {
  162.                 if (x < array[i])
  163.                     array[i] -= 5;
  164.                 else if (x > array[i])
  165.                     array[i] += 5;
  166.             }
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement