2016-03-05 158 views
-1

我在第一眼看上去做了相当简单的练习,但最终变得相当困难。我需要将日期作为输入,并将日期添加到它也从用户输入中获取。我已经做了一些功能和一些简单的计算,现在我从日期(01,01,0001为零)中获取所有日期,例如:C#在指定日期添加日期

第一个1月第二年(01.01.0002)+ 0天是相等的到365天它也计算正确,如果我添加一些天:01.01.0002 + 12天= 387 ..它也计算闰年。现在,我已经totalDays我只是需要将其转换成一个正常的日/月/年的格式..

IM不允许使用DATETIME

这里是我的代码:

private static int[] daysPerMonth = new int[12]; 
    private static int days; 
    private static int months; 
    private static int years; 
    private static int add; 

    private static void Main() 
    { 
     Console.Write("Enter day : "); 
     int.TryParse(Console.ReadLine(), out days); 
     Console.Write("Enter Month : "); 
     int.TryParse(Console.ReadLine(), out months); 
     Console.Write("Enter Year : "); 
     int.TryParse(Console.ReadLine(), out years); 
     Console.Write("Enter days to add : "); 
     int.TryParse(Console.ReadLine(), out add); 
     int totalDays = GetTotalDays(new[] {days, months, years}); 
     totalDays += add; 
     TransformIntoDate(totalDays); 

     Console.ReadKey(); 
    } 

    private static void TransformIntoDate(int inputDays) 
    { 

    } 
    private static int GetTotalDays(IReadOnlyList<int> date) 
    { 
     int totalDays = 0; 
     for (int i = date[2]; i > 1; i--) 
     { 
      if (IsLeap(i)) 
      { 
       daysPerMonth = new[] {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
       totalDays += 366; 
      } 
      else 
      { 
       daysPerMonth = new[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
       totalDays += 365; 
      } 
     } 
     for (int i = 1; i <= date[1]; i++) 
     { 
      if (i == date[1]) 
      { 
       totalDays += date[0] - 1; 
      } 
      else 
      { 
       totalDays += daysPerMonth[i]; 
      } 
     } 
     return totalDays; 
    } 

    private static bool IsLeap(int year) 
    { 
     if (year%400 == 0) return true; 
     return (year%4 == 0) && (year%100 != 0); 
    } 
+5

* GetTotalDays(new [] {days,months,years})*请勿。请勿将三个完全断开连接的数字它们是三个参数,它们应该有名称,比如* year *,* month *,* day *,不是'date [2]','date [1]','date [0]' – xanatos

+0

我是计划为他们创建枚举,但我没有 – KOPEUE

+0

创建一个类或结构来保存数据并进行计算。也许你不能*使用* DateTime,但你可以模仿它做什么 – Plutonix

回答

1

有是“保存”日期的两种方法:分别保存年份,月份,日期或从“0点”保存总天数(或小时或分钟或秒或毫秒...选择此处的度量单位)。 .NET的DateTime例如使用100纳秒作为Tick,0001年1月1日作为“0点”。 Unix使用1970年1月1日以来的秒数。显然,.NET和Unix的方式在内存中更加紧凑(单个值来保存),如果你想添加/减少数量(简单地添加/减少它),它非常有用。问题在于将此内部号码转换为年/月/日或将年/月/日转换为此号码会更复杂。

如何年/月/日,以内部号码可以做一个简单的例子:

public class MyDate 
{ 
    public int TotalDaysFrom00010101 { get; private set; } 

    private const int DaysIn400YearCycle = 365 * 400 + 97; 
    private const int DaysIn100YearCycleNotDivisibleBy400 = 365 * 100 + 24; 
    private const int DaysIn4YearCycle = 365 * 4 + 1; 

    private static readonly int[] DaysPerMonthNonLeap = new[] 
    { 
     31, 
     31 + 28, 
     31 + 28 + 31, 
     31 + 28 + 31 + 30, 
     31 + 28 + 31 + 30 + 31, 
     31 + 28 + 31 + 30 + 31 + 30, 
     31 + 28 + 31 + 30 + 31 + 30 + 31, 
     31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, 
     31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, 
     31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, 
     31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, 
     31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 // Useless 
    }; 

    private static readonly int[] DaysPerMonthLeap = new[] 
    { 
     31, 
     31 + 29, 
     31 + 29 + 31, 
     31 + 29 + 31 + 30, 
     31 + 29 + 31 + 30 + 31, 
     31 + 29 + 31 + 30 + 31 + 30, 
     31 + 29 + 31 + 30 + 31 + 30 + 31, 
     31 + 29 + 31 + 30 + 31 + 30 + 31 + 31, 
     31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30, 
     31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, 
     31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, 
     31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 // Useless 
    }; 

    public static bool IsLeap(int year) 
    { 
     if (year % 400 == 0) return true; 
     return (year % 4 == 0) && (year % 100 != 0); 
    } 

    public void SetDate(int year, int month, int day) 
    { 
     TotalDaysFrom00010101 = 0; 

     { 
      int year2 = year - 1; 

      // Full 400 year cycles 
      TotalDaysFrom00010101 += (year2/400) * DaysIn400YearCycle; 
      year2 %= 400; 

      // Remaining 100 year cycles (0...3) 
      if (year2 >= 100) 
      { 
       year2 -= 100; 
       TotalDaysFrom00010101 += DaysIn100YearCycleNotDivisibleBy400; 

       if (year2 >= 100) 
       { 
        year2 -= 100; 
        TotalDaysFrom00010101 += DaysIn100YearCycleNotDivisibleBy400; 

        if (year2 >= 100) 
        { 
         year2 -= 100; 
         TotalDaysFrom00010101 += DaysIn100YearCycleNotDivisibleBy400; 
        } 
       } 
      } 

      // Full 4 year cycles 
      TotalDaysFrom00010101 += (year2/4) * DaysIn4YearCycle; 
      year2 %= 4; 

      // Remaining non-leap years 
      TotalDaysFrom00010101 += year2 * 365; 
     } 

     // Days from the previous month 
     if (month > 1) 
     { 
      // -2 is because -1 is for the 1 January == 0 index, plus -1 
      // because we must add only the previous full months here. 
      // So if the date is 1 March 2016, we must add the days of 
      // January + February, so month 3 becomes index 1. 
      TotalDaysFrom00010101 += DaysPerMonthNonLeap[month - 2]; 

      if (month > 2 && IsLeap(year)) 
      { 
       TotalDaysFrom00010101 += 1; 
      } 
     } 

     // Days (note that the "instant 0" in this class is day 1, so 
     // we must add day - 1) 
     TotalDaysFrom00010101 += day - 1; 
    } 

    public void GetDate(out int year, out int month, out int day) 
    { 
     int days = TotalDaysFrom00010101; 

     // year 
     { 
      year = days/DaysIn400YearCycle * 400; 
      days %= DaysIn400YearCycle; 

      if (days >= DaysIn100YearCycleNotDivisibleBy400) 
      { 
       year += 100; 
       days -= DaysIn100YearCycleNotDivisibleBy400; 

       if (days >= DaysIn100YearCycleNotDivisibleBy400) 
       { 
        year += 100; 
        days -= DaysIn100YearCycleNotDivisibleBy400; 

        if (days >= DaysIn100YearCycleNotDivisibleBy400) 
        { 
         year += 100; 
         days -= DaysIn100YearCycleNotDivisibleBy400; 
        } 
       } 
      } 

      year += days/DaysIn4YearCycle * 4; 
      days %= DaysIn4YearCycle; 

      // Special case: 31 dec of a leap year 
      if (days != 1460) 
      { 
       year += days/365; 
       days %= 365; 
      } 
      else 
      { 
       year += 3; 
       days = 365; 
      } 

      year++; 
     } 

     // month 
     { 
      bool isLeap = IsLeap(year); 

      int[] daysPerMonth = isLeap ? DaysPerMonthLeap : DaysPerMonthNonLeap; 

      for (month = 0; month < daysPerMonth.Length; month++) 
      { 
       if (daysPerMonth[month] > days) 
       { 
        if (month > 0) 
        { 
         days -= daysPerMonth[month - 1]; 
        } 

        break; 
       } 
      } 

      month++; 
     } 

     // day 
     { 
      day = days; 
      day++; 
     } 
    } 

    public void AddDays(int days) 
    { 
     TotalDaysFrom00010101 += days; 
    } 
} 

这里的关键是,我们知道,有400年“周期”,而每一个这些“周期”有365 * 400 + 97天。扣除这些“周期”后,有100年的“周期”较短,每一天有365 * 100 + 24天。然后我们有4年的“周期”,每个周期为365 * 4 + 3天,再加上剩下的年份(0-3),每个周期365天。

在为“所有前几年”添加了日子后,我们可以添加“前几个月”的日子。在这里,我们必须考虑今年是闰年的可能性。

最后我们添加选定日期。

如何编写GetDate()留作练习。

现在...如何检查结果是否正确?我们可以写一个基于该DateTime实施一些单元测试...喜欢的东西:

var date = default(DateTime); 
var endDate = new DateTime(2017, 1, 1); 

while (date < endDate) 
{ 
    int year = date.Year; 
    int month = date.Month; 
    int day = date.Day; 

    int totalDays = (int)(date - default(DateTime)).TotalDays; 

    var md = new MyDate(); 
    md.SetDate(year, month, day); 

    if (totalDays != md.TotalDaysFrom00010101) 
    { 
     Console.WriteLine("{0:d}: {1} vs {2}", date, totalDays, md.TotalDaysFrom00010101); 
    } 

    int year2, month2, day2; 
    md.GetDate(out year2, out month2, out day2); 

    if (year != year2 || month != month2 || day != day2) 
    { 
     Console.WriteLine("{0:d}: {1:D4}-{2:D2}-{3:D2} vs {4:D4}-{5:D2}-{6:D2}", date, year, month, day, year2, month2, day2); 
    } 

    date = date.AddDays(1); 
} 

(我知道这是不是一个单元测试......但展示了如何使用DateTime做一个比较)

请注意,这是一个如何实现它的例子。我不会这样实施它。我会做一个不变的structDateTime,用构造函数而不是SetDate()。但是你的观点似乎是一个练习,而且我认为应该采取小步骤:首先正确地构建一些东西,然后使它“正式地正确”。所以第一步是建立一个正确的,然后你可以正确地封装在一个更正式的数据结构。这个小样本甚至缺少一些参数验证:您可以SetDate(-1, 13, 32) :-)

GetDate()构建起来相当复杂。比我想象的要多得多。最后,我能够理解如何编写它。请注意,最终它与微软的实施非常相似(请参阅GetDatePart(int part)

+0

谢谢你的深入答案,但你离开了我需要转换成一年的日子到我的一部分吗?我仍然试图了解代码(即时通讯初学者在C#)和IM有点困惑,我们从SetDate得到的是几天?我在我的问题中也发布了类似的实现,并且我的问题正在将日期转换为日/月/年 – KOPEUE

+0

SetDate()完成且工作正常。 AddDays()是完整的并且正常工作。缺少的是将内部日数('TotalDaysFrom00010101')转换为年/月/日(在'GetDate()'中完成的事情)。但是,如果有一个工作的SetDate(),它应该是可行的(即使因为它很容易测试)...执行一个日期的SetDate()并尝试用GetDate() – xanatos

+0

为什么GetDate将年份,月份,日期作为参数不应该只需要几天并且回馈年,月,日? – KOPEUE