Advertisement
Timtsa

MyDateClass

May 13th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp9
  8. {
  9.  
  10.     class MyData
  11.     {
  12.         int[] datearr = { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 303, 334 };
  13.         public enum Days { Sunday, Monday, Tuesday, Wensday, Thursday, Friday, Sataday };
  14.         int day;
  15.         public int Day { get; }
  16.         int mounth;
  17.         public int Mounth { get; }
  18.         int year;
  19.         public int Year { get; }
  20.  
  21.  
  22.         public MyData(int year, int mounth, int day)
  23.         {
  24.             this.year = year;
  25.             this.mounth = mounth;
  26.             this.day = day;
  27.  
  28.         }
  29.  
  30.         public MyData()
  31.         {
  32.             this.year = 1;
  33.             this.mounth = 1;
  34.             this.day = 1;
  35.         }
  36.  
  37.         public Days WhatDay()
  38.  
  39.         {
  40.             int a = (14 - mounth) / 12;
  41.             int y = year - a;
  42.             int m = mounth + 12 * a - 2;
  43.             int result = (day + y + y / 4 - y / 100 + y / 400 + (31 * m) / 12) % 7;
  44.             Days weakDay = (Days)result;
  45.             return weakDay;
  46.  
  47.         }
  48.         static bool IsYearLeap(int year)
  49.         {
  50.             if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
  51.                 return true;
  52.             else
  53.                 return false;
  54.         }
  55.  
  56.         public int NumberOfLeapYears(int year)
  57.         {
  58.             int counter = 0;
  59.  
  60.             for (int years = 0; years <= year; years++)
  61.                 counter += MyData.IsYearLeap(years) ? 1 : 0;
  62.  
  63.             return counter;
  64.         }
  65.  
  66.         public int NumberOfDays()
  67.         {
  68.             int days;
  69.             if (this.mounth > 2)
  70.             {
  71.                 days = NumberOfLeapYears(this.year - 1);
  72.             }
  73.             else
  74.             {
  75.                 days = NumberOfLeapYears(this.year);
  76.             }
  77.             days += 365 * (this.year - 1);
  78.             days += datearr[this.mounth];
  79.             days += this.day;
  80.             return days;
  81.         }
  82.         private int DayInMonths(int month, int year)
  83.         {
  84.             int[] days = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  85.             if (IsYearLeap(year))
  86.                 days[1] = 29;
  87.             return days[month - 1];
  88.         }
  89.  
  90.         public MyData(int days)
  91.         {
  92.  
  93.             if (day < 0)
  94.                 throw new Exception("InvalidDataExeption.DataOutOfRange");
  95.             else
  96.             {
  97.                 int years = 0;
  98.                 int i = days;
  99.                 int month = 0;
  100.                 int countLeapYear = 0;
  101.                 do
  102.                 {
  103.                     if (IsYearLeap(years))
  104.                     {
  105.                         i -= 366;
  106.                         countLeapYear++;
  107.                     }
  108.                     else
  109.                         i -= 365;
  110.                     years++;
  111.                 } while (i > 1);
  112.  
  113.                 int lostDays = days - ((years - 1) * 365 + countLeapYear);
  114.                 for (month = 1; lostDays > DayInMonths(month, years); month++)
  115.                 {
  116.                     lostDays -= DayInMonths(month, years);
  117.                 }
  118.                 day = lostDays;
  119.                 this.mounth = month;
  120.                 year = years;
  121.             }
  122.  
  123.  
  124.  
  125.  
  126.         }
  127.  
  128.         public static int operator - (MyData date, MyData other)
  129.         {
  130.             int sumOfDaysDate = date.NumberOfDays();
  131.             int sumOfDaysOther = other.NumberOfDays();
  132.             return sumOfDaysDate - sumOfDaysOther;
  133.         }
  134.  
  135.         public static MyData operator + (MyData date, int days)
  136.         {
  137.             int sumOfDays = date.NumberOfDays() + days;
  138.              MyData newdate = new MyData(sumOfDays);
  139.             return newdate;
  140.         }
  141.  
  142.         public static MyData operator ++ (MyData date)
  143.         {
  144.             int days = date.NumberOfDays();
  145.                 days++;
  146.             MyData newdate = new MyData(days);
  147.             return newdate;
  148.  
  149.         }
  150.         public static MyData operator --(MyData date)
  151.         {
  152.             int days = date.NumberOfDays();
  153.             days--;
  154.             MyData newdate = new MyData(days);
  155.             return newdate;
  156.  
  157.         }
  158.  
  159.         public static bool operator > (MyData date, MyData other)
  160.         {
  161.             int myDate = date.NumberOfDays();
  162.             int otherDate = other.NumberOfDays();
  163.             return myDate > otherDate;
  164.  
  165.         }
  166.         public static bool operator <(MyData date, MyData other)
  167.         {
  168.             int myDate = date.NumberOfDays();
  169.             int otherDate = other.NumberOfDays();
  170.             return myDate < otherDate;
  171.  
  172.         }
  173.  
  174.  
  175.         public static bool operator == (MyData date, MyData other)
  176.         {
  177.             int myDate = date.NumberOfDays();
  178.             int otherDate = other.NumberOfDays();
  179.             return myDate == otherDate;
  180.  
  181.         }
  182.         public static bool operator !=(MyData date, MyData other)
  183.         {
  184.             int myDate = date.NumberOfDays();
  185.             int otherDate = other.NumberOfDays();
  186.             return myDate != otherDate;
  187.  
  188.  
  189.         }
  190.         public override string ToString()
  191.         {
  192.             return $"{day}, {mounth}, {year}, {WhatDay()};";
  193.         }
  194.     }
  195.     class Program
  196.     {
  197.  
  198.  
  199.         static void Main(string[] args)
  200.         {
  201.             MyData calendar = new MyData(2019, 4, 27);
  202.             Console.WriteLine(calendar.ToString());
  203.             Console.WriteLine(calendar.NumberOfDays());
  204.  
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement