ralichka

Exam-30.06.2019-03.ContactList

Oct 30th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03.ContactList
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> contacts = Console.ReadLine().Split().ToList();
  12.  
  13.             string[] command = Console.ReadLine().Split();
  14.             string word = command[0];
  15.  
  16.             while (word != "Print")
  17.             {
  18.                 if (word == "Add")
  19.                 {
  20.                     string name = command[1];
  21.                     int index = int.Parse(command[2]);
  22.  
  23.                     if (index >= 0 && index < contacts.Count)
  24.                     {
  25.                         if (!contacts.Contains(name))
  26.                         {
  27.                             contacts.Add(name);
  28.                         }
  29.                         else
  30.                         {
  31.                             contacts.Insert(index, name);
  32.                         }
  33.                     }
  34.                 }
  35.                 else if (word == "Remove")
  36.                 {
  37.                     int index = int.Parse(command[1]);
  38.  
  39.                     if (index >= 0 && index < contacts.Count)
  40.                     {
  41.                         contacts.RemoveAt(index);
  42.                     }
  43.                 }
  44.                 else if (word == "Export")
  45.                 {
  46.                     int startIndex = int.Parse(command[1]);
  47.                     int count = int.Parse(command[2]);
  48.  
  49.                     List<string> exportedContacts = new List<string>();
  50.  
  51.                     int lastIndex = startIndex + count;
  52.  
  53.                     if (lastIndex > contacts.Count)
  54.                     {
  55.                         lastIndex = contacts.Count;
  56.                     }
  57.  
  58.                     for (int i = startIndex; i < lastIndex; i++)
  59.                     {
  60.                         exportedContacts.Add(contacts[i]);
  61.                     }
  62.                     Console.WriteLine(string.Join(" ", exportedContacts));
  63.                 }
  64.                 command = Console.ReadLine().Split();
  65.                 word = command[0];
  66.             }
  67.  
  68.             string type = command[1];
  69.             if (type == "Normal")
  70.             {
  71.                 Console.WriteLine("Contacts: " + string.Join(" ", contacts));
  72.             }
  73.             else if (type == "Reversed")
  74.             {
  75.                 contacts.Reverse();
  76.                 Console.WriteLine("Contacts: " + string.Join(" ", contacts));
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment