接下来的是录入航班、修改航班信息功能的实现:
1 //录入航班 2 BOOL EntryFlight(HWND hEntryDlg){ 3 4 TCHAR szDiscount[16]; 5 TCHAR szFare[16],szSeat_Number[16]; 6 TCHAR szFlight_Number[16], szDate[16]; 7 TCHAR szDeparture[16], szDestination[16]; 8 TCHAR szTakeOff_Time[16], szLanding_Time[16]; 9 10 Flight *p = (Flight *)malloc(sizeof(Flight)),*q; 11 if (!p){ 12 MessageBox(hEntryDlg, TEXT("内存申请错误!"), TEXT("错误"), MB_OK | MB_ICONERROR); 13 return FALSE; 14 } 15 16 GetDlgItemText(hEntryDlg, IDC_DEPARTURE, szDeparture, 16); //获取录入航班信息 17 GetDlgItemText(hEntryDlg, IDC_DESTINATION, szDestination, 16); 18 GetDlgItemText(hEntryDlg, IDC_FLTNUM, szFlight_Number, 16); 19 GetDlgItemText(hEntryDlg, IDC_FARE, szFare, 16); 20 GetDlgItemText(hEntryDlg, IDC_TAKEOFFTIME, szTakeOff_Time, 16); 21 GetDlgItemText(hEntryDlg, IDC_LANDINGTIME, szLanding_Time, 16); 22 GetDlgItemText(hEntryDlg, IDC_SEATNUM, szSeat_Number, 16); 23 GetDlgItemText(hEntryDlg, IDC_DISCOUNT, szDiscount, 16); 24 GetDlgItemText(hEntryDlg, IDC_DATE, szDate, 16); 25 26 if (!lstrlen(szDeparture) || !lstrlen(szDestination) || 27 !lstrlen(szFlight_Number) || !lstrlen(szFare) || 28 !lstrlen(szTakeOff_Time) || !lstrlen(szLanding_Time) || 29 !lstrlen(szSeat_Number)) 30 { 31 MessageBox(hEntryDlg, TEXT("请您填写完整航班信息!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION); 32 return FALSE; 33 } 34 35 q = flightlink.head; 36 while (q){ 37 if (!lstrcmp(q->Flight_Number, szFlight_Number) && !lstrcmp(q->Date,szDate)){ 38 MessageBox(hEntryDlg, TEXT("对不起,同一航班在一天内只能有一班"), TEXT("提示"), MB_OK | MB_ICONINFORMATION); 39 free(p); 40 return FALSE; 41 } 42 q = q->Next; 43 }//while 44 45 lstrcpy(p->Date, szDate); //填入录入航班结点信息 46 lstrcpy(p->Discount, szDiscount); 47 lstrcpy(p->Departure, szDeparture); 48 lstrcpy(p->Destination, szDestination); 49 lstrcpy(p->TakeOff_Time, szTakeOff_Time); 50 lstrcpy(p->Landing_Time, szLanding_Time); 51 lstrcpy(p->Flight_Number, szFlight_Number); 52 p->Fare = _ttof(szFare); 53 p->Seat_Number = _ttoi(szSeat_Number); 54 p->Vacant_Seat = p->Seat_Number; 55 p->Next = NULL; 56 57 if (flightlink.head == NULL) //将新结点添至航班链表 58 flightlink.head = p; 59 else 60 flightlink.tail->Next = p; 61 flightlink.tail = p; 62 flightlink.Flight_Number++; //航班数目增加 63 64 MessageBox(hEntryDlg, TEXT("航班录入成功!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION); 65 66 return TRUE; 67 }//EntryFlight()
1 //修改航班信息 2 BOOL ModifyFlight(HWND hModifyDlg){ 3 int seat_num; 4 TCHAR szDiscount[16]; 5 TCHAR szFare[16], szSeat_Number[16]; 6 TCHAR szTakeOff_Time[16], szLanding_Time[16]; 7 TCHAR Mdf_flt_num[16], Mdf_flt_date[16]; 8 9 OrderForm *q; 10 Flight *p; 11 12 GetDlgItemText(hModifyDlg, IDC_MDFFLTNUM, Mdf_flt_num, 16); //获取要修改的航班 13 GetDlgItemText(hModifyDlg, IDC_MDFDATE, Mdf_flt_date, 16); 14 15 if (!lstrlen(Mdf_flt_num)){ 16 MessageBox(hModifyDlg, TEXT("请输入所要修改信息的航班号"), TEXT("信息"), MB_OK | MB_ICONINFORMATION); 17 return FALSE; 18 } 19 20 GetDlgItemText(hModifyDlg, IDC_FARE, szFare, 16); //获取要修改的信息 21 GetDlgItemText(hModifyDlg, IDC_DISCOUNT, szDiscount, 16); 22 GetDlgItemText(hModifyDlg, IDC_SEATNUM, szSeat_Number, 16); 23 GetDlgItemText(hModifyDlg, IDC_TAKEOFFTIME, szTakeOff_Time, 16); 24 GetDlgItemText(hModifyDlg, IDC_LANDINGTIME, szLanding_Time, 16); 25 26 p = flightlink.head; 27 while (p){ //查找要修改的航班 28 if (!lstrcmp(Mdf_flt_num, p->Flight_Number) && !lstrcmp(Mdf_flt_date, p->Date)){ 29 if (lstrlen(szFare)) //改变航班票价 30 p->Fare = _ttof(szFare); 31 if (lstrlen(szDiscount)) //改变航班折扣 32 wsprintf(p->Discount, szDiscount); 33 if (lstrlen(szSeat_Number)){ 34 seat_num = _ttoi(szSeat_Number); 35 if ((p->Seat_Number - seat_num) > p->Vacant_Seat){ //余座数量>=0 36 MessageBox(hModifyDlg, TEXT("对不起,座位数过少,请重新填写"), TEXT("提示"), MB_OK | MB_ICONINFORMATION); 37 return FALSE; 38 } 39 p->Vacant_Seat -= p->Seat_Number - seat_num; //改变余票数量 40 p->Seat_Number = seat_num; //改变座位数 41 }//if 42 if (lstrlen(szTakeOff_Time)){ //改变航班起飞时间 43 wsprintf(p->TakeOff_Time, szTakeOff_Time); 44 q = allorder.head; //改变该航班所有订单出发时间 45 while (q){ 46 if (!lstrcmp(q->Flight_Number, p->Flight_Number) && !lstrcmp(q->Date, p->Date)) 47 wsprintf(q->TakeOff_Time, szTakeOff_Time); 48 q = q->Next; 49 } 50 }//if 51 if (lstrlen(szLanding_Time)){ //改变航班降落时间 52 wsprintf(p->Landing_Time, szLanding_Time); 53 q = allorder.head; //改变该航班所有订单降落时间 54 while (q) { 55 if (!lstrcmp(q->Flight_Number, p->Flight_Number) && !lstrcmp(q->Date, p->Date)) 56 wsprintf(q->Landing_Time, szLanding_Time); 57 q = q->Next; 58 } 59 }//if 60 MessageBox(hModifyDlg, TEXT("修改成功!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION); 61 return TRUE; 62 }//if 63 p = p->Next; 64 }//while 65 66 MessageBox(hModifyDlg, TEXT("对不起,您所要修改的航班尚未录入。"), TEXT("提示"), MB_OK | MB_ICONINFORMATION); 67 return FALSE; 68 69 }//ModifyFlight()
至此,订票系统的功能已经基本全部实现了,剩下的问题就是在程序退出时将账户、航班、订单数据写进文件里。
具体实现起来很简单,需要注意的知识写入与读取要相对应。代码如下:
1 //保存账户资料 2 BOOL WriteAccountData(HWND hMainDlg){ 3 Passenger *p; 4 FILE *fp; 5 if ((fp = fopen(".\\AccountData.txt", "w")) == NULL){ 6 MessageBox(hMainDlg, TEXT("账户信息保存出现错误!"), TEXT("错误"), MB_OK | MB_ICONERROR); 7 return FALSE; 8 } 9 10 p = psglink.head; 11 while (p){ 12 fprintf(fp, "%s\n%s\n%s\n", p->Name, p->IdNum, p->PassWord); 13 p = p->Next; 14 }//while 15 16 fclose(fp); 17 return TRUE; 18 }//WriteAccountData 19 20 21 //保存订单信息 22 BOOL WriteOrderData(HWND hMainDlg){ 23 OrderForm *p; 24 FILE *fp; 25 if ((fp = fopen(".\\OrderForm.txt", "w")) == NULL){ 26 MessageBox(hMainDlg, TEXT("订单信息保存出现错误!"), TEXT("错误"), MB_OK | MB_ICONERROR); 27 return FALSE; 28 } 29 p = allorder.head; 30 while (p){ 31 fprintf(fp, "%s\n", p->IdNum); 32 fprintf(fp, "%d\n", p->Order_Number); 33 fprintf(fp, "%d\n", p->Tickets_Num); 34 fprintf(fp, "%s\n", p->Flight_Number); 35 fprintf(fp, "%s\n", p->Departure); 36 fprintf(fp, "%s\n", p->Destination); 37 fprintf(fp, "%s\n", p->Date); 38 fprintf(fp, "%s\n", p->TakeOff_Time); 39 fprintf(fp, "%s\n", p->Landing_Time); 40 p = p->Next; 41 }//while 42 fclose(fp); 43 return TRUE; 44 }//WriteOrderData() 45 46 47 //保存航班信息 48 BOOL WriteFlightData(HWND hMainDlg){ 49 Flight *p; 50 FILE *fp; 51 if ((fp = fopen(".\\FlightData.txt", "w")) == NULL){ 52 MessageBox(hMainDlg, TEXT("航班信息保存出现错误!"), TEXT("错误"), MB_OK | MB_ICONERROR); 53 return FALSE; 54 } 55 p = flightlink.head; 56 while (p){ 57 fprintf(fp, "%s\n", p->Flight_Number); 58 fprintf(fp, "%.2lf\n", p->Fare); 59 fprintf(fp, "%s\n", p->Discount); 60 fprintf(fp, "%d\n%d\n", p->Seat_Number, p->Vacant_Seat); 61 fprintf(fp, "%s\n", p->Departure); 62 fprintf(fp, "%s\n", p->Destination); 63 fprintf(fp, "%s\n", p->Date); 64 fprintf(fp, "%s\n", p->TakeOff_Time); 65 fprintf(fp, "%s\n", p->Landing_Time); 66 p = p->Next; 67 }//while 68 fclose(fp); 69 return TRUE; 70 }//WriteFlight()
至此,这个飞机订票系统算是完成了,其中还是有很多不足之处。查找算法只是简单的遍历链表,数据结构也只是采用了单链表,倒是订单用上了十字链表来存储。查找起来效率不高,一旦数据量变大,便会很耗费时间,不过考虑到这只是一个课程设计,数据量很小,查找起来时间耗费上没有多少差别,姑且就这样吧。
把头文件以及其资源头文件代码就不贴出来了,想看的朋友可以直接把源码下载下来看一下。