private static List <Demon> FillDemon(string line) { string[] demonName = DemonName(line); List <Demon> demonsCollection = new List <Demon>(); for (int i = 0; i < demonName.Length; i++) { int health = TotalHealth(demonName[i]); List <string> opp = MultiplyDivide(demonName[i]); decimal damage = Damage(demonName[i], opp); Demon demon = new Demon(demonName[i], health, damage); demonsCollection.Add(demon); } return(demonsCollection); }
static void Main() { List <Demon> demonList = new List <Demon>(); string[] input = Regex.Split(Console.ReadLine(), @"\s*,\s*"); foreach (string demon in input) { Regex patterHealth = new Regex(@"[^+\-*\/0-9\.]"); Regex patternDigit = new Regex(@"-?\d+\.?\d*"); Regex patternOpertation = new Regex(@"[*\/]"); double damage = 0; int health = 0; MatchCollection matchesHealth = patterHealth.Matches(demon); MatchCollection matchesDamage = patternDigit.Matches(demon); MatchCollection matchesOperator = patternOpertation.Matches(demon); foreach (Match item in matchesHealth) { health += char.Parse(item.Value); } foreach (Match item in matchesDamage) { damage += double.Parse(item.Value); } foreach (Match item in matchesOperator) { char currOpp = char.Parse(item.Value); if (currOpp == '*') { damage *= 2; } else if (currOpp == '/') { damage /= 2; } } Demon newDemon = new Demon(demon, health, damage); demonList.Add(newDemon); } foreach (var demon in demonList.OrderBy(x => x.Name)) { Console.WriteLine($"{demon.Name} - {demon.Helath} health, {demon.Damage:F2} damage"); } }
static void Main(string[] args) { string[] demons = Console.ReadLine() .Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); string Pattern = @"[-+]?[0-9]+\.?[0-9]*"; List <Demon> demonList = new List <Demon>(); foreach (string demon in demons) { string filter = "1234567890./+-*"; int health = demon.Where(c => !filter.Contains(c)).Sum(c => c); double dmg = 0; MatchCollection dmgMatches = Regex.Matches(demon, Pattern); foreach (Match m in dmgMatches) { dmg += double.Parse(m.Value); } foreach (char c in demon) { if (c == '*') { dmg *= 2; } else if (c == '/') { dmg /= 2; } } Demon current = new Demon(demon, health, dmg); demonList.Add(current); } foreach (Demon d in demonList.OrderBy(d => d.Name)) { Console.WriteLine(d.ToString()); } }