![]() |
#2
m10139237282010-12-28 21:17
c# 代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace VehicleMngSys { class Program { //利用正则表达式判断车辆ID是否合法,合法的ID是以C或T开头,后面跟4个数字 static bool IsValidvId(string vId) { Regex r = new Regex(@"^[CT]\d{4}$"); return r.IsMatch(vId); } static bool IsValidDateTime(string inputDateTime) { try { //解析成功,返回true,有异常则返回false DateTime.Parse(inputDateTime); return true; } catch { return false; } } //功能如果选择错误,按q可以回到主菜单 static bool IsReturn(string quit) { return Char.ToLower(quit[0]) == 'q' ? true : false; } //添加一辆新车 static void AddNewVehicle(Fleet f) { string vId ; do { Console.WriteLine("请输入车辆ID:"); vId = Console.ReadLine(); //功能如果选择错误,按q可以回到主菜单 if (IsReturn(vId)) return; } while (!IsValidvId(vId));//判断车辆ID是否合法 string strType; do { Console.WriteLine("请输入车辆类型(Car/Truck):"); strType = Console.ReadLine(); if (IsReturn(strType)) return; }while ((strType != "Car" && strType != "Truck"));//判断车辆类型的输入是否合法 f.AddNewVehicle(vId, strType == "Car" ? VehicleType.Car : VehicleType.Truck); Console.WriteLine("添加新车成功,任意键继续!"); Console.ReadKey(); } //删除一辆已经存在的车 static void DeleteExistVehicle(Fleet f) { string vId; do { Console.WriteLine("请输入要删除车辆的ID:"); vId = Console.ReadLine(); if (IsReturn(vId)) return; } while (!IsValidvId(vId)); f.DeleteExistVehicle(vId); } //租车 static void BookVehicle(Fleet f) { string strType; do { Console.WriteLine("请输入车辆类型(Car/Truck):"); strType = Console.ReadLine(); if (IsReturn(strType)) return; } while ((strType != "Car" && strType != "Truck"));//判断车辆类型的输入是否合法 VehicleType vType = strType == "Car" ? VehicleType.Car : VehicleType.Truck; string vId; do { Console.WriteLine("请输入车辆ID:"); vId = Console.ReadLine(); } while (!IsValidvId(vId));//判断车辆ID是否合法 DateTime rentStartDate; try { string inputDateTime; if (vType == VehicleType.Car) { do { Console.WriteLine("请输入租车的开始时间(YYYY-MM-DD):"); inputDateTime = Console.ReadLine(); } while (!IsValidDateTime(inputDateTime)); rentStartDate = Convert.ToDateTime(inputDateTime); //利用linq方法在车辆集合中找到和指定ID匹配的车,v => v.vId == vId为lambda表达式 (f.vehicles.Single(v => v.vId == vId) as Car).BookCar(rentStartDate); } else { string strRentType; do { Console.WriteLine("请输入租车的方式(Hour/Day):"); strRentType = Console.ReadLine(); } while ((strRentType != "Hour" && strRentType != "Day"));//判断卡车的出租类型是按小时还是按天 TructRentType trType = strRentType == "Hour" ? TructRentType.ByHour : TructRentType.ByDay; do { if (trType == TructRentType.ByDay) { Console.WriteLine("请输入租车的开始时间(YYYY-MM-DD):"); } else { Console.WriteLine("请输入租车的开始时间(YYYY-MM-DD HH:mm:ss):"); } inputDateTime = Console.ReadLine(); } while (!IsValidDateTime(inputDateTime)); rentStartDate = Convert.ToDateTime(inputDateTime); //利用linq方法在车辆集合中找到和指定ID匹配的车,v => v.vId == vId为lambda表达式 (f.vehicles.Single(v => v.vId == vId) as Truck).BookTruck(trType, rentStartDate); } Console.WriteLine("租车成功,任意键继续!"); Console.ReadKey(); } //异常可能是车辆已经出租,不能重复出租。或者是租车的时间不能比上一个还车的时间早,这与租车逻辑矛盾 catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); } } //还车 static public void ReturnVehicle(Fleet f) { string vId; do { Console.WriteLine("请输入车辆ID:"); vId = Console.ReadLine(); if (IsReturn(vId)) return; } while (!IsValidvId(vId)); Vehicle v = f.vehicles.Single(qv => qv.vId == vId); if (v.vType == VehicleType.Car) { Console.WriteLine("请输入租车的天数:"); } else { //利用linq找到车辆出租信息集合中的最后一条记录,以确定是按小时还是按天出租 if ((v as Truck).trDetails.OrderBy(qv => qv.rentStartDate).Last().tRentType == TructRentType.ByDay) { Console.WriteLine("请输入租车的天数:"); } else { Console.WriteLine("请输入租车的小时数:"); } } try { v.ReturnVehicle(Convert.ToInt32(Console.ReadLine())); //计算租车费用 Console.WriteLine("您需要支付租车费用:{0:c} 元", v.CaculateFee()); Console.WriteLine(" 还车车成功,任意键继续!"); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); } } //按车辆ID及年月查询出租情况 static void QueryRentChartById(Fleet f) { string inputDateTime; do { Console.WriteLine("请输入租车的开始时间(YYYY-MM-DD):"); inputDateTime = Console.ReadLine(); } while (!IsValidDateTime(inputDateTime)); DateTime queryYearMonth = Convert.ToDateTime(inputDateTime); string vId; do { Console.WriteLine("请输入车辆ID:"); vId = Console.ReadLine(); if (IsReturn(vId)) return; } while (!IsValidvId(vId)); f.ShowRentChartByVId(vId, queryYearMonth); Console.WriteLine("敲任意键继续!"); Console.ReadKey(); } //按年月查询所有车的出租情况 static void QueryRentChartByMonth(Fleet f) { string inputDateTime; do { Console.WriteLine("请输入租车的开始时间(YYYY-MM-DD):"); inputDateTime = Console.ReadLine(); } while (!IsValidDateTime(inputDateTime)); DateTime queryYearMonth = Convert.ToDateTime(inputDateTime); f.ShowRentChartByMonth(queryYearMonth); Console.WriteLine("敲任意键继续!"); Console.ReadKey(); } // 查询车辆每月出租的详细费用情况 static void ShowRentDetails(Fleet f) { string inputDateTime; do { Console.WriteLine("请输入租车的开始时间(YYYY-MM-DD):"); inputDateTime = Console.ReadLine(); } while (!IsValidDateTime(inputDateTime)); DateTime queryYearMonth = Convert.ToDateTime(inputDateTime); f.ShowRentDetails(queryYearMonth); Console.WriteLine("敲任意键继续!"); Console.ReadKey(); } static void Main(string[] args) { bool isExit = false; Fleet f = new Fleet(); while (1 == 1) { if (!isExit) { Console.WriteLine("欢迎使用好运来车辆出租管理系统!"); Console.WriteLine("1.添加一辆新车"); Console.WriteLine("2.删除一辆已经存在的车"); Console.WriteLine("3.租车"); Console.WriteLine("4.还车"); Console.WriteLine("5.按年月及车辆ID查询车辆出租情况"); Console.WriteLine("6.按年月查询所有车辆的使用情况"); Console.WriteLine("7.按年月查询所有车辆的费用情况"); Console.WriteLine("8.保存车辆及出租情况记录"); Console.WriteLine("9.退出系统"); Console.WriteLine("请选择系统功能!"); ConsoleKeyInfo pk = Console.ReadKey(); switch (pk.KeyChar) { case '1': AddNewVehicle(f); break; case '2': DeleteExistVehicle(f); break; case '3': BookVehicle(f); break; case '4': ReturnVehicle(f); break; case '5': QueryRentChartById(f); break; case '6': QueryRentChartByMonth(f); break; case '7': ShowRentDetails(f); break; case '8': f.SaveRecs(); Console.WriteLine("保存记录成功,任意键继续!"); Console.ReadKey(); break; case '9': isExit = true; f.SaveRecs(); break; default: break; } } else { break; } } } } } |
请求高手:
c#源代码怎样转换成c++源代码??????????????????