计算从哪天起应该购买预售火车票.cs

代码直接CSC编译即可。

计算从哪天起应该购买预售火车票.cs

  1 using System;
  2 using System.Diagnostics;
  3 using System.IO;
  4 
  5 class Program
  6 {
  7     static ConsoleColor DefaultForegroundColor = Console.ForegroundColor; //保留Console前景色
  8     
  9     static int Main(params string[] args)
 10     {
 11         int days = 0; //预售天数
 12         if (args.Length>0)
 13         {
 14             if(!int.TryParse(args[0], out days))
 15             {
 16                 ShowHelpAndExit(1); //第一个参数错误
 17             }
 18             else if(args.Length>1)
 19             {
 20                 DateTime argsDate; //出行日期
 21                 if(args[1].Trim().Equals("now",StringComparison.OrdinalIgnoreCase))
 22                 {
 23                     argsDate = DateTime.Now.AddDays(days - 1); //出行日期为now的话,计算出今天可预订的出行日期
 24                 }
 25                 else if(!DateTime.TryParse(args[1], out argsDate))
 26                 {
 27                     ShowHelpAndExit(2); //第二个参数错误
 28                 }                
 29                 Console.WriteLine("\r\n * 火车票预售期为:{0}天\r\n\r\n出行日期:{1}",days, FormatDate(argsDate));
 30                 OutputBookingDay(days, argsDate); //输出预售日期
 31                 var paused = true;
 32                 if(args.Length>2)
 33                 {
 34                     if(args[2].Trim().Equals("nopause",StringComparison.OrdinalIgnoreCase))
 35                     {
 36                         paused = false; //第三个参数为 nopause 则不暂停直接退出,用于配合命令提示符
 37                     }
 38                 }
 39                 if(paused)
 40                 {
 41                     Console.Write("\r\n按任意键退出...");
 42                     Console.ReadKey(true);
 43                 }
 44                 Environment.Exit(0); //正常退出
 45             }
 46         }
 47         else
 48         {
 49             Console.WriteLine("计算从哪天起应该购买预售火车票"); //未带参数,输出标题
 50             Console.WriteLine();
 51         }
 52         if(days <= 0)
 53         {
 54             Console.Write("火车票预售期(缺省为60天):");
 55             var input=Console.ReadLine().Trim();  //手动输入预售期
 56             inputToExit(input);
 57             if (!int.TryParse(input, out days)||days<=0)
 58             {
 59                 days = 60; //输入错误,预售期设置为60天
 60                 ConsoleCursorRelativeLine(-1); //光标移动到上一行行首。
 61                 var point = ConsoleCursorGetCurrentPosition();
 62                 if(point!=null)
 63                 {
 64                     ConsoleCursorFillLines(point.Value.Top,point.Value.Top, ' '); //清除光标所在行的显示字符(用空格填充)
 65                     Console.Write("火车票预售期(缺省为60天):{0}\r\n", days);
 66                 }
 67             }
 68         }
 69         Console.WriteLine("\r\n * 火车票预售期为:{0}天",days);
 70         Console.WriteLine(" * 今天可以预定 {0} 的火车票", FormatDate(DateTime.Now.AddDays(days - 1)));
 71         Console.WriteLine(" * 输入\"exit\"可退出程序,输入\"g\"打开12306订票网站。\r\n");
 72         while(true)
 73         {
 74             Console.Write("出行日期:");  //输入出行日期
 75             var input = Console.ReadLine().Trim();
 76             inputToExit(input);  //若输入exit则退出
 77             if(input.Equals("g",StringComparison.OrdinalIgnoreCase))
 78             {
 79                 const string web = "http://www.12306.cn";
 80                 Process.Start(web); //输入g打开12306网站
 81                 Console.WriteLine("正在打开 {0} ", web);
 82             }
 83             else
 84             {
 85                 DateTime dest;
 86                 if(DateTime.TryParse(input, out dest))
 87                 {
 88                     ConsoleCursorRelativeLine(-1);
 89                     var point = ConsoleCursorGetCurrentPosition();
 90                     if(point!=null)
 91                     {
 92                         ConsoleCursorFillLines(point.Value.Top,point.Value.Top, ' '); //清除前一行显示字符
 93                         Console.WriteLine("出行日期:{0}",FormatDate(dest)); //更新输出日期
 94                     }
 95                     OutputBookingDay(days, dest);
 96                 }
 97                 else
 98                 {
 99                     Console.ForegroundColor = ConsoleColor.Red;
100                     Console.WriteLine("输入错误"); //显示输出错误
101                     Console.ForegroundColor = DefaultForegroundColor;
102                     ConsoleCursorRelativeLine(-3); //光标向上移动3行
103                     var point = ConsoleCursorGetCurrentPosition();
104                     if(point!=null)
105                     {
106                         ConsoleCursorFillLines(point.Value.Top,point.Value.Top+1, ' ');
107                     }
108                 }
109             }
110             Console.WriteLine();
111         }
112     }
113     
114     static void inputToExit(string input)
115     {
116         if(input.Equals("exit", StringComparison.OrdinalIgnoreCase))
117         {
118              Environment.Exit(0); //用户退出
119         }
120     }
121     
122     static void ShowHelp()
123     {
124         //帮助提示
125         var appname = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
126         Console.WriteLine(" * 参数格式:");
127         Console.WriteLine("   {0} <火车票预售期>", appname);
128         Console.WriteLine("   {0} <火车票预售期> <出行日期> [nopause]", appname);
129         Console.WriteLine("   {0} <火车票预售期> now [nopause]", appname);
130         Console.WriteLine("\r\n * 例子:预售期60天,查看今天可以预定哪天火车票");
131         Console.WriteLine("   {0} 60 now", appname);
132         Console.WriteLine("\r\n * 批处理:");
133         Console.WriteLine("   {0} 60 now nopause|Find \"出行日期:\"", appname);
134         Console.WriteLine();
135     }
136     
137     static void ShowHelpAndExit(int exitcode)
138     {
139         ShowHelp(); //显示帮助提示然后按任意键退出
140         Console.Write("按任意键退出...");
141         Console.ReadKey(true);
142         Environment.Exit(exitcode); //返回错误码(errorlevel)
143     }
144     
145     static string FormatDate(DateTime date)
146     {
147         ConsoleColor? cc;
148         return FormatDate(date, out cc); //格式化日期
149     }
150     
151     static string[] dayOfWeek = new []{ "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
152     static string FormatDate(DateTime date, out ConsoleColor? consoleColor)
153     {        
154         var d = (date.Date - DateTime.Now.Date).Days;
155         string tip;
156         switch (d)
157         {
158             case -2:
159                 tip = "(前天)";
160                 break;
161             case -1:
162                 tip = "(昨天)";
163                 break;
164             case 0:
165                 tip = "(今天)";
166                 break;
167             case 1:
168                 tip = "(明天)";
169                 break;
170             case 2:
171                 tip = "(后天)";
172                 break;
173             default:
174                 tip = string.Format("({0})",dayOfWeek[(int)date.DayOfWeek]);
175                 break;
176         }
177         
178         if (d>=0)
179         {
180             consoleColor = ConsoleColor.Green;  //返回建议颜色,下同
181         }
182         else
183         {
184             consoleColor = ConsoleColor.Yellow;
185         }
186         
187         return string.Format("{0:yyyy-M-d}{1}", date, tip);
188     }
189     
190     static void OutputBookingDay(int days, DateTime destDate)
191     {
192         var date = destDate.AddDays(-days + 1);
193         ConsoleColor? cc;
194         var formattedDest = FormatDate(date, out cc);
195         Console.ForegroundColor = DefaultForegroundColor;
196         Console.Write("预售日期:"); //用缺省前景色显示“预售日期:”这几个字
197         if(cc.HasValue)
198         {
199             Console.ForegroundColor = cc.Value; //更换前景色
200         }
201         Console.WriteLine(formattedDest); //按格式输出预售日期
202         Console.ForegroundColor = DefaultForegroundColor; //还原缺省前景色
203     }
204     
205     static bool ConsoleCursorRelativeLine(int rows)
206     {
207         //将ConsoleCursor操作封装成方法,加入try语句,防止在非控制台下输出造成异常,下同
208         try
209         {
210             var t = Console.CursorTop + rows; //相对行号
211             Console.SetCursorPosition(0, t); //移动光标到相对行
212             return true; //成功
213         }
214         catch
215         {
216         }
217         return false; //失败
218     }
219     
220     static bool ConsoleCursorFillLines(int startLine, int endLine, char @char)
221     {
222         var d = endLine - startLine + 1;
223         if (d>0)
224         {
225             try 
226             {
227                 var point = ConsoleCursorGetCurrentPosition().Value;            
228                 Console.SetCursorPosition(0, startLine); //光标移动到起始行
229                 Console.Write(new string(@char,Console.BufferWidth * d)); //用字符填满指定行数(d)
230                 Console.SetCursorPosition(point.Left, point.Top); //返回光标原来的位置
231                 return true; //成功
232             }
233             catch
234             {
235             }
236         }
237         return false; //失败
238     }
239     
240     static Point? ConsoleCursorGetCurrentPosition()
241     {
242         try
243         {
244             return new Point(Console.CursorLeft, Console.CursorTop); //返回光标位置
245         }
246         catch
247         {
248         }
249         return null; //失败
250     }
251     
252     struct Point
253     {
254         public int Left;
255         public int Top;
256         
257         public Point(int left, int top)
258         {
259             Left = left;
260             Top = top;
261         }
262     }
263 }

 

posted @ 2015-04-21 16:10  Bob-wei  阅读(442)  评论(0编辑  收藏  举报