Пример #1
0
        /// <summary>
        /// Determines the next date
        /// </summary>
        public static (int, int, int) IncreaseDate(string value_day, string value_month, string value_year, string count_day)
        {
            int real_day_month_next = 0;
            int leftover_days       = 0;
            int last_day_month      = 0;

            bool isNumberDay       = int.TryParse(value_day, out int day);
            bool isNumberMonth     = int.TryParse(value_month, out int month);
            bool isNumberYear      = int.TryParse(value_year, out int year);
            bool isNumberCountDays = int.TryParse(count_day, out int amount_days);

            bool isDate = AuxiliaryMethods.IsCorrectDate(isNumberDay, isNumberMonth, isNumberYear, day, month, year);

            //Validating date
            if (!isDate || !isNumberCountDays)
            {
                throw new ArgumentException("Error, incorrect date");
            }

            last_day_month = AuxiliaryMethods.LastDayMonthNext(month - 1, year);
            //Difference between the total days of the month and the number of days
            leftover_days = last_day_month - day;

            if (leftover_days >= amount_days)
            {
                day = day + amount_days;
                return(day, month, year);
            }
            real_day_month_next = amount_days - leftover_days;
            last_day_month      = AuxiliaryMethods.LastDayMonthNext(month, year);

            while (true)
            {
                if (real_day_month_next > last_day_month)
                {
                    if (month == 12)
                    {
                        month = 0;
                        year++;
                    }
                    month++;
                    real_day_month_next = real_day_month_next - last_day_month;
                    last_day_month      = AuxiliaryMethods.LastDayMonthNext(month, year);
                }
                else
                {
                    if (month == 12)
                    {
                        month = 1;
                        year++;
                        return(real_day_month_next, month, year);
                    }
                    else
                    {
                        month++;
                        return(real_day_month_next, month, year);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Returns the median of the values ​​of an array
        /// </summary>
        public static int Median(int[] listInteger)
        {
            int median = 0;

            if (listInteger.Length == 0)
            {
                throw new Exception("The array is empty");
            }
            if (listInteger == null)
            {
                throw new Exception("The array no exist");
            }

            int[] listOrder     = AuxiliaryMethods.OrderListLessBigger(listInteger);
            int   countElements = listOrder.Length;
            int   quotient      = countElements / 2;

            if (countElements % 2 == 0)
            {
                median = (listOrder[quotient] + listOrder[(quotient - 1)]) / 2;
            }
            else
            {
                median = listInteger[quotient];
            }
            return(median);
        }
Пример #3
0
        public static (int day, int month, int year) PreviousDate(string value_day, string value_month, string value_year)
        {
            bool isNumberDay   = int.TryParse(value_day, out int day);
            bool isNumberMonth = int.TryParse(value_month, out int month);
            bool isNumberYear  = int.TryParse(value_year, out int year);

            bool isDate = AuxiliaryMethods.IsCorrectDate(isNumberDay, isNumberMonth, isNumberYear, day, month, year);

            if (!isDate)
            {
                throw new ArgumentException("Error, incorrect date");
            }

            bool isLeapYear = AuxiliaryMethods.IsLeapYear(year);

            if (day == 1)
            {
                if (month == 1)
                {
                    day   = 31;
                    month = 12;
                    year  = year - 1;
                }
                if (month == 4 || month == 6 || month == 9 || month == 11 || month == 2 || month == 8)
                {
                    month = month - 1;
                    day   = 31;
                }
                else if (month == 3)
                {
                    if (!isLeapYear)
                    {
                        month = 2;
                        day   = 28;
                    }
                    else
                    {
                        month = 2;
                        day   = 29;
                    }
                }
                else if (month != 12 || day != 31)
                {
                    month = month - 1;
                    day   = 30;
                }
            }
            else
            {
                day = day - 1;
            }

            return(day, month, year);
        }
Пример #4
0
        /// <summary>
        /// Determines the second largest number
        /// </summary>
        public static int SecondLargest(int[] listNumber)
        {
            if (listNumber.Length == 0)
            {
                throw new Exception("The array is empty");
            }
            if (listNumber == null)
            {
                throw new Exception("The array no exist");
            }

            int[] listOrder     = AuxiliaryMethods.OrderListLessBigger(listNumber);
            int   secondLargest = listOrder.Length - 2;

            return(secondLargest);
        }
Пример #5
0
        public static (int day, int month, int year) FollowingDate(string value_day, string value_month, string value_year)
        {
            int month_next = 0;
            int day_next   = 0;
            int year_next  = 0;

            bool isNumberDay   = int.TryParse(value_day, out int day);
            bool isNumberMonth = int.TryParse(value_month, out int month);
            bool isNumberYear  = int.TryParse(value_year, out int year);

            bool isDate = AuxiliaryMethods.IsCorrectDate(isNumberDay, isNumberMonth, isNumberYear, day, month, year);

            if (!isDate)
            {
                throw new ArgumentException("Error, incorrect date");
            }

            bool isLeapYear = AuxiliaryMethods.IsLeapYear(year);

            if ((day == 30 && (month == 4 || month == 6 || month == 9 || month == 11)) || (day == 29 && month == 2) || ((day == 28 && month == 2) && !isLeapYear) || day == 31)
            {
                month_next = month + 1;
                day_next   = 1;
            }
            else
            {
                day_next   = day + 1;
                month_next = month;
            }
            if (day_next == 1 && month_next == 13)
            {
                month_next = 1;
                year_next  = year + 1;
            }
            else
            {
                year_next = year;
            }

            return(day_next, month_next, year_next);
        }
Пример #6
0
        public static int WeeksDayVersion2(string day, string month, string year)
        {
            int    rest    = 0;
            int    dayWeek = 0;
            string day1    = "1";
            string month1  = "1";
            string year1   = "999";

            int  differentTwoDates = Week4.DifferentTwoDates(day1, month1, year1, day, month, year);
            bool isFirstDateLess   = AuxiliaryMethods.FirstDateLess(day1, month1, year1, day, month, year);

            if (!isFirstDateLess)
            {
                rest    = (differentTwoDates - 1) % 7;
                dayWeek = 7 - rest;
            }
            else
            {
                rest    = (differentTwoDates % 7) + 1;
                dayWeek = rest + 1;
            }
            return(dayWeek);
        }
Пример #7
0
        public static bool IsCorrectDate(string value_day, string value_month, string value_year)
        {
            bool isNumberDay   = int.TryParse(value_day, out int day);
            bool isNumberMonth = int.TryParse(value_month, out int month);
            bool isNumberYear  = int.TryParse(value_year, out int year);

            bool isDate = AuxiliaryMethods.IsCorrectDate(isNumberDay, isNumberMonth, isNumberYear, day, month, year);

            if (!isDate)
            {
                return(false);
            }

            bool isLeapYear = AuxiliaryMethods.IsLeapYear(year);

            if ((day > 30 && (month == 4 || month == 6 || month == 9 || month == 11)) || (day > 29 && month == 2) || (day > 28 && month == 2 && year % 4 != 0) || (day == 29 && month == 2 && isLeapYear))
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Пример #8
0
        public static int DifferentTwoDates(string value_day, string value_month, string value_year, string value_day2, string value_month2, string value_year2)
        {
            int util_day          = 0;
            int util_month        = 0;
            int util_year         = 0;
            int last_day_month    = 0;
            int real_day_year     = 0;
            int cont_year_leap    = 0;
            int cont_year_no_leap = 0;

            bool isNumberDay   = int.TryParse(value_day, out int day);
            bool isNumberMonth = int.TryParse(value_month, out int month);
            bool isNumberYear  = int.TryParse(value_year, out int year);

            bool isNumberDay2   = int.TryParse(value_day2, out int second_day);
            bool isNumberMonth2 = int.TryParse(value_month2, out int second_month);
            bool isNumberYear2  = int.TryParse(value_year2, out int second_year);

            bool isDate  = AuxiliaryMethods.IsCorrectDate(isNumberDay, isNumberMonth, isNumberYear, day, month, year);
            bool isDate2 = AuxiliaryMethods.IsCorrectDate(isNumberDay2, isNumberMonth2, isNumberYear2, second_day, second_month, second_year);

            //Validating date
            if (!isDate || !isDate2)
            {
                throw new ArgumentException("Error, incorrect date");
            }

            if ((second_day <= day && second_month == month && second_year == year) || (second_year < year) || second_month < month && second_year == year)
            {   //In case the initial date is greater than the final date, it is reversed.
                util_day     = day;
                util_month   = month;
                util_year    = year;
                day          = second_day;
                month        = second_month;
                year         = second_year;
                second_day   = util_day;
                second_month = util_month;
                second_year  = util_year;
            }

            int temp_year         = year + 1;
            int first_latest_year = year;

            if (year == second_year && month == second_month)
            {
                real_day_year = second_day - day;
                return(real_day_year);
            }

            last_day_month = AuxiliaryMethods.LastDayMonthNext(month - 1, year);
            real_day_year  = last_day_month - day;

            while (temp_year < second_year)
            {
                bool isLeapYear = AuxiliaryMethods.IsLeapYear(temp_year);

                if (isLeapYear)
                {
                    temp_year++;
                    cont_year_leap++;
                }
                else
                {
                    temp_year++;
                    cont_year_no_leap++;
                }
            }

            real_day_year = real_day_year + (cont_year_no_leap * 365 + cont_year_leap * 366);

            while (true)
            {
                if (month != 12)
                {
                    last_day_month = AuxiliaryMethods.LastDayMonthNext(month, year);
                    real_day_year  = real_day_year + last_day_month;
                    month++;
                }
                if (month == 12 && first_latest_year == year)
                {
                    month             = 1;
                    year              = second_year;
                    first_latest_year = year;
                }
                if ((month == second_month - 1 || month == second_month) && first_latest_year == second_year)
                {
                    real_day_year = real_day_year + second_day;
                    return(real_day_year);
                }
                else if (month == 1 && first_latest_year == second_year)
                {
                    last_day_month = AuxiliaryMethods.LastDayMonthNext(month - 1, year);
                    real_day_year  = real_day_year + last_day_month;
                }
            }
        }