Advertisement
Guest User

loty

a guest
Nov 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. namespace Loty
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             new Thread(() =>
  12.             {
  13.                 Thread.CurrentThread.IsBackground = true;
  14.                 /* run your code here */
  15.                 for (; ; )
  16.                 {
  17.                     if (Console.ReadKey(true).Key == ConsoleKey.Escape) Environment.Exit(0);
  18.                 }
  19.             }).Start();
  20.  
  21.             List<DateTime> odloty = new List<DateTime>();
  22.             List<DateTime> przyloty = new List<DateTime>();
  23.  
  24.             odloty.Add(DateTime.ParseExact("08.00", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  25.             przyloty.Add(DateTime.ParseExact("10.16", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  26.  
  27.             odloty.Add(DateTime.ParseExact("09.43", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  28.             przyloty.Add(DateTime.ParseExact("11.52", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  29.  
  30.             odloty.Add(DateTime.ParseExact("11.19", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  31.             przyloty.Add(DateTime.ParseExact("13.31", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  32.  
  33.             odloty.Add(DateTime.ParseExact("12.47", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  34.             przyloty.Add(DateTime.ParseExact("15.00", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  35.  
  36.             odloty.Add(DateTime.ParseExact("14.00", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  37.             przyloty.Add(DateTime.ParseExact("16.08", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  38.  
  39.             odloty.Add(DateTime.ParseExact("15.45", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  40.             przyloty.Add(DateTime.ParseExact("17.55", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  41.  
  42.             odloty.Add(DateTime.ParseExact("19.00", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  43.             przyloty.Add(DateTime.ParseExact("21.27", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  44.  
  45.             odloty.Add(DateTime.ParseExact("21.45", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  46.             przyloty.Add(DateTime.ParseExact("23.58", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  47.  
  48.             odloty.Add(DateTime.ParseExact("22.50", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  49.             przyloty.Add(DateTime.ParseExact("01.55", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  50.  
  51.             odloty.Add(DateTime.ParseExact("23.45", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  52.             przyloty.Add(DateTime.ParseExact("02.50", "HH.mm", System.Globalization.CultureInfo.InvariantCulture));
  53.  
  54.             DateTime g;
  55.  
  56.             for (; ; ) {
  57.                 Console.WriteLine("Wprowadź godzinę z minutami: ");
  58.                 try
  59.                 {
  60.                     g = DateTime.ParseExact(Console.ReadLine(), "HH.mm", System.Globalization.CultureInfo.InvariantCulture);
  61.                     if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape) break;
  62.                     int i = 0;
  63.                     int difference = int.MaxValue;
  64.                     foreach (DateTime c in odloty)
  65.                     {
  66.                         if(Math.Abs(g.Subtract(c).TotalMinutes) < difference) // && c > g) //wtedy tylko po tej godzinie
  67.                         {
  68.                             difference = (int)g.Subtract(c).TotalMinutes;
  69.                             i = odloty.IndexOf(c);
  70.                         }
  71.                     }
  72.  
  73.                     Console.WriteLine("Najbliższy odlot: " + odloty[i].ToString("HH:mm") + " przylot: " + przyloty[i].ToString("HH:mm"));
  74.                     Console.ReadLine();
  75.  
  76.                 } catch(Exception e)
  77.                 {
  78.                     Console.WriteLine("To nie jest poprawna godzina.");
  79.                 }
  80.             }
  81.  
  82.  
  83.  
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement