Advertisement
martinvalchev

Weather

Feb 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Weather
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string pattern = @"([A-Z][A-Z])([0-9]+\.[0-9]+)([A-Za-z]+)\|";
  15.  
  16.             Dictionary<string, WeatherAndTemperatur> items = new Dictionary<string, WeatherAndTemperatur>();
  17.  
  18.             string input = Console.ReadLine();
  19.  
  20.             while (input != "end")
  21.             {
  22.                 if (Regex.IsMatch(input, pattern))
  23.                 {
  24.                     MatchCollection matches = Regex.Matches(input, pattern);
  25.                     foreach (Match match in matches)
  26.                     {
  27.                         double temperatur = double.Parse(match.Groups[2].Value);
  28.                         string weather = match.Groups[3].Value;
  29.                         string city = match.Groups[1].Value;
  30.  
  31.                         WeatherAndTemperatur weatherAndTemperatur = new WeatherAndTemperatur { Weather = weather, Temperatur = temperatur };
  32.  
  33.                         if (!items.ContainsKey(city))
  34.                         {
  35.                             items.Add(city, weatherAndTemperatur);
  36.                         }
  37.                         else
  38.                         {
  39.                             items[city] = weatherAndTemperatur;
  40.                         }
  41.                     }
  42.                 }
  43.                 else
  44.                 {
  45.                     input = Console.ReadLine();
  46.                     continue;
  47.                 }
  48.                 input = Console.ReadLine();
  49.             }
  50.  
  51.             foreach (var item in items.OrderBy(x => x.Value.Temperatur))
  52.             {
  53.                 Console.WriteLine($"{item.Key} => {item.Value.Temperatur:F2} => {item.Value.Weather}");
  54.             }
  55.         }
  56.     }
  57.     public class WeatherAndTemperatur
  58.     {
  59.         public string Weather { get; set; }
  60.         public double Temperatur { get; set; }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement