⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customdate.cs

📁 c sharp program for every one once in a awhile
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace Quiz_2
{
    class CustomDate
    {
        //Private Fields//
        private int day;
        private int month;
        private int year;



        //Public Properties//
        public int DAY
        {
            get
            {
                return day;
            }
            set 
            {
                if (CheckMonth(month) == 1 && value <= 31)
                {
                    day = value;
                }
                else if (CheckMonth(month) == 2 && value <= 30)
                {
                    day = value;
                }
                else if (CheckMonth(month) == 3 && year % 4 == 0 && value <= 29)
                {
                    day = value;
                }
                else if (CheckMonth(month) == 3 && year % 4 != 0 && value <= 28)
                {
                    day = value;
                }
                
                else throw new Exception("Incorrect Date!");
            }
        }

        public int MONTH
        {
            get
            {
                return month;
            }
            set
            {
                if (value >= 1 && value <= 12) month = value;
                else throw new Exception("Incorrect month!");
            }
        }

        public int YEAR
        {
            get
            {
                return year;
            }
            set
            {
                if (value >= 1800 && value <= 9999) year = value;
                else throw new Exception("Incorrect Year! Year Should be between 1800 - 9999");
            }
        }



        //Constructors//
        public CustomDate():this(1,1,1800)
        {

        }
        public CustomDate(int day,int month,int year)
        {            
            this.YEAR = year;            
            this.MONTH = month;
            this.DAY = day;
        }
        public CustomDate(CustomDate obj):this(obj.day,obj.month,obj.year)
        {
            
        }



        //Private Mehtods//
        private static int CheckMonth(int month)
        {
            if (month == 2) return 3;
            if (month ==  4|| month == 6 || month == 9 || month == 11 ) return 2;
            else return 1;
        }

        private static int DateConverter(CustomDate obj)
        {
            int Year = obj.year - 1;
            int Month = obj.month - 1;
            int Day = obj.day;

            int days = Year * 365;

            for (int i = 1; i <= Month; i++)
            {
                if (CheckMonth(i) == 1)
                {
                    days += 31;                    
                }
                else if (CheckMonth(i) == 2)
                {
                    days += 30;
                }
                else
                {
                    days += 28;
                }
            }
            days += Day;
            if (Month >= 2) days += ((Year + 1) / 4);
            else days += (Year  / 4);
            return days;
        }

        private static CustomDate DayConverter(int totalDays)
        {
            
            int day = 0;
            int month = 1;
            int year = 1;

            while (totalDays >= (365 + 365 + 365 + 366))
            {
                totalDays -= (365 + 365 + 365 + 366);
                year += 4;
            }
            while (totalDays >= 365)
            {
                totalDays -= 365;
                year++;
            }
            while (totalDays >= 28)
            {
                if (CheckMonth(month) == 1)
                {
                    totalDays-=31;
                    month++;
                    continue;
                }
                if (CheckMonth(month) == 2)
                {
                    totalDays -= 30;
                    month++;
                    continue;
                }
                if (CheckMonth(month) == 3)
                {
                    if (year % 4 == 0)
                    {
                        totalDays -= 29;
                        month++;
                    }
                    else
                    {
                        totalDays -= 28;
                        month++;
                    }
                }              
            }

            if (totalDays <= 0)
            {
                month--;
                if (month == 0)
                {
                    month = 12;
                    year--;
                }
                if (CheckMonth(month) == 1)
                {
                    totalDays += 31;
                }
                if (CheckMonth(month) == 2)
                {
                    totalDays += 30;
                }
                if (CheckMonth(month) == 3)
                {
                    if (year % 4 == 0)
                    {
                        totalDays += 29;                        
                    }
                    else
                    {
                        totalDays += 28;
                    }
                }
            }

            day = totalDays;
            CustomDate obj = new CustomDate(day, month, year);
            
            return obj;
        }



        //Public Methods//
        public void PrintCustomDate()
        {
            Console.WriteLine("{0:D2}/{1:D2}/{2:D4}",day,month,year);
        }

        public void Add(int totalDays)
        {            
            totalDays += DateConverter(this);
            CustomDate newDate = DayConverter(totalDays);
            this.YEAR = newDate.year;
            this.MONTH = newDate.month;
            this.DAY = newDate.day;                        
        }

        public void Add(CustomDate obj)
        {
            int totalDays = DateConverter(obj);
            totalDays += DateConverter(this);
            CustomDate newDate = DayConverter(totalDays);
            this.YEAR = newDate.year;
            this.MONTH = newDate.month;
            this.DAY = newDate.day;  

        }

        public void Sub(int totalDays)
        {
            totalDays = DateConverter(this) - totalDays;
            if (totalDays < 0) throw new Exception("Days in negative not possible");
            CustomDate newDate = DayConverter(totalDays);
            this.YEAR = newDate.year;
            this.MONTH = newDate.month;
            this.DAY = newDate.day;  
        }

        public void Sub(CustomDate obj)
        {
            int totalDays = DateConverter(obj);            
            totalDays = DateConverter(this) - totalDays;
            if (totalDays < 0) throw new Exception("Days in negative not possible");
            CustomDate newDate = DayConverter(totalDays);
            this.YEAR = newDate.year;
            this.MONTH = newDate.month;
            this.DAY = newDate.day;  

        }

        public static CustomDate Parse(string date)
        {
            int day = int.Parse(date.Substring(0, date.IndexOf('/')));
            int month = int.Parse(date.Substring(date.IndexOf('/')+1, date.Length - date.LastIndexOf('/')- date.IndexOf('/')-1));
            int year = int.Parse(date.Substring(date.LastIndexOf('/')+1));
            return new CustomDate(day, month, year);
        }

        public static CustomDate operator +(CustomDate obj1, CustomDate obj2)
        {
            int totalDays = 0;
            totalDays = DateConverter(obj1);
            totalDays += DateConverter(obj2);
            return DayConverter(totalDays);
        }

        public static CustomDate operator +(CustomDate obj1, int totalDays)
        {
            totalDays += DateConverter(obj1);
            return DayConverter(totalDays);
        }

        public static CustomDate operator +(int totalDays, CustomDate obj1)
        {
            totalDays += DateConverter(obj1);
            return DayConverter(totalDays);
        }

        public static CustomDate operator -(CustomDate obj1, CustomDate obj2)
        {
            int totalDays = 0;
            totalDays = DateConverter(obj1);
            totalDays -= DateConverter(obj2);
            if (totalDays < 0) throw new Exception("Days in negative not possible");
            return DayConverter(totalDays);
        }

        public static CustomDate operator -(CustomDate obj1, int totalDays)
        {
            totalDays -= DateConverter(obj1);
            if (totalDays < 0) throw new Exception("Days in negative not possible");
            return DayConverter(totalDays);
        }

        public static CustomDate operator -(int totalDays, CustomDate obj1)
        {
            totalDays -= DateConverter(obj1);
            if (totalDays < 0) throw new Exception("Days in negative not possible");
            return DayConverter(totalDays);
        }

        public static CustomDate operator ++(CustomDate obj1)
        {
            int totalDays = 0;
            totalDays = DateConverter(obj1) + 1;
            return DayConverter(totalDays);
        }

        public static CustomDate operator --(CustomDate obj1)
        {
            int totalDays = 0;
            totalDays = DateConverter(obj1) - 1;
            if (totalDays < 0) throw new Exception("Days in negative not possible");
            return DayConverter(totalDays);
        }

        public static bool operator ==(CustomDate obj1, CustomDate obj2)
        {
            int totalDays1 = DateConverter(obj1);
            int totalDays2 = DateConverter(obj2);
            if (totalDays1 == totalDays2) return true;
            else return false;
        }

        public static bool operator !=(CustomDate obj1, CustomDate obj2)
        {
            int totalDays1 = DateConverter(obj1);
            int totalDays2 = DateConverter(obj2);
            if (totalDays1 != totalDays2) return true;
            else return false;
        }

        

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -