Прокоментируйте код - C#
Формулировка задачи:
Написал лабу, суть создать структуру, и функции для работы с ней: (структура дата ДД/ММ/ГГ), должна уметь добавлять/ убирать один день, коректно отображать дату при введении например из 180/2/2013 сделать 30/7/2013 + в минуса не вгонить (31/2/-5).
Пожалуйста прокоментируйте/посоветуйте алгоритм функции check_DMY, может можно что нить рационалнее придумать?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace Lab1 { internal struct DMY { internal int Day; internal int Mounth; internal int Year; } class Program { static void parse_DMY(ref DMY TStruct) { } static void InDMY(ref DMY TStruct) { bool _flagOk = false; string[] mas_dates = null; string _dateString; char[] dem = { '/' }; while (_flagOk == false) { try { Console.Write("\n\nВведите дату в формате ДД/ММ/ГГ ->"); _dateString = Console.ReadLine(); mas_dates = _dateString.Split(dem, 3); TStruct.Day = Int32.Parse(mas_dates[0]); TStruct.Mounth = Int32.Parse(mas_dates[1]); TStruct.Year = Int32.Parse(mas_dates[2]); if (TStruct.Day <= 0 || TStruct.Mounth <= 0 || TStruct.Year <= 0) { throw new FormatException(); } _flagOk = true; } catch { Console.Clear(); Console.WriteLine("Ошибка ввода, повторите попытку"); } } } static void PrintDMY(ref DMY TStruct) { Console.WriteLine("\n\nФормат даты ДД/ММ/ГГ\n\nДата:" + TStruct.Day + "/" + TStruct.Mounth + "/" + TStruct.Year); } static void check_DMY(ref DMY TStruct) { int _years_from_mouth, dictionaryValue; bool check_ok; Dictionary<int, int> mas_days = new Dictionary<int, int> { { 1, 31 }, { 2, 28 }, { 3, 31 }, { 4, 30 }, { 5, 31 }, { 6, 30 }, { 7, 31 }, { 8, 31 }, { 9, 30 }, { 10, 31 }, { 11, 30 }, { 12, 31 }}; check_ok = false; while (!check_ok) { //опредление высокосного года if (TStruct.Year % 4 == 0) { mas_days[2] = 29; } //если количество месяцев >12 if (TStruct.Mounth > 12) { TStruct.Year++; _years_from_mouth = TStruct.Mounth / 12; TStruct.Mounth -= _years_from_mouth * 12; } else // когда отнимается день, если месяц и день первый в году if (TStruct.Mounth <= 1 && TStruct.Day < 1 && TStruct.Year > 1) { TStruct.Year--; TStruct.Mounth = 12; TStruct.Day = mas_days[12]; } dictionaryValue = mas_days[TStruct.Mounth]; //число дней в месяце //если кол.дней > кол.дней в месяце if (TStruct.Day > dictionaryValue) { TStruct.Day -= dictionaryValue; TStruct.Mounth++; } else //если кол.дней < 1, и месяц не первый (0/2/2005) if (TStruct.Day < 1 && TStruct.Mounth > 1) { TStruct.Mounth--; TStruct.Day = mas_days[TStruct.Mounth]; } //если день соответствует диапазону 1-кол.дней в определенном мецяце, удовлетворяется условие выхода из цикла if (TStruct.Day >= 1 && TStruct.Day <= dictionaryValue && TStruct.Mounth <= 12) { check_ok = true; } //если дата ровна 0/1/1 и дальше некуда уменьшать дни, день = 1 и удовлетворяется условие выхода из цикла if (TStruct.Day <= 1 && TStruct.Mounth <= 1 && TStruct.Year <= 1) { TStruct.Day = 1; check_ok = true; } } } static void AddDay(ref DMY TStuct) { TStuct.Day++; check_DMY(ref TStuct); } static void RemoveDay(ref DMY TStuct) { TStuct.Day--; check_DMY(ref TStuct); } static void Main(string[] args) { DMY mystruct = new DMY(); InDMY(ref mystruct); check_DMY(ref mystruct); char _Skey; menu: Console.Clear(); PrintDMY(ref mystruct); Console.WriteLine("\n\n\n1 - Добавить день\n2 - Убавить день\n3 - Изменить дату\n4 - Выход"); _Skey = Console.ReadKey().KeyChar; switch (_Skey) { case '1': AddDay(ref mystruct); goto menu; case '2': RemoveDay(ref mystruct); goto menu; case '3': Console.Clear(); InDMY(ref mystruct); goto menu; case '4': System.Environment.Exit(0); break; default: goto menu; } } } }
Решение задачи: «Прокоментируйте код»
textual
Листинг программы
internal struct DMY { internal int Day; internal int Mounth; internal int Year; }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д