Guest User

Untitled

a guest
Jun 24th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4.  
  5. class Date
  6. {
  7. public enum Month
  8. {
  9. Jan = 0,
  10. Feb,
  11. Mar,
  12. Apr,
  13. May,
  14. Jun,
  15. Jul,
  16. Aug,
  17. Sep,
  18. Oct,
  19. Nov,
  20. Dec
  21. };
  22.  
  23. private int year;
  24. private int month;
  25. private int day;
  26.  
  27. public Date()
  28. {
  29. year = 1980;
  30. month = (int)Month.Jan;
  31. day = 0;
  32. }
  33.  
  34. public Date(int year, int month, int day)
  35. {
  36. if (year < 0)
  37. {
  38. throw new System.ArgumentOutOfRangeException("Year should be non-negative");
  39. }
  40.  
  41. if (day <= 0 || day > 365)
  42. {
  43. throw new System.ArgumentOutOfRangeException("Bad day range");
  44. }
  45.  
  46. this.year = year;
  47. this.month = month;
  48. this.day = day;
  49. }
  50.  
  51. ~Date()
  52. {
  53. // Nothing to release
  54. }
  55.  
  56. public int GetYear() { return year; }
  57. public int GetMonth() { return month; }
  58. public int GetDay() { return day; }
  59.  
  60. private static int DaysPerMonth(int mon)
  61. {
  62. switch ((Month)mon)
  63. {
  64. case Month.Jan: return 31;
  65. case Month.Feb: return 28;
  66. case Month.Mar: return 31;
  67. case Month.Apr: return 30;
  68. case Month.May: return 31;
  69. case Month.Jun: return 30;
  70. case Month.Jul: return 31;
  71. case Month.Aug: return 31;
  72. case Month.Sep: return 30;
  73. case Month.Oct: return 31;
  74. case Month.Nov: return 30;
  75. case Month.Dec: return 31;
  76. default: throw new System.IndexOutOfRangeException();
  77. };
  78. }
  79.  
  80. private const ulong SecondsPerDay = 86400;
  81.  
  82. private ulong ToSeconds()
  83. {
  84. ulong res = (ulong)year * SecondsPerDay * 365;
  85. res += (((ulong)year / 4) * SecondsPerDay); // Include 1 day per leap year
  86.  
  87. for (int i = 0; i < month; ++i)
  88. {
  89. res += (ulong)DaysPerMonth(i) * SecondsPerDay;
  90. }
  91.  
  92. res += (ulong)day * SecondsPerDay;
  93. return res;
  94. }
  95.  
  96. private Date FromSeconds(ulong seconds)
  97. {
  98. // We don't know how many leap years are encoded here
  99. // But we know that each 4 years have 1 leap year, so we can use total number of seconds _per 4 years_
  100. int yearBy4 = (int)(seconds / (SecondsPerDay * (365 * 4 + 1)));
  101. seconds -= (ulong)yearBy4 * (SecondsPerDay * (365 * 4 + 1));
  102.  
  103. // Remaining years not divisible by 4. Non-leap by definition
  104. int remainingYear = (int)(seconds / (SecondsPerDay * 365));
  105. seconds -= (ulong)remainingYear * SecondsPerDay * 365;
  106.  
  107. int year = yearBy4 * 4 + remainingYear;
  108.  
  109. int month = 0;
  110. for (int i = 0; i < (int)Month.Dec; ++i)
  111. {
  112. ulong secondsInMonth = (ulong)DaysPerMonth(i) * SecondsPerDay;
  113. if (seconds >= secondsInMonth)
  114. {
  115. seconds -= secondsInMonth;
  116. month++;
  117. } else
  118. {
  119. break;
  120. }
  121. }
  122.  
  123. int day = (int)(seconds / SecondsPerDay);
  124. return new Date(year, month, day);
  125. }
  126.  
  127. public Date AddDays(int days)
  128. {
  129. if (days > 31)
  130. {
  131. throw new System.ArgumentOutOfRangeException();
  132. }
  133.  
  134. return FromSeconds(ToSeconds() + ((ulong)days * SecondsPerDay));
  135. }
  136.  
  137. public static bool operator == (Date a, Date b)
  138. {
  139. return a.ToSeconds() == b.ToSeconds();
  140. }
  141.  
  142. public static bool operator != (Date a, Date b)
  143. {
  144. return !(a == b);
  145. }
  146.  
  147. public static Date operator - (Date a, Date b)
  148. {
  149. ulong a_sec = a.ToSeconds();
  150. ulong b_sec = b.ToSeconds();
  151.  
  152. if (a_sec < b_sec)
  153. {
  154. throw new System.ArgumentOutOfRangeException();
  155. }
  156.  
  157. Date res = new Date();
  158. return res.FromSeconds(a_sec - b_sec);
  159. }
  160.  
  161. // DD:MM:YYYY
  162. public static Date ReadFromConsole()
  163. {
  164. string[] elements = Console.ReadLine().Split(':');
  165. if (elements.Count() != 3)
  166. {
  167. Console.WriteLine("Bad date format");
  168. throw new System.InvalidProgramException();
  169. }
  170.  
  171. int day = int.Parse(elements[0]);
  172. int month = int.Parse(elements[1]);
  173. int year = int.Parse(elements[2]);
  174.  
  175. // Constructor will validate arguments
  176. return new Date(year, month, day);
  177. }
  178.  
  179. public int NumberOfMatches(List<Date> dates, Date key)
  180. {
  181. int res = 0;
  182.  
  183. foreach (Date d in dates)
  184. {
  185. if(d == key)
  186. {
  187. ++res;
  188. }
  189. }
  190.  
  191. return res;
  192. }
  193.  
  194. public void WriteToConsole()
  195. {
  196. Console.WriteLine("{0}:{1}:{2}", day, month, year);
  197. }
  198. };
  199.  
  200. class App
  201. {
  202. static void Main(string[] args)
  203. {
  204. global::Date date = new global::Date(2018, 6, 23);
  205. date.WriteToConsole();
  206.  
  207. global::Date added = date.AddDays(1);
  208. added.WriteToConsole();
  209.  
  210. Debug.Assert(date != added);
  211.  
  212. Debug.Assert(date.GetYear() == added.GetYear());
  213. Debug.Assert(date.GetMonth() == added.GetMonth());
  214. Debug.Assert(date.GetDay() == added.GetDay() - 1);
  215.  
  216. Console.ReadKey();
  217. }
  218. }
Add Comment
Please, Sign In to add comment