Advertisement
Guest User

Storage lab final fixed

a guest
Nov 12th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.92 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace Storage
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var st = new Storage<int>();
  12.             while (true)
  13.             {
  14.                 try
  15.                 {
  16.                     Console.WriteLine(
  17.                         "Menu:\n" +
  18.                         "1. Print all items\n" +
  19.                         "2. Insert item\n" +
  20.                         "3. Get current item\n" +
  21.                         "4. Move current item to next\n" +
  22.                         "5. Move current item to prev\n" +
  23.                         "6. Remove current item\n" +
  24.                         "7. Search item by index\n" +
  25.                         "8. Remove item by index");
  26.  
  27.                     Console.Write(">> ");
  28.                     char selected = Console.ReadKey().KeyChar;
  29.                     Console.WriteLine();
  30.                     switch (selected)
  31.                     {
  32.                         case '1':
  33.                             {
  34.                                 Console.WriteLine("Storage data:");
  35.                                 if (st.Count == 0) Console.WriteLine("*empty*");
  36.                                 else
  37.                                 {
  38.                                     int i = 0;
  39.                                     foreach (var item in st)
  40.                                     {
  41.                                         Console.WriteLine($"[{i}] {item}");
  42.                                         i++;
  43.                                     }
  44.                                 }
  45.                             };
  46.                             break;
  47.                         case '2':
  48.                             {
  49.                                 Console.Write("Insert value: ");
  50.                                 int value = Convert.ToInt32(Console.ReadLine());
  51.                                 int index = st.Add(value);
  52.                                 Console.WriteLine($"Success. Item inserted at index {index}.");
  53.                             };
  54.                             break;
  55.                         case '3':
  56.                             {
  57.                                 Console.WriteLine($"Current item index: {st.CurrentIndex}.\n" +
  58.                                     $"Current item value: {st.Current}.");
  59.                             };
  60.                             break;
  61.                         case '4':
  62.                             {
  63.                                 bool moved = st.MoveNext();
  64.                                 string status = moved ? "Success" : "Failed (last item)";
  65.                                 Console.WriteLine($"Move status: {status}.\n" +
  66.                                     $"Current item index: {st.CurrentIndex}.");
  67.                             };
  68.                             break;
  69.                         case '5':
  70.                             {
  71.                                 bool moved = st.MovePrev();
  72.                                 string status = moved ? "Success" : "Failed (first item)";
  73.                                 Console.WriteLine($"Move status: {status}.\n" +
  74.                                     $"Current item index: {st.CurrentIndex}.");
  75.                             };
  76.                             break;
  77.                         case '6':
  78.                             {
  79.                                 st.RemoveByIndex(st.CurrentIndex);
  80.                                 Console.WriteLine("Current item removed." +
  81.                                     $"New current item value: {st.Current}." +
  82.                                     $"New current item index: {st.CurrentIndex}.");
  83.                             };
  84.                             break;
  85.                         case '7':
  86.                             {
  87.                                 Console.Write("Enter item index to search: ");
  88.                                 int index = Convert.ToInt32(Console.ReadLine());
  89.                                 int value = st.GetByIndex(index);
  90.                                 Console.WriteLine($"Found. Item value: {value}.");
  91.                             };
  92.                             break;
  93.                         case '8':
  94.                             {
  95.                                 Console.Write("Enter item index to remove: ");
  96.                                 int index = Convert.ToInt32(Console.ReadLine());
  97.                                 st.RemoveByIndex(index);
  98.                                 Console.WriteLine($"Success. Item removed.");
  99.                             };
  100.                             break;
  101.                         default:
  102.                             {
  103.                                 Console.WriteLine("Incorrect input.");
  104.                             };
  105.                             break;
  106.                     }
  107.                 }
  108.                 catch(Exception ex)
  109.                 {
  110.                     Console.WriteLine($"Exception: {ex.Message}.");
  111.                 }
  112.                 Console.WriteLine();
  113.                 Console.Write("Press any key to continue...");
  114.                 Console.ReadKey();
  115.                 Console.Clear();
  116.             }
  117.         }
  118.  
  119.         static void test()
  120.         {
  121.             var mike = new CSStudent("Mike", 80);
  122.             var alex = new CSStudent("Alex", 90);
  123.             var tommy = new CSStudent("Tommy", 100);
  124.             var prog = new CSStudent("1CProgrammer", -20);
  125.             var st = new Storage<CSStudent>();
  126.  
  127.             st.Add(mike);
  128.             st.Add(alex);
  129.             st.Add(tommy);
  130.             st.Add(prog);
  131.  
  132.             st.Remove(tommy);
  133.             st.RemoveByIndex(0);
  134.             foreach (var student in st)
  135.             {
  136.                 Console.WriteLine($"{student.Name}: {student.IQ}");
  137.                 student.InstallArch();
  138.             }
  139.  
  140.             foreach (var student in st)
  141.             {
  142.                 Console.WriteLine($"{student.Name}: {student.IQ}");
  143.             }
  144.  
  145.             var kek = st.GetByIndex(0);
  146.             Console.WriteLine($"index 0 {kek.Name}: {kek.IQ}");
  147.         }
  148.     }
  149.  
  150.     class Storage<T> : IEnumerable<T>
  151.     {
  152.         private class Node
  153.         {
  154.             public T Value { get; }
  155.             public Node Next { get; set; }
  156.             public Node Prev { get; set; }
  157.             public Node(T value)
  158.             {
  159.                 this.Value = value;
  160.             }
  161.         }
  162.  
  163.         private Node _first;
  164.         private Node _last;
  165.         private Node _current;
  166.         private int _currentIndex;
  167.         private int _count { get; set; }
  168.  
  169.         public T Current
  170.         {
  171.             get
  172.             {
  173.                 return _current.Value;
  174.             }
  175.         }
  176.  
  177.         public int CurrentIndex
  178.         {
  179.             get
  180.             {
  181.                 return _currentIndex;
  182.             }
  183.             set
  184.             {
  185.                 if (value < 0 || value > _count)
  186.                     throw new IndexOutOfRangeException();
  187.  
  188.                 _currentIndex = value;
  189.             }
  190.         }
  191.         public bool MoveNext()
  192.         {
  193.             if (_currentIndex + 1 < _count)
  194.             {
  195.                 _currentIndex++;
  196.                 _current = _current.Next;
  197.                 return true;
  198.             }
  199.             return false;
  200.         }
  201.  
  202.         public bool MovePrev()
  203.         {
  204.             if (_currentIndex > 0)
  205.             {
  206.                 _currentIndex--;
  207.                 _current = _current.Prev;
  208.                 return true;
  209.             }
  210.             return false;
  211.         }
  212.  
  213.         public int Count
  214.         {
  215.             get
  216.             {
  217.                 return _count;
  218.             }
  219.         }
  220.  
  221.         public int Add(T value)
  222.         {
  223.             var node = new Node(value);
  224.             if (_first == null)
  225.                 _first = node;
  226.             else
  227.             {
  228.                 _last.Next = node;
  229.                 node.Prev = _last;
  230.             }
  231.  
  232.             _last = node;
  233.             _currentIndex = _count;
  234.             _current = node;
  235.             _count++;
  236.  
  237.             return _currentIndex;
  238.         }
  239.  
  240.         private void RemoveNode(Node node)
  241.         {
  242.             if (_current.Equals(node)) this.MovePrev();
  243.             // если узел не последний
  244.             if (node.Next != null)
  245.             {
  246.                 node.Next.Prev = node.Prev;
  247.             }
  248.             else
  249.             {
  250.                 // если последний, переустанавливаем _last
  251.                 _last = node.Prev;
  252.             }
  253.  
  254.             // если узел не первый
  255.             if (node.Prev != null)
  256.             {
  257.                 node.Prev.Next = node.Next;
  258.             }
  259.             else
  260.             {
  261.                 // если первый, переустанавливаем _first
  262.                 _first = node.Next;
  263.             }
  264.  
  265.             _count--;
  266.             return;
  267.         }
  268.  
  269.         public void Remove(T value)
  270.         {
  271.             for (Node node = _first; node != null; node = node.Next)
  272.             {
  273.                 if (node.Value.Equals(value))
  274.                 {
  275.                     RemoveNode(node);
  276.                     return;
  277.                 }
  278.             }
  279.  
  280.             throw new Exception($"Element {value} not found");
  281.         }
  282.  
  283.         public void RemoveByIndex(int index)
  284.         {
  285.             bool reversedSearch = (index > (_count / 2));
  286.             //Console.WriteLine(reversedSearch);
  287.             if (reversedSearch)
  288.             {
  289.                 int i = _count - 1;
  290.                 for (Node node = _last; node != null; node = node.Prev)
  291.                 {
  292.                     if (i == index)
  293.                     {
  294.                         RemoveNode(node);
  295.                         return;
  296.                     }
  297.                     i--;
  298.                 }
  299.             }
  300.             else
  301.             {
  302.                 int i = 0;
  303.                 for (Node node = _first; node != null; node = node.Next)
  304.                 {
  305.                     if (i == index)
  306.                     {
  307.                         RemoveNode(node);
  308.                         return;
  309.                     }
  310.                     i++;
  311.                 }
  312.             }
  313.  
  314.             throw new Exception($"Element at index {index} not found");
  315.         }
  316.  
  317.         public T GetByIndex(int index)
  318.         {
  319.             bool reversedSearch = (index > (_count / 2));
  320.             //Console.WriteLine(reversedSearch);
  321.             if (reversedSearch)
  322.             {
  323.                 int i = _count - 1;
  324.                 for (Node node = _last; node != null; node = node.Prev)
  325.                 {
  326.                     if (i == index) return node.Value;
  327.                     i--;
  328.                 }
  329.             }
  330.             else
  331.             {
  332.                 int i = 0;
  333.                 for (Node node = _first; node != null; node = node.Next)
  334.                 {
  335.                     if (i == index) return node.Value;
  336.                     i++;
  337.                 }
  338.             }
  339.  
  340.             throw new Exception($"Element at index {index} not found");
  341.         }
  342.  
  343.         public IEnumerator<T> GetEnumerator()
  344.         {
  345.             for (Node node = _first; node != null; node = node.Next)
  346.             {
  347.                 yield return node.Value;
  348.             }
  349.         }
  350.  
  351.         IEnumerator IEnumerable.GetEnumerator()
  352.         {
  353.             return this.GetEnumerator();
  354.         }
  355.     }
  356.  
  357.     class CSStudent
  358.     {
  359.         public string Name { get; }
  360.         private int _iq { get; set; }
  361.         public int IQ
  362.         {
  363.             get
  364.             {
  365.                 return _iq;
  366.             }
  367.         }
  368.         public CSStudent(string name, int iq)
  369.         {
  370.             this.Name = name;
  371.             this._iq = iq;
  372.         }
  373.         public void InstallArch()
  374.         {
  375.             Console.WriteLine($"User {Name} has installed ArchLinux. Congrats!");
  376.             this._iq += 9999;
  377.         }
  378.     }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement