Я написал программу, которая вычисляет количество дней до события, может быть несколько событий, я хотел бы знать, как уменьшить или упростить этот код.
namespace Determining_the_time_before_the_event_starts
{
class Program
{
struct Event
{
public string name;
public DateTime time;
}
static Event[] InPutEvents()
{
Console.Write("Number of events: ");
int n, year, month, day, hour, min;
n = int.Parse(Console.ReadLine());
Event[] time = new Event[n];
for(int i=0; i<n; i++)
{
Console.Write("Event name: ");
time[i].name = Console.ReadLine();
Console.Write("Event start year: ");
year = int.Parse(Console.ReadLine());
Console.Write("Month of the beginning of the event: ");
month = int.Parse(Console.ReadLine());
Console.Write("The first day of the event: ");
day = int.Parse(Console.ReadLine());
Console.Write("Watch the beginning of the event: ");
hour = int.Parse(Console.ReadLine());
Console.Write("Minutes before the event starts: ");
min = int.Parse(Console.ReadLine());
time[i].time = new DateTime(year, month, day, hour, min, 0);
Console.WriteLine();
}
return time;
}
static void Main(string[] args)
{
Event[] time = InPutEvents();
Console.WriteLine();
BeginEvent(time);
Console.ReadLine();
}
static void BeginEvent (Event[] time)
{
DateTime now = DateTime.Now;
for(int i = 0; i < time.Length; i++)
{
SearchTime(time[i], now);
Console.WriteLine();
}
}
static void SearchTime(Event time, DateTime now)
{
int year, month, day, hour, min;
year = time.time.Year - now.Year;
month = time.time.Month - now.Month;
day = time.time.Day - now.Day;
hour = time.time.Hour - now.Hour;
min = time.time.Minute - now.Minute;
if (min < 0)
{
min += 60;
hour--;
}
if (min > 60)
{
min -= 60;
hour++;
}
if (hour < 0)
{
hour += 24;
day--;
}
if (hour > 24)
{
hour -= 24;
day++;
}
if (day < 1)
{
day += 31;
month--;
}
if (day > 31)
{
day -= 31;
month++;
}
if (month < 1)
{
month += 12;
year--;
}
if (month > 12)
{
month -= 12;
year++;
}
day += (month * 31) + (year * 365);
Console.WriteLine($"Event {time.name} starts in {day} days, {hour}hours, and {min}m. ");
}
}
}