Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Linq;
  4.  
  5. namespace _01._Animal_Sanctuary
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             var countLines = int.Parse(Console.ReadLine());
  12.             var weightOfAnimals = 0;
  13.  
  14.             for (int i = 0; i < countLines; i++)
  15.             {
  16.                 var input = Console.ReadLine();
  17.  
  18.                 var splittedInput = input
  19.                     .Split(";", StringSplitOptions.RemoveEmptyEntries);
  20.  
  21.                 var checkIsValid = true;
  22.  
  23.                 if (splittedInput.Length == 3)
  24.                 {
  25.                     var name = splittedInput[0];
  26.                     var kind = splittedInput[1];
  27.                     var country = splittedInput[2];
  28.  
  29.                     if (name[0] != 'n' || name[1] != ':' || name.Contains(";"))//Проверяваме дали има ;(contains any ASCII character except for ";”)
  30.                     {
  31.                         checkIsValid = false;
  32.                     }
  33.  
  34.                     if (kind[0] != 't' || kind[1] != ':' || kind.Contains(";"))//Проверяваме дали има ;(contains any ASCII character except for ";”)
  35.                     {
  36.                         checkIsValid = false;
  37.                     }
  38.  
  39.                     if (country[0] != 'c' ||
  40.                         country[1] != '-' ||
  41.                         country[2] != '-')
  42.                     {
  43.                         checkIsValid = false;
  44.                     }
  45.  
  46.                     //Проверяваме дали държавата съдържа само букви и празни места(contains only letters and spaces) + + 'c' и '-', защото още не са махнати
  47.                     foreach (char c in country)
  48.                     {
  49.                         if (!char.IsLetter(c) && !char.IsWhiteSpace(c) && c != 'c' && c != '-')
  50.                         {
  51.                             checkIsValid = false;
  52.                         }
  53.                     }
  54.  
  55.                         if (checkIsValid)
  56.                         {
  57.                             name = name.Substring(2);
  58.                             kind = kind.Substring(2);
  59.                             country = country.Substring(3);
  60.  
  61.                             var pattern = @"[A-Za-z ]+";
  62.                             var nameAfterRegex = Regex.Matches(name, pattern);
  63.                             var kindAfterRegex = Regex.Matches(kind, pattern);
  64.                             var pattern1 = @"[A-Za-z ]+";
  65.                             var countryAfterRegex = Regex.Matches(country, pattern1);
  66.  
  67.                             foreach (var item in nameAfterRegex)
  68.                             {
  69.                                 Console.Write(item);
  70.                             }
  71.  
  72.                             Console.Write(" is a ");
  73.  
  74.                             foreach (var item in kindAfterRegex)
  75.                             {
  76.                                 Console.Write(item);
  77.                             }
  78.  
  79.                             Console.Write(" from ");
  80.  
  81.                             foreach (var item in countryAfterRegex)
  82.                             {
  83.                                 Console.Write(item);
  84.                             }
  85.  
  86.                             Console.WriteLine();
  87.  
  88.                             for (int j = 0; j < input.Length; j++)
  89.                             {
  90.                                 var symbol = input[j];
  91.  
  92.                                 if (char.IsDigit(symbol))
  93.                                 {
  94.                                     weightOfAnimals += (symbol - 48);
  95.                                 }
  96.                             }
  97.                         }
  98.                     }
  99.                 }
  100.  
  101.                 Console.WriteLine($"Total weight of animals: {weightOfAnimals}KG");
  102.             }
  103.         }
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement