123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using Avalonia.Controls;
- using System;
- using System.Data;
- using Avalonia.Interactivity;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- namespace AvaloniaApplication3
- {
- // Ñòðóêòóðà äëÿ õðàíåíèÿ âîçðàñòà
- struct Age
- {
- public int Years;
- public int Months;
- public int Days;
- public DayOfWeek BirthDayOfWeek;
- public int BirthdaysOnSameWeekday;
- public List<int> LeapYears;
- }
- public partial class MainWindow : Window
- {
- private DatePicker dp;
- private TextBlock zodiacTextBlock;
- private TextBlock chineseZodiacTextBlock;
- public MainWindow()
- {
- InitializeComponent();
- dp = this.FindControl<DatePicker>("DatePicker");
- var button = this.FindControl<Button>("Button");
- var zodiacButton = this.FindControl<Button>("ZodiacButton");
- var chineseZodiacButton = this.FindControl<Button>("ChineseZodiacButton");
- zodiacTextBlock = this.FindControl<TextBlock>("ZodiacTextBlock");
- chineseZodiacTextBlock = this.FindControl<TextBlock>("ChineseZodiacTextBlock");
- button.Click += Button_Click;
- zodiacButton.Click += ZodiacButton_Click;
- chineseZodiacButton.Click += KinaiZodiacButton_Click;
- }
- private void Button_Click(object source, RoutedEventArgs args)
- {
- // ïîëó÷àåì äàòó ðîæäåíèÿ
- var birthDate = dp.SelectedDate;
- if (birthDate == null)
- {
- var errorTextBlock = this.FindControl<TextBlock>("ErrorTextBlock");
- errorTextBlock.Text = "Âû ââåëè íåñóùåñòâóþùóþ äàòó";
- return;
- }
- // âû÷èñëåíèå âîçðàñòà
- var birthDateTime = birthDate.Value.DateTime;
- var age = CalculateAge(birthDateTime);
- var resultTextBlock = this.FindControl<TextBlock>("ResultTextBlock");
- resultTextBlock.Text = $"Âàì {age.Years} ëåò, {age.Months} ìåñÿöåâ, {age.Days} äíåé. \n" +
- $"Âû ðîäèëèñü â {GetRussianDayOfWeek(age.BirthDayOfWeek)}. \n" + // Èçìåíåíèå: âûâîä äíÿ íåäåëè íà ðóññêîì
- $"Êîëè÷åñòâî äíåé ðîæäåíèÿ, êîòîðûå âû îòïðàçäíîâàëè â ýòîò æå äåíü íåäåëè: {age.BirthdaysOnSameWeekday}. \n" +
- $"Âèñîêîñíûå ãîäû: {string.Join(", ", age.LeapYears)}.";
- }
- private void ZodiacButton_Click(object sender, RoutedEventArgs e)
- {
- var birthDate = dp.SelectedDate;
- if (birthDate == null)
- {
- var errorTextBlock = this.FindControl<TextBlock>("ErrorTextBlock");
- errorTextBlock.Text = "Âû ââåëè íåñóùåñòâóþùóþ äàòó";
- return;
- }
- // çíàê çîäèàêà
- var birthDateTime = birthDate.Value.DateTime;
- int year = birthDateTime.Year;
- int month = birthDateTime.Month;
- int day = birthDateTime.Day;
- string Zod = GetZodiacSign(month, day);
- zodiacTextBlock.Text = $"Çíàê çîäèàêà: {Zod}";
- zodiacTextBlock.IsVisible = true;
- chineseZodiacTextBlock.Text = "";
- chineseZodiacTextBlock.IsVisible = false;
- }
- private void KinaiZodiacButton_Click(object sender, RoutedEventArgs e)
- {
- var birthDate = dp.SelectedDate;
- if (birthDate == null)
- {
- var errorTextBlock = this.FindControl<TextBlock>("ErrorTextBlock");
- errorTextBlock.Text = "Âû ââåëè íåñóùåñòâóþùóþ äàòó";
- return;
- }
- // çíàê âîñòî÷íîãî êàëåíäàðÿ
- var birthDateTime = birthDate.Value.DateTime;
- int year = birthDateTime.Year;
- string[] ZnakV = { "Êðûñà", "Áûê", "Òèãð", "Êðîëèê", "Äðàêîí", "Çìåÿ", "Ëîøàäü", "Êîçà", "Îáåçüÿíà", "Ïåòóõ", "Ñîáàêà", "Ñâèíüÿ" };
- int index = (year - 4) % 12;
- chineseZodiacTextBlock.Text = $"Çíàê âîñòî÷íîãî êàëåíäàðÿ: {ZnakV[index]}";
- chineseZodiacTextBlock.IsVisible = true;
- zodiacTextBlock.Text = "";
- zodiacTextBlock.IsVisible = false;
- }
- private Age CalculateAge(DateTime birthDate)
- {
- var today = DateTime.Now;
- var years = today.Year - birthDate.Year;
- var months = today.Month - birthDate.Month;
- var days = today.Day - birthDate.Day;
- var birthDayOfWeek = birthDate.DayOfWeek;
- if (months < 0 || (months == 0 && days < 0))
- {
- years--;
- months += 12;
- }
- if (days < 0)
- {
- months--;
- days += DateTime.DaysInMonth(today.Year, today.Month);
- }
- // êîëè÷åñòâî äíåé ðîæäåíèÿ â ýòîò æå äåíü íåäåëè
- int birthdaysOnSameWeekday = 0;
- var currentYear = birthDate.Year;
- while (currentYear < today.Year)
- {
- var potentialBirthday = new DateTime(currentYear, birthDate.Month, birthDate.Day);
- if (potentialBirthday.DayOfWeek == birthDayOfWeek)
- {
- birthdaysOnSameWeekday++;
- }
- currentYear++;
- }
- // âèñîêîñíûå ãîäû
- var leapYears = Enumerable.Range(birthDate.Year + 1, today.Year - birthDate.Year - 1)
- .Where(year => DateTime.IsLeapYear(year))
- .ToList();
- return new Age { Years = years, Months = months, Days = days, BirthDayOfWeek = birthDayOfWeek, BirthdaysOnSameWeekday = birthdaysOnSameWeekday, LeapYears = leapYears };
- }
- private string GetZodiacSign(int month, int day)
- {
- switch (month)
- {
- case 3:
- return day >= 21 ? "Îâåí" : "Ðûáû";
- case 4:
- return day >= 20 ? "Òåëåö" : "Îâåí";
- case 5:
- return day >= 21 ? "Áëèçíåöû" : "Òåëåö";
- case 6:
- return day >= 21 ? "Ðàê" : "Áëèçíåöû";
- case 7:
- return day >= 23 ? "Ëåâ" : "Ðàê";
- case 8:
- return day >= 23 ? "Äåâà" : "Ëåâ";
- case 9:
- return day >= 23 ? "Âåñû" : "Äåâà";
- case 10:
- return day >= 23 ? "Ñêîðïèîí" : "Âåñû";
- case 11:
- return day >= 22 ? "Ñòðåëåö" : "Ñêîðïèîí";
- case 12:
- return day >= 22 ? "Êîçåðîã" : "Ñòðåëåö";
- case 1:
- return day >= 20 ? "Âîäîëåé" : "Êîçåðîã";
- case 2:
- return day >= 19 ? "Ðûáû" : "Âîäîëåé";
- default:
- return "Íåèçâåñòíûé çíàê";
- }
- }
- private string GetRussianDayOfWeek(DayOfWeek dayOfWeek)
- {
- switch (dayOfWeek)
- {
- case DayOfWeek.Monday: return "Ïîíåäåëüíèê";
- case DayOfWeek.Tuesday: return "Âòîðíèê";
- case DayOfWeek.Wednesday: return "Ñðåäà";
- case DayOfWeek.Thursday: return "×åòâåðã";
- case DayOfWeek.Friday: return "Ïÿòíèöà";
- case DayOfWeek.Saturday: return "Ñóááîòà";
- case DayOfWeek.Sunday: return "Âîñêðåñåíüå";
- default: return "Íåèçâåñòíûé äåíü";
- }
- }
- }
- }
|