Advertisement
KeepCoding

P04Weather-RegexExercises

Feb 26th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace P04Weather
  7. {
  8.     class WeatherInformation
  9.     {
  10.         public double Temperature { get; set; }
  11.         public string Weather { get; set; }
  12.  
  13.         public WeatherInformation(double temperature, string weather)
  14.         {
  15.             Temperature = temperature;
  16.             Weather = weather;
  17.         }
  18.     }
  19.     class Program
  20.     {
  21.         static void Main(string[] args)
  22.         {
  23.             //List<string> inputs = new List<string>();
  24.             Dictionary<string, WeatherInformation> townWeather = new Dictionary<string, WeatherInformation>();
  25.             Regex regex = new Regex(@"(?<town>[A-Z]{2})(?<temperature>[0-9]{1,}\.[0-9]{1,})(?<weather>\w+)(?=\|)");
  26.  
  27.             MatchCollection matches;
  28.  
  29.             while (true)
  30.             {
  31.                 string currentInput = Console.ReadLine();
  32.                 if (currentInput == "end")
  33.                 {
  34.                     break;
  35.                 }
  36.                 if (regex.IsMatch(currentInput))
  37.                 {
  38.                     Match match = regex.Match(currentInput);
  39.                     if (townWeather.ContainsKey(match.Groups["town"].ToString()) == false)
  40.                     {
  41.                         WeatherInformation currentInfo = new WeatherInformation(
  42.                             double.Parse(match.Groups["temperature"].ToString()),
  43.                             match.Groups["weather"].ToString());
  44.  
  45.                         townWeather.Add(match.Groups["town"].ToString(), currentInfo);
  46.                     }
  47.                     else
  48.                     {
  49.                         //WeatherInformation currentInfo = new WeatherInformation( // ima li nujda it "new" po-skoro ne, no ne znam kak inache da go napravq
  50.                         //    double.Parse(match.Groups["temperature"].ToString()),
  51.                         //    match.Groups["weather"].ToString());
  52.                         //townWeather[match.Groups["town"].ToString()] = currentInfo;
  53.                         string weather = match.Groups["weather"].ToString();
  54.                         double temperature = double.Parse(match.Groups["temperature"].ToString());
  55.                         townWeather[match.Groups["town"].ToString()].Weather = weather;
  56.                         townWeather[match.Groups["town"].ToString()].Temperature = temperature;
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             foreach (var kvp in townWeather.Values.OrderBy(t => t.Temperature))
  62.             {
  63.                
  64.             }
  65.             //Console.WriteLine($"{currentMatch.Groups["town"]} => {currentMatch.Groups["temperature"]} => {currentMatch.Groups["weather"]}");
  66.  
  67.             //main ends here
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement