Advertisement
gospod1978

Regular Expression/Nether Realms

Oct 11th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Text.RegularExpressions;
  8.  
  9.  
  10. namespace Orders
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             string heltsPattern = @"[^0-9+\-*\/.]";
  17.             Regex heltsRegex = new Regex(heltsPattern);
  18.  
  19.             string digitPattern = @"-?\d+\.?\d*";
  20.             Regex digitRegex = new Regex(digitPattern);
  21.  
  22.             string operatorPattern = @"[*\/]";
  23.             Regex operatorRegex = new Regex(operatorPattern);
  24.  
  25.             string[] demonNames = Regex.Split(Console.ReadLine(), @"\s*,\s*").OrderBy(x => x).ToArray();
  26.  
  27.             for (int i = 0; i < demonNames.Length; i++)
  28.             {
  29.                 string currentDemon = demonNames[i];
  30.                 int currentHelts = 0;
  31.  
  32.                 MatchCollection heltsSymbols = heltsRegex.Matches(currentDemon);
  33.  
  34.                 foreach (Match symbol in heltsSymbols)
  35.                 {
  36.                     currentHelts += char.Parse(symbol.Value);
  37.                 }
  38.  
  39.                 double baseDamage = 0;
  40.  
  41.                 MatchCollection digitMatch = digitRegex.Matches(currentDemon);
  42.  
  43.  
  44.                 foreach (Match number in digitMatch)
  45.                 {
  46.                     baseDamage += double.Parse(number.Value);
  47.                 }
  48.  
  49.  
  50.                 MatchCollection operatorMatch = operatorRegex.Matches(currentDemon);
  51.  
  52.  
  53.                 foreach (Match operatorr in operatorMatch)
  54.                 {
  55.                     string ooper = operatorr.Value;
  56.  
  57.                     if (ooper == "*")
  58.                     {
  59.                         baseDamage *= 2;
  60.                     }
  61.                     else
  62.                     {
  63.                         baseDamage /= 2;
  64.                     }
  65.  
  66.                 }
  67.  
  68.                 Console.WriteLine($"{currentDemon} - {currentHelts} health, {baseDamage:f2} damage");
  69.  
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement