TheBulgarianWolf

List Manipulation

Nov 10th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SoftUniLists
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.Write("Enter your numbers here: ");
  12.             List<int> numbers = ReadList();
  13.             Console.WriteLine("There are several command you can use now:");
  14.             Console.WriteLine("Add {} - adds the given number to the list.");
  15.             Console.WriteLine("Remove {} - removes the given number from  the list.");
  16.             Console.WriteLine("RemoveAt {} - removes a number from the given index.");
  17.             Console.WriteLine("Insert {number} {index}  - removes a given number from a given index.");
  18.             Console.WriteLine("end - stops the program and prints the final list.");
  19.             while (true)
  20.             {
  21.                 string line = Console.ReadLine();
  22.                 if(line == "end")
  23.                 {
  24.                     break;
  25.                 }
  26.  
  27.                 string[] tokens = line.Split();
  28.                 switch (tokens[0])
  29.                 {
  30.                     case "Add":
  31.                         Add(numbers,int.Parse(tokens[1]));
  32.                         break;
  33.                     case "Remove":
  34.                         Remove(numbers, int.Parse(tokens[1]));
  35.                         break;
  36.                     case "RemoveAt":
  37.                         RemoveAt(numbers, int.Parse(tokens[1]));
  38.                         break;
  39.                     case "Insert":
  40.                         Insert(numbers, int.Parse(tokens[1]), int.Parse(tokens[2]));
  41.                         break;
  42.                     default:
  43.                         Console.WriteLine("Invalid command!");
  44.                         break;
  45.                 }
  46.             }
  47.  
  48.             Console.WriteLine(string.Join(" ", numbers));
  49.  
  50.         }
  51.        
  52.         static List<int> ReadList()
  53.         {
  54.             List<int> list = new List<int>();
  55.             var line = Console.ReadLine();
  56.             list = line.Split().Select(int.Parse).ToList();
  57.             return list;
  58.         }
  59.        
  60.        
  61.         static void Add(List<int> numbers,int num)
  62.         {
  63.             numbers.Add(num);
  64.         }
  65.  
  66.         static void Remove(List<int> numbers, int num)
  67.         {
  68.             numbers.Remove(num);
  69.         }
  70.  
  71.         static void RemoveAt(List<int> numbers, int index)
  72.         {
  73.             numbers.RemoveAt(index);
  74.         }
  75.  
  76.         static void Insert(List<int> numbers, int num,int index)
  77.         {
  78.             numbers.Insert(index,num);
  79.         }
  80.     }
  81. }
  82.  
Add Comment
Please, Sign In to add comment