Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Dictionary <string, WeatherInfo> registry = new Dictionary <string, WeatherInfo>();

            Regex  regex = new Regex(@"(?<city>[A-Z]{2})(?<temp>\d+\.\d+)(?<weather>[a-zA-Z]+)\|");
            string line  = Console.ReadLine();

            while (line != "end")
            {
                var weatherMatch = regex.Match(line);
                if (!weatherMatch.Success)
                {
                    line = Console.ReadLine();
                    continue;
                }
                var currCity        = weatherMatch.Groups["city"].Value;
                var currAvgTemp     = double.Parse(weatherMatch.Groups["temp"].Value);
                var currWeatherType = weatherMatch.Groups["weather"].Value;
                var currWeatherInfo = new WeatherInfo
                {
                    Average = currAvgTemp,
                    Weather = currWeatherType
                };
                //currWeatherInfo.Average = currAvgTemp;
                currWeatherInfo.Weather = currWeatherType;

                registry[currCity] = currWeatherInfo;
                line = Console.ReadLine();
            }
            foreach (var item in registry.OrderBy(x => x.Value.Average))
            {
                Console.WriteLine($"{item.Key} => {item.Value.Average:F2} => {item.Value.Weather}");
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var regex  = new Regex(@"(?<city>[A-Z]{2})(?<temp>[0-9]{1,2}\.[0-9]{1,2})(?<weather>[A-Za-z]+)\|");
            var cities = new Dictionary <string, WeatherInfo>();
            var line   = Console.ReadLine();

            while (line != "end")
            {
                var weatherMatch = regex.Match(line);
                if (!weatherMatch.Success)
                {
                    line = Console.ReadLine();
                    continue;
                }
                var city        = weatherMatch.Groups["city"].Value;
                var averageTemp = double.Parse(weatherMatch.Groups["temp"].Value);
                var weather     = weatherMatch.Groups["weather"].Value;
                var weatherInfo = new WeatherInfo
                {
                    AverageTemperature = averageTemp,
                    Weather            = weather
                };
                cities[city] = weatherInfo;
                line         = Console.ReadLine();
            }
            var sortedCities = cities.OrderBy(a => a.Value.AverageTemperature).ToDictionary(a => a.Key, a => a.Value);

            foreach (var cityInfo in sortedCities)
            {
                var city        = cityInfo.Key;
                var weatherInfo = cityInfo.Value;
                Console.WriteLine($"{city} => {weatherInfo.AverageTemperature:f2} => {weatherInfo.Weather}");
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var regex =
                new Regex(@"(?<city>[A-Z]{2})(?<temp>\d+\.\d+)(?<weather>[A-Za-z]+(&|(?=\|)))");


            var cities = new Dictionary <string, WeatherInfo>();

            while (true)
            {
                var input = Console.ReadLine();

                if (input == "end")
                {
                    break;
                }
                var weatherMatch = regex.Match(input);

                if (!weatherMatch.Success)
                {
                    continue;
                }


                var city        = weatherMatch.Groups["city"].Value;
                var temp        = double.Parse(weatherMatch.Groups["temp"].Value);
                var weatherType = weatherMatch.Groups["weather"].Value;

                if (!cities.ContainsKey(city))
                {
                    cities[city] = new WeatherInfo()
                    {
                        AverageTemp = temp,
                        WeatherType = weatherType
                    };
                }

                cities[city].AverageTemp = temp;
                cities[city].WeatherType = weatherType;
            }

            foreach (var m in cities.OrderBy(x => x.Value.AverageTemp))
            {
                Console.WriteLine($"{m.Key} => {m.Value.AverageTemp:f2} => {m.Value.WeatherType}");
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            var regex = new Regex(@"(?<city>[A-Z]{2})(?<temp>\d+\.\d+)(?<weather>[a-zA-Z]+)\|");

            var cities = new Dictionary <string, WeatherInfo>();


            var textInput = Console.ReadLine();

            while (textInput != "end")
            {
                var weatherMatch = regex.Match(textInput);

                if (!weatherMatch.Success)
                {
                    textInput = Console.ReadLine();
                    continue;
                }

                var city        = weatherMatch.Groups["city"].Value;
                var temperature = double.Parse(weatherMatch.Groups["temp"].Value);
                var weather     = weatherMatch.Groups["weather"].Value;

                var weatherInfo = new WeatherInfo
                {
                    AverageTemp = temperature,
                    Weather     = weather
                };

                cities[city] = weatherInfo;

                textInput = Console.ReadLine();
            }

            var sortedCities = cities.OrderBy(x => x.Value.AverageTemp).ToDictionary(a => a.Key, a => a.Value);

            foreach (var cityWeatherInfo in sortedCities)
            {
                var city        = cityWeatherInfo.Key;
                var weatherInfo = cityWeatherInfo.Value;

                Console.WriteLine($"{city} => {weatherInfo.AverageTemp:f2} => {weatherInfo.Weather}");
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            var pattern = @"(?<town>[A-Z]{2})(?<temp>[\d]+\.[\d]+)(?<weather>[a-zA-Z]+)\|";
            var result  = new Dictionary <string, WeatherInfo>();

            var   input = Console.ReadLine();
            Regex regex = new Regex(pattern);

            while (true)
            {
                if (input == "end")
                {
                    break;
                }
                var match = regex.Match(input);
                if (match.Success)
                {
                    var town        = match.Groups["town"].ToString();
                    var temperature = double.Parse(match.Groups["temp"].ToString());
                    var weather     = match.Groups["weather"].ToString();

                    var info = new WeatherInfo {
                        Temperature = temperature, Weather = weather
                    };

                    result[town] = info;
                    input        = Console.ReadLine();
                }
                else
                {
                    input = Console.ReadLine();
                }
            }
            var sorted = result.OrderBy(a => a.Value.Temperature);

            foreach (var a in sorted)
            {
                Console.WriteLine($"{a.Key} => {a.Value.Temperature} => {a.Value.Weather}");
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            var regex = new Regex(@"(?<city>[A-Z]{2})(?<temp>\d+\.\d+)(?<weather>[A-Za-z]+)\|");
            var dict  = new Dictionary <string, WeatherInfo>();

            while (true)
            {
                var input = Console.ReadLine();
                if (input == "end")
                {
                    break;
                }

                Match matchedInput = regex.Match(input);

                if (matchedInput.Success)
                {
                    var city    = matchedInput.Groups["city"].Value;
                    var temp    = double.Parse(matchedInput.Groups["temp"].Value);
                    var weather = matchedInput.Groups["weather"].Value;

                    var classWeatherInfo = new WeatherInfo()
                    {
                        Temp    = temp,
                        Weather = weather
                    };

                    dict[city] = classWeatherInfo;
                }
            }

            var output = dict.OrderBy(a => a.Value.Temp);

            foreach (var item in output)
            {
                Console.WriteLine($"{item.Key} => {item.Value.Temp:F2} => {item.Value.Weather}");
            }
        }
        static void Main(string[] args)
        {
            var input        = Console.ReadLine();
            var weatherInfos = new Dictionary <string, WeatherInfo>();


            while (input != "end")
            {
                var pattern = @"(?<city>[A-Z]{2})(?<temp>\d{2}\.\d+)(?<weather>[a-zA-Z]+)\|";

                var match = Regex.Match(input, pattern);
                if (match.Success)
                {
                    var city    = match.Groups["city"].Value;
                    var temp    = double.Parse(match.Groups["temp"].Value);
                    var weather = match.Groups["weather"].Value;

                    var weatherInfo = new WeatherInfo()
                    {
                        Temperature = temp,
                        Weather     = weather
                    };

                    weatherInfos[city] = weatherInfo;
                }

                input = Console.ReadLine();
            }

            weatherInfos = weatherInfos.OrderBy(a => a.Value.Temperature).ToDictionary(a => a.Key, a => a.Value);

            foreach (var info in weatherInfos)
            {
                Console.WriteLine($"{info.Key} => {info.Value.Temperature:F2} => {info.Value.Weather}");
            }
        }