Guest User

Untitled

a guest
Jun 20th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _4._Weather
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.  
  13.             Dictionary<string, KeyValuePair<double, string>> wheater = new Dictionary<string, KeyValuePair<double, string>>();
  14.  
  15.             while (true)
  16.             {
  17.                 string forecast = Console.ReadLine();
  18.                 if (forecast == "end")
  19.                 {
  20.                     break;
  21.                 }
  22.                 string pattern = @"([A-Z]{2})(\d+.\d+)([a-zA-Z]+)\|";
  23.                 Regex regex = new Regex(pattern);
  24.                 Match match = regex.Match(forecast);
  25.  
  26.                 if (!match.Success)
  27.                 {
  28.                     continue;
  29.                 }
  30.                 string city = match.Groups[1].ToString();
  31.  
  32.                 double temperature = double.Parse(match.Groups[2].ToString());
  33.                 string KindOfWheather = match.Groups[3].ToString();
  34.                 if (!wheater.ContainsKey(city))
  35.                 {
  36.                     wheater.Add(city, new KeyValuePair<double, string>(temperature, KindOfWheather));
  37.                 }
  38.                 else
  39.                 {
  40.                     wheater[city] = new KeyValuePair<double, string>(temperature, KindOfWheather);
  41.                 }
  42.  
  43.             }
  44.             foreach (var city in wheater.OrderBy(x => x.Value.Key))
  45.             {
  46.                 Console.WriteLine($"{city.Key} => {city.Value.Key:F2} => {city.Value.Value}");
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment