MainWindow.axaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Avalonia.Controls;
  2. using System;
  3. using System.Data;
  4. using Avalonia.Interactivity;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace AvaloniaApplication3
  9. {
  10. // Ñòðóêòóðà äëÿ õðàíåíèÿ âîçðàñòà
  11. struct Age
  12. {
  13. public int Years;
  14. public int Months;
  15. public int Days;
  16. public DayOfWeek BirthDayOfWeek;
  17. public int BirthdaysOnSameWeekday;
  18. public List<int> LeapYears;
  19. }
  20. public partial class MainWindow : Window
  21. {
  22. private DatePicker dp;
  23. private TextBlock zodiacTextBlock;
  24. private TextBlock chineseZodiacTextBlock;
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. dp = this.FindControl<DatePicker>("DatePicker");
  29. var button = this.FindControl<Button>("Button");
  30. var zodiacButton = this.FindControl<Button>("ZodiacButton");
  31. var chineseZodiacButton = this.FindControl<Button>("ChineseZodiacButton");
  32. zodiacTextBlock = this.FindControl<TextBlock>("ZodiacTextBlock");
  33. chineseZodiacTextBlock = this.FindControl<TextBlock>("ChineseZodiacTextBlock");
  34. button.Click += Button_Click;
  35. zodiacButton.Click += ZodiacButton_Click;
  36. chineseZodiacButton.Click += KinaiZodiacButton_Click;
  37. }
  38. private void Button_Click(object source, RoutedEventArgs args)
  39. {
  40. // ïîëó÷àåì äàòó ðîæäåíèÿ
  41. var birthDate = dp.SelectedDate;
  42. if (birthDate == null)
  43. {
  44. var errorTextBlock = this.FindControl<TextBlock>("ErrorTextBlock");
  45. errorTextBlock.Text = "Âû ââåëè íåñóùåñòâóþùóþ äàòó";
  46. return;
  47. }
  48. // âû÷èñëåíèå âîçðàñòà
  49. var birthDateTime = birthDate.Value.DateTime;
  50. var age = CalculateAge(birthDateTime);
  51. var resultTextBlock = this.FindControl<TextBlock>("ResultTextBlock");
  52. resultTextBlock.Text = $"Âàì {age.Years} ëåò, {age.Months} ìåñÿöåâ, {age.Days} äíåé. \n" +
  53. $"Âû ðîäèëèñü â {GetRussianDayOfWeek(age.BirthDayOfWeek)}. \n" + // Èçìåíåíèå: âûâîä äíÿ íåäåëè íà ðóññêîì
  54. $"Êîëè÷åñòâî äíåé ðîæäåíèÿ, êîòîðûå âû îòïðàçäíîâàëè â ýòîò æå äåíü íåäåëè: {age.BirthdaysOnSameWeekday}. \n" +
  55. $"Âèñîêîñíûå ãîäû: {string.Join(", ", age.LeapYears)}.";
  56. }
  57. private void ZodiacButton_Click(object sender, RoutedEventArgs e)
  58. {
  59. var birthDate = dp.SelectedDate;
  60. if (birthDate == null)
  61. {
  62. var errorTextBlock = this.FindControl<TextBlock>("ErrorTextBlock");
  63. errorTextBlock.Text = "Âû ââåëè íåñóùåñòâóþùóþ äàòó";
  64. return;
  65. }
  66. // çíàê çîäèàêà
  67. var birthDateTime = birthDate.Value.DateTime;
  68. int year = birthDateTime.Year;
  69. int month = birthDateTime.Month;
  70. int day = birthDateTime.Day;
  71. string Zod = GetZodiacSign(month, day);
  72. zodiacTextBlock.Text = $"Çíàê çîäèàêà: {Zod}";
  73. zodiacTextBlock.IsVisible = true;
  74. chineseZodiacTextBlock.Text = "";
  75. chineseZodiacTextBlock.IsVisible = false;
  76. }
  77. private void KinaiZodiacButton_Click(object sender, RoutedEventArgs e)
  78. {
  79. var birthDate = dp.SelectedDate;
  80. if (birthDate == null)
  81. {
  82. var errorTextBlock = this.FindControl<TextBlock>("ErrorTextBlock");
  83. errorTextBlock.Text = "Âû ââåëè íåñóùåñòâóþùóþ äàòó";
  84. return;
  85. }
  86. // çíàê âîñòî÷íîãî êàëåíäàðÿ
  87. var birthDateTime = birthDate.Value.DateTime;
  88. int year = birthDateTime.Year;
  89. string[] ZnakV = { "Êðûñà", "Áûê", "Òèãð", "Êðîëèê", "Äðàêîí", "Çìåÿ", "Ëîøàäü", "Êîçà", "Îáåçüÿíà", "Ïåòóõ", "Ñîáàêà", "Ñâèíüÿ" };
  90. int index = (year - 4) % 12;
  91. chineseZodiacTextBlock.Text = $"Çíàê âîñòî÷íîãî êàëåíäàðÿ: {ZnakV[index]}";
  92. chineseZodiacTextBlock.IsVisible = true;
  93. zodiacTextBlock.Text = "";
  94. zodiacTextBlock.IsVisible = false;
  95. }
  96. private Age CalculateAge(DateTime birthDate)
  97. {
  98. var today = DateTime.Now;
  99. var years = today.Year - birthDate.Year;
  100. var months = today.Month - birthDate.Month;
  101. var days = today.Day - birthDate.Day;
  102. var birthDayOfWeek = birthDate.DayOfWeek;
  103. if (months < 0 || (months == 0 && days < 0))
  104. {
  105. years--;
  106. months += 12;
  107. }
  108. if (days < 0)
  109. {
  110. months--;
  111. days += DateTime.DaysInMonth(today.Year, today.Month);
  112. }
  113. // êîëè÷åñòâî äíåé ðîæäåíèÿ â ýòîò æå äåíü íåäåëè
  114. int birthdaysOnSameWeekday = 0;
  115. var currentYear = birthDate.Year;
  116. while (currentYear < today.Year)
  117. {
  118. var potentialBirthday = new DateTime(currentYear, birthDate.Month, birthDate.Day);
  119. if (potentialBirthday.DayOfWeek == birthDayOfWeek)
  120. {
  121. birthdaysOnSameWeekday++;
  122. }
  123. currentYear++;
  124. }
  125. // âèñîêîñíûå ãîäû
  126. var leapYears = Enumerable.Range(birthDate.Year + 1, today.Year - birthDate.Year - 1)
  127. .Where(year => DateTime.IsLeapYear(year))
  128. .ToList();
  129. return new Age { Years = years, Months = months, Days = days, BirthDayOfWeek = birthDayOfWeek, BirthdaysOnSameWeekday = birthdaysOnSameWeekday, LeapYears = leapYears };
  130. }
  131. private string GetZodiacSign(int month, int day)
  132. {
  133. switch (month)
  134. {
  135. case 3:
  136. return day >= 21 ? "Îâåí" : "Ðûáû";
  137. case 4:
  138. return day >= 20 ? "Òåëåö" : "Îâåí";
  139. case 5:
  140. return day >= 21 ? "Áëèçíåöû" : "Òåëåö";
  141. case 6:
  142. return day >= 21 ? "Ðàê" : "Áëèçíåöû";
  143. case 7:
  144. return day >= 23 ? "Ëåâ" : "Ðàê";
  145. case 8:
  146. return day >= 23 ? "Äåâà" : "Ëåâ";
  147. case 9:
  148. return day >= 23 ? "Âåñû" : "Äåâà";
  149. case 10:
  150. return day >= 23 ? "Ñêîðïèîí" : "Âåñû";
  151. case 11:
  152. return day >= 22 ? "Ñòðåëåö" : "Ñêîðïèîí";
  153. case 12:
  154. return day >= 22 ? "Êîçåðîã" : "Ñòðåëåö";
  155. case 1:
  156. return day >= 20 ? "Âîäîëåé" : "Êîçåðîã";
  157. case 2:
  158. return day >= 19 ? "Ðûáû" : "Âîäîëåé";
  159. default:
  160. return "Íåèçâåñòíûé çíàê";
  161. }
  162. }
  163. private string GetRussianDayOfWeek(DayOfWeek dayOfWeek)
  164. {
  165. switch (dayOfWeek)
  166. {
  167. case DayOfWeek.Monday: return "Ïîíåäåëüíèê";
  168. case DayOfWeek.Tuesday: return "Âòîðíèê";
  169. case DayOfWeek.Wednesday: return "Ñðåäà";
  170. case DayOfWeek.Thursday: return "×åòâåðã";
  171. case DayOfWeek.Friday: return "Ïÿòíèöà";
  172. case DayOfWeek.Saturday: return "Ñóááîòà";
  173. case DayOfWeek.Sunday: return "Âîñêðåñåíüå";
  174. default: return "Íåèçâåñòíûé äåíü";
  175. }
  176. }
  177. }
  178. }