Advertisement
Vladimir76

Nether Realms

Feb 22nd, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 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. using System.Text.RegularExpressions;
  7.  
  8. namespace Nether_Realms
  9. {
  10.     class Demon
  11.     {
  12.         public string Name { get; set; }
  13.         public double Health { get; set; }
  14.         public double Damage { get; set; }
  15.     }
  16.  
  17.     class Program
  18.     {
  19.         static void Main()
  20.         {
  21.             string[] nameDemons = Console.ReadLine()
  22.                 .Trim()
  23.                 .Split(',')
  24.                 .Select(x => x.Trim())
  25.                 .ToArray();
  26.             if (nameDemons.Length==0)
  27.             {
  28.                 return;
  29.             }
  30.             List<Demon> demons = new List<Demon>();
  31.             int health = 0;
  32.             double resultDamage = 0;
  33.             string patternForHealth = @"[^0-9\+-\.*/]";
  34.             string patternForDamage = @"[\d+-.]+";
  35.             string patternAction = @"[*/]";
  36.  
  37.             for (int i = 0; i < nameDemons.Length; i++)
  38.             {
  39.                 Demon demon = new Demon();  
  40.                 Match matchHealth = Regex.Match(nameDemons[i], patternForHealth);
  41.                 Match matchDamage = Regex.Match(nameDemons[i], patternForDamage);
  42.                 Match matchAction = Regex.Match(nameDemons[i], patternAction);
  43.                 while (matchHealth.Success)
  44.                 {
  45.                     int codeSymbol = Convert.ToChar(matchHealth.ToString());
  46.                     health += codeSymbol;
  47.                     matchHealth = matchHealth.NextMatch();
  48.                 }
  49.                 while (matchDamage.Success)
  50.                 {
  51.                     double currentDamage = double.Parse(matchDamage.ToString());
  52.                     resultDamage += currentDamage;
  53.                     matchDamage = matchDamage.NextMatch();
  54.                 }
  55.                 while (matchAction.Success)
  56.                 {
  57.                     char ch = Convert.ToChar(matchAction.ToString());
  58.                     if (ch=='/')
  59.                     {
  60.                         resultDamage /= 2;
  61.                     }
  62.                     else if (ch=='*')
  63.                     {
  64.                         resultDamage *= 2;
  65.                     }
  66.                     matchAction = matchAction.NextMatch();
  67.                 }
  68.                 demon.Name = nameDemons[i];
  69.                 demon.Health = health;
  70.                 demon.Damage = resultDamage;
  71.                 demons.Add(demon);
  72.                 health = 0;
  73.                 resultDamage = 0;
  74.             }
  75.             var sortDemons = demons.OrderBy(x => x.Name);
  76.             foreach (var result in sortDemons)
  77.             {
  78.                 Console.WriteLine("{0} - {1} health, {2:F2} damage"
  79.                     ,result.Name,result.Health,result.Damage);
  80.             }
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement