代码直接CSC编译即可。
using System; using System.Diagnostics; using System.IO; using System.Threading; using System.Threading.Tasks; class Program { static ConsoleColor DefaultForegroundColor = Console.ForegroundColor; static int Main(params string[] args) { int days = 0; if (args.Length>0) { if(!int.TryParse(args[0], out days)) { ShowHelp(); Console.Write("按任意键退出..."); Console.ReadKey(true); Environment.Exit(1); //第一个参数错误 } else if(args.Length>1) { DateTime argsDate; if(args[1].Trim().Equals("now",StringComparison.OrdinalIgnoreCase)) { argsDate = DateTime.Now.AddDays(days - 1); } else if(!DateTime.TryParse(args[1], out argsDate)) { ShowHelp(); Console.Write("按任意键退出..."); Console.ReadKey(true); Environment.Exit(2); //第二个参数错误 } Console.WriteLine("\r\n * 火车票预售期为:{0}天\r\n\r\n出行日期:{1}",days, FormatDate(argsDate)); OutputDays(days, argsDate); var paused = true; if(args.Length>2) { if(args[2].Trim().Equals("nopause",StringComparison.OrdinalIgnoreCase)) { paused = false; } } if(paused) { Console.Write("\r\n按任意键退出..."); Console.ReadKey(true); } Environment.Exit(0); } } else { Console.WriteLine("计算从哪天起应该购买预售火车票"); Console.WriteLine(); } if(days <= 0) { Console.Write("火车票预售期(缺省为60天):"); var input=Console.ReadLine().Trim(); inputToExit(input); if (!int.TryParse(input, out days)||days<=0) { days = 60; ConsoleCursorRelativeLine(-1); var point = ConsoleCursorGetCurrentPosition(); if(point!=null) { ConsoleCursorFillLines(point.Value.Top,point.Value.Top); Console.Write("火车票预售期(缺省为60天):{0}\r\n", days); } } } Console.WriteLine("\r\n * 火车票预售期为:{0}天",days); Console.WriteLine(" * 今天可以预定 {0} 的火车票", FormatDate(DateTime.Now.AddDays(days - 1))); Console.WriteLine(" * 输入\"exit\"可退出程序,输入\"g\"打开12306订票网站。\r\n"); while(true) { Console.Write("出行日期:"); var input = Console.ReadLine().Trim(); inputToExit(input); if(input.Equals("g",StringComparison.OrdinalIgnoreCase)) { const string web = "http://www.12306.cn"; Process.Start(web); Console.WriteLine("正在打开 {0} ", web); } else { DateTime dest; if(DateTime.TryParse(input, out dest)) { ConsoleCursorRelativeLine(-1); var point = ConsoleCursorGetCurrentPosition(); if(point!=null) { ConsoleCursorFillLines(point.Value.Top,point.Value.Top); Console.WriteLine("出行日期:{0}",FormatDate(dest)); } OutputDays(days, dest); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("输入错误"); Console.ForegroundColor = DefaultForegroundColor; ConsoleCursorRelativeLine(-3); var point = ConsoleCursorGetCurrentPosition(); if(point!=null) { ConsoleCursorFillLines(point.Value.Top,point.Value.Top+1); } } } Console.WriteLine(); } } static void inputToExit(string input) { if(input.Equals("exit", StringComparison.OrdinalIgnoreCase)) { Environment.Exit(0); } } static void ShowHelp() { var appname = Path.GetFileName(Environment.GetCommandLineArgs()[0]); Console.WriteLine(" * 参数格式:"); Console.WriteLine(" {0} <火车票预售期>", appname); Console.WriteLine(" {0} <火车票预售期> <出行日期> [nopause]", appname); Console.WriteLine(" {0} <火车票预售期> now [nopause]", appname); Console.WriteLine("\r\n * 例子:预售期60天,查看今天可以预定哪天火车票"); Console.WriteLine(" {0} 60 now", appname); Console.WriteLine("\r\n * 批处理:"); Console.WriteLine(" {0} 60 now nopause|Find \"出行日期:\"", appname); Console.WriteLine(); } static string FormatDate(DateTime date) { ConsoleColor? cc; return FormatDate(date, out cc); } static string FormatDate(DateTime date, out ConsoleColor? consoleColor) { var dayOfWeek = new []{ "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; var d = (date.Date - DateTime.Now.Date).Days; string tip; switch (d) { case -2: tip = "(前天)"; break; case -1: tip = "(昨天)"; break; case 0: tip = "(今天)"; break; case 1: tip = "(明天)"; break; case 2: tip = "(后天)"; break; default: tip = string.Format("({0})",dayOfWeek[(int)date.DayOfWeek]); break; } if (d>=0) { consoleColor = ConsoleColor.Green; } else if(d<0) { consoleColor = ConsoleColor.Yellow; } else { consoleColor = null; } return string.Format("{0:yyyy-M-d}{1}", date, tip); } static void OutputDays(int days, DateTime destDate) { var date = destDate.AddDays(-days + 1); ConsoleColor? cc; var formattedDest = FormatDate(date, out cc); Console.ForegroundColor = DefaultForegroundColor; Console.Write("预售日期:"); if(cc.HasValue) { Console.ForegroundColor = cc.Value; } Console.WriteLine(formattedDest); Console.ForegroundColor = DefaultForegroundColor; } static bool ConsoleCursorRelativeLine(int rows) { try { var t = Console.CursorTop + rows; Console.SetCursorPosition(0, t); return true; } catch { } return false; } static bool ConsoleCursorFillLines(int startLine, int endLine, char @char = ‘ ‘) { var d = endLine - startLine + 1; if (d>0) { try { var point = ConsoleCursorGetCurrentPosition().Value; Console.SetCursorPosition(0, startLine); Console.Write(new string(@char,Console.BufferWidth * d)); Console.SetCursorPosition(point.Left, point.Top); } catch { } } return false; } static Point? ConsoleCursorGetCurrentPosition() { try { return new Point(Console.CursorLeft, Console.CursorTop); } catch { } return null; } struct Point { public int Left; public int Top; public Point(int left, int top) { Left = left; Top = top; } } }
时间: 2024-10-16 02:58:34