mockingbird_ls

Exam 2015 (Regex)

Oct 12th, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class Problem04
  7. {
  8.     static void Main()
  9.     {
  10.         var srubskoInfo = new Dictionary<string, Dictionary<string, long>>();
  11.  
  12.         Regex validation = new Regex(@"((([a-zA-Z])+\s))+@(([a-zA-Z])+\s{0,1}([a-zA-Z]))*(\s[0-9]+)+");
  13.         Regex foundVenue = new Regex(@"@([a-zA-z]+\s)+");
  14.         Regex foundPerformer = new Regex(@"^([a-zA-z]+)(\s*[a-zA-z])*");
  15.         Regex foundTicketsData = new Regex(@"\d+\s\d+");
  16.  
  17.         string input = Console.ReadLine();
  18.  
  19.         while (input != "End")
  20.         {
  21.             if (validation.IsMatch(input))
  22.             {
  23.                 var performerName = foundPerformer.Match(input).Value.Trim();
  24.                 var moneyData = foundTicketsData.Match(input).Value.Split();
  25.                 var performingPlaceAsArr = foundVenue.Match(input).Value.Split(new [] { '@', ' '}, StringSplitOptions.RemoveEmptyEntries);
  26.                 var performingPlace = string.Join(" ", performingPlaceAsArr);                
  27.                 long money = long.Parse(moneyData[0]) * long.Parse(moneyData[1]);
  28.                
  29.  
  30.                 if (!srubskoInfo.ContainsKey(performingPlace))
  31.                 {
  32.                     srubskoInfo[performingPlace] = new Dictionary<string, long>();
  33.                 }
  34.                 if (!srubskoInfo[performingPlace].ContainsKey(performerName))
  35.                 {
  36.                     srubskoInfo[performingPlace].Add(performerName,money);
  37.                 }
  38.                 else
  39.                 {
  40.                     srubskoInfo[performingPlace][performerName] += money;
  41.                 }
  42.             }
  43.             else
  44.             {
  45.                 input = Console.ReadLine();
  46.                 continue;
  47.             }
  48.  
  49.             input = Console.ReadLine();
  50.         }
  51.  
  52.         foreach (var city in srubskoInfo)
  53.         {
  54.             Console.WriteLine(city.Key);
  55.  
  56.             foreach (var pair in city.Value.OrderByDescending(pair => pair.Value))
  57.             {
  58.                 Console.WriteLine("#  {0} -> {1}", pair.Key, pair.Value);
  59.             }
  60.         }
  61.        
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment