コード例 #1
0
ファイル: Program.cs プロジェクト: Eger37/OOP_C_Sharp
 public WeatherParametersDay(float td, float tn, float ap, int pr, TypeOfWeather tow) // constuctor
 {
     AverageTemperaturePerDay   = td;
     AverageTemperatureAtNight  = tn;
     AverageAtmosphericPressure = ap;
     Precipitation      = pr;
     TypeOfWeatherAtDay = tow;
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: MarynaOhorodnik/OOP_tasks
        public WeatherParametersDay(double average_temperature_day, double average_temperature_night,
                                    double average_atmospheric_pressure, double precipitation, TypeOfWeather type_of_weather)
        {
            if (precipitation >= 0 && average_atmospheric_pressure >= 0 && Enum.IsDefined(typeof(TypeOfWeather), type_of_weather))
            {
                Average_temperature_day      = average_temperature_day;
                Average_temperature_night    = average_temperature_night;
                Average_atmospheric_pressure = average_atmospheric_pressure;
                Precipitation   = precipitation;
                Type_of_weather = type_of_weather;
            }

            else
            {
                Console.WriteLine("Некоректні дані. Екзмепляр не ініціалізувався");
            }
        }
コード例 #3
0
 public WeatherParametersDay(double averageTemperatureDay,
                             double averageTemperatureNight,
                             double averageAtmosphericPressure,
                             double precipitation,
                             int typeOfWeather)
 {
     if (precipitation >= 0 &&
         averageAtmosphericPressure >= 0 &&
         Enumerable.Range(0, 7).Contains(typeOfWeather))
     {
         AverageTemperatureDay      = averageTemperatureDay;
         AverageTemperatureNight    = averageTemperatureNight;
         AverageAtmosphericPressure = averageAtmosphericPressure;
         Precipitation = precipitation;
         TypeOfWeather = (TypeOfWeather)typeOfWeather;
     }
 }
コード例 #4
0
ファイル: Program.cs プロジェクト: Eger37/OOP_C_Sharp
        private static WeatherDays InputDataFromConsole()
        {
            WeatherDays arrDaysParams;

            Console.WriteLine("Ввод из консоли");
            int numberOfDays;

            while (true)
            {
                Console.WriteLine("Введите количество дней:");
                string strNumberOfDays = Console.ReadLine();
                try
                {
                    numberOfDays = Convert.ToInt32(strNumberOfDays);
                    break;
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Вы не правильно ввели данные: {ex.Message}");
                    continue;
                }
            }
            arrDaysParams = new WeatherDays(numberOfDays);

            Console.WriteLine("В слудующих строчках вводите параметры погоды дня в формате:");
            Console.WriteLine("f1 f2 f3 i s");
            Console.WriteLine("f1 = Средняя температура днём - float");
            Console.WriteLine("f2 = Средняя температура ночью - float");
            Console.WriteLine("f3 = Средняе атмосферное давление - float (мм ртутного столба");
            Console.WriteLine("i = Количество осадков - int (мм/день)");
            Console.WriteLine("s = Тип погоды в этот день - string (ниже примеры типов погоды, вводить это значение необязательно)");
            Console.WriteLine("Типы погоды: не_определено, дождь, кратковременный_дождь, гроза, снег, туман, хмуро, солнечно\n");

            int count = 0;

            while (count != numberOfDays)
            {
                while (true)
                {
                    Console.WriteLine($"Вводите параметры {count + 1} дня:");
                    string   nextLine  = Console.ReadLine();
                    string[] splitLine = nextLine.Split(" ");
                    if (splitLine.Length != 4 & splitLine.Length != 5)
                    {
                        Console.WriteLine($"Данные введены неверно");
                        continue;
                    }
                    try
                    {
                        float         AverageTemperaturePerDay   = float.Parse(splitLine[0], System.Globalization.CultureInfo.InvariantCulture);
                        float         AverageTemperatureAtNight  = float.Parse(splitLine[1], System.Globalization.CultureInfo.InvariantCulture);
                        float         AverageAtmosphericPressure = float.Parse(splitLine[2], System.Globalization.CultureInfo.InvariantCulture);
                        int           Precipitation      = Convert.ToInt32(splitLine[3]);
                        TypeOfWeather TypeOfWeatherAtDay = TypeOfWeather.не_определено;
                        if (splitLine.Length == 5)
                        {
                            TypeOfWeatherAtDay = (TypeOfWeather)Enum.Parse(typeof(TypeOfWeather), splitLine[4], true);
                        }
                        WeatherParametersDay dayParams = new WeatherParametersDay(AverageTemperaturePerDay, AverageTemperatureAtNight, AverageAtmosphericPressure, Precipitation, TypeOfWeatherAtDay);
                        arrDaysParams.insertDaySettings(count, dayParams);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Данные введены неверно");
                        Console.WriteLine($"Ошибка: {ex.Message}");
                        continue;
                    }
                }
                count++;
            }

            return(arrDaysParams);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: Eger37/OOP_C_Sharp
        private static WeatherDays InputDataFromFile()
        {
            WeatherDays arrDaysParams;

            Console.WriteLine("Ввод из файла");
            Console.WriteLine("В первой строчке введите количество дней.");
            Console.WriteLine("В слудующих строчках вводите параметры погоды дня в формате:");
            Console.WriteLine("f1 f2 f3 i s");
            Console.WriteLine("f1 = Средняя температура днём - float");
            Console.WriteLine("f2 = Средняя температура ночью - float");
            Console.WriteLine("f3 = Средняе атмосферное давление - float (мм ртутного столба");
            Console.WriteLine("i = Количество осадков - int (мм/день)");
            Console.WriteLine("s = Тип погоды в этот день - string (ниже примеры типов погоды, вводить это значение необязательно)");
            Console.WriteLine("Типы погоды: не_определено, дождь, кратковременный_дождь, гроза, снег, туман, хмуро, солнечно\n");

            string PathToFile = "D:/OneDrive - ДонНУ/Рабочий стол/Univer/WeatherDays.txt";

            //string PathToFile = "C:/Users/Admin/Desktop/Univer/WeatherDays.txt";
            while (true)
            {
                bool restartRead = false;
                if (!File.Exists(PathToFile))
                {
                    Console.WriteLine($"Файл не найден, создайте файл WeatherDays.txt по пути {PathToFile}");
                    Console.WriteLine("Нажмите \"Enter\", если создали файл");
                    Console.ReadLine();
                    continue;
                }
                int numberOfDays;
                using (StreamReader fileData = new StreamReader(PathToFile))
                {
                    //StreamReader fileData = new StreamReader(PathToFile);
                    string firstLine = fileData.ReadLine();

                    if (firstLine == null)
                    {
                        Console.WriteLine($"Файл {Path.GetFileName(PathToFile)} пустой, введите в него данные!");
                        fileData.Close();
                        Console.WriteLine("Нажмите \"Enter\", если изменили файл");
                        Console.ReadLine();
                        continue;
                    }
                    try
                    { numberOfDays = Convert.ToInt32(firstLine); }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Вы не правильно ввели данные: {ex.Message}");
                        fileData.Close();
                        Console.WriteLine("Нажмите \"Enter\", если изменили файл");
                        Console.ReadLine();
                        continue;
                    }
                    arrDaysParams = new WeatherDays(numberOfDays);
                    int count = 0;
                    while (count != numberOfDays)
                    {
                        string nextLine = fileData.ReadLine();
                        if (nextLine == null)
                        {
                            Console.WriteLine($"В строке {count + 2} пусто");
                            fileData.Close();
                            Console.WriteLine("Нажмите \"Enter\", если исправили строку");
                            Console.ReadLine();
                            restartRead = true;
                            break;
                        }
                        string[] splitLine = nextLine.Split(" ");
                        if (splitLine.Length != 4 & splitLine.Length != 5)
                        {
                            Console.WriteLine($"В строке {count + 2} данные введены неверно");
                            fileData.Close();
                            //Console.WriteLine($"count: {count}, splitLine.Length {splitLine.Length}");
                            Console.WriteLine("Нажмите \"Enter\", если исправили строку");
                            Console.ReadLine();
                            restartRead = true;
                            break;
                        }
                        try
                        {
                            float         AverageTemperaturePerDay   = float.Parse(splitLine[0], System.Globalization.CultureInfo.InvariantCulture);
                            float         AverageTemperatureAtNight  = float.Parse(splitLine[1], System.Globalization.CultureInfo.InvariantCulture);
                            float         AverageAtmosphericPressure = float.Parse(splitLine[2], System.Globalization.CultureInfo.InvariantCulture);
                            int           Precipitation      = Convert.ToInt32(splitLine[3]);
                            TypeOfWeather TypeOfWeatherAtDay = TypeOfWeather.не_определено;
                            if (splitLine.Length == 5)
                            {
                                TypeOfWeatherAtDay = (TypeOfWeather)Enum.Parse(typeof(TypeOfWeather), splitLine[4], true);
                            }
                            WeatherParametersDay dayParams = new WeatherParametersDay(AverageTemperaturePerDay, AverageTemperatureAtNight, AverageAtmosphericPressure, Precipitation, TypeOfWeatherAtDay);
                            arrDaysParams.insertDaySettings(count, dayParams);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"В строке {count + 2} данные введены неверно");
                            Console.WriteLine($"Ошибка: {ex.Message}");
                            fileData.Close();
                            Console.WriteLine("Нажмите \"Enter\", если исправили строку");
                            Console.ReadLine();
                            restartRead = true;
                            break;
                        }
                        count++;
                        if (restartRead)
                        {
                            break;
                        }
                    }
                    if (restartRead)
                    {
                        continue;
                    }
                }

                break;
            }
            Console.WriteLine("\nФайл принят");
            return(arrDaysParams);
        }