Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace SoftUniLists
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Enter your numbers here: ");
- List<int> numbers = ReadList();
- Console.WriteLine("There are several command you can use now:");
- Console.WriteLine("Add {} - adds the given number to the list.");
- Console.WriteLine("Remove {} - removes the given number from the list.");
- Console.WriteLine("RemoveAt {} - removes a number from the given index.");
- Console.WriteLine("Insert {number} {index} - removes a given number from a given index.");
- Console.WriteLine("end - stops the program and prints the final list.");
- while (true)
- {
- string line = Console.ReadLine();
- if(line == "end")
- {
- break;
- }
- string[] tokens = line.Split();
- switch (tokens[0])
- {
- case "Add":
- Add(numbers,int.Parse(tokens[1]));
- break;
- case "Remove":
- Remove(numbers, int.Parse(tokens[1]));
- break;
- case "RemoveAt":
- RemoveAt(numbers, int.Parse(tokens[1]));
- break;
- case "Insert":
- Insert(numbers, int.Parse(tokens[1]), int.Parse(tokens[2]));
- break;
- default:
- Console.WriteLine("Invalid command!");
- break;
- }
- }
- Console.WriteLine(string.Join(" ", numbers));
- }
- static List<int> ReadList()
- {
- List<int> list = new List<int>();
- var line = Console.ReadLine();
- list = line.Split().Select(int.Parse).ToList();
- return list;
- }
- static void Add(List<int> numbers,int num)
- {
- numbers.Add(num);
- }
- static void Remove(List<int> numbers, int num)
- {
- numbers.Remove(num);
- }
- static void RemoveAt(List<int> numbers, int index)
- {
- numbers.RemoveAt(index);
- }
- static void Insert(List<int> numbers, int num,int index)
- {
- numbers.Insert(index,num);
- }
- }
- }
Add Comment
Please, Sign In to add comment