StoimenK

C#_Lists_1.6.List_Manipulation_Basics

Feb 28th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 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 _1._6.List_Manipulation_Basics
  8. {
  9.     class Program
  10.     {
  11.         // 1. Правя лист от int +++
  12.         // 2. Докато не получа команда "end", ще получавам ще получавам различни команди +++
  13.         // 3. Commands:
  14.         //      Add {number}: add a number to the end of the list.           +++
  15.         //      Remove { number}: remove a number from the list.             +++
  16.         //      RemoveAt { index}: remove a number at a given index.         +++
  17.         //      Insert { number} { index}: insert a number at a given index. +++
  18.         static void Main()
  19.         {
  20.             List<int> inNums = Console.ReadLine().Split().Select(int.Parse).ToList();
  21.            
  22.  
  23.             while (true)
  24.             {
  25.                 List<string> command = Console.ReadLine().Split().ToList();
  26.  
  27.                 string findResult = command.Find(result => result == command[0]);
  28.  
  29.                 if (findResult == "end")
  30.                 {
  31.                     break;
  32.                 }
  33.  
  34.                 else if (findResult == "Add")
  35.                 {
  36.                     int numAdd = int.Parse(command[1]);
  37.                     inNums.Add(numAdd);
  38.                 }
  39.  
  40.                 else if (findResult == "Remove")
  41.                 {
  42.                     int numRemove = int.Parse(command[1]);
  43.                     inNums.Remove(numRemove);
  44.                 }
  45.  
  46.                 else if (findResult == "RemoveAt")
  47.                 {
  48.                     int numRemoveAt = int.Parse(command[1]);
  49.                     inNums.RemoveAt(numRemoveAt);
  50.                 }
  51.                 else
  52.                 {
  53.                     int insertElement = int.Parse(command[1]);
  54.                     int insertIndex = int.Parse(command[2]);
  55.                     inNums.Insert(insertIndex, insertElement);
  56.                 }
  57.             }
  58.             Console.WriteLine(string.Join(" ", inNums));
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment