程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> windows API 開發飛機訂票系統 圖形化界面 (三),api圖形化

windows API 開發飛機訂票系統 圖形化界面 (三),api圖形化

編輯:關於C語言

windows API 開發飛機訂票系統 圖形化界面 (三),api圖形化


來吧,接下來是各個功能的函數的實現代碼。

首先,程序運行時加載讀入賬戶信息和航班信息。接下來就該讀取文件了。

我把賬戶資料和航班信息儲存在了.txt文件裡

 

那麼問題就來了,挖掘機...額,不對,應該怎麼讀入賬戶資料和航班信息?

每一行是結構體的一個成員,我讀入的方式是一行一行讀入。 

 

兩個讀入函數代碼如下:

 1 //讀入賬戶信息
 2 BOOL ReadAccountData(HWND hwndDlg){                        
 3     
 4     FILE *fp;
 5 
 6     passenger = (Passenger *)malloc(sizeof(Passenger));                                        //為登錄賬戶分配內存
 7     if (!passenger){
 8         MessageBox(hwndDlg, TEXT("內存申請錯誤!"), TEXT("錯誤"), MB_OK | MB_ICONERROR);
 9         EndDialog(hwndDlg, 0);
10         return FALSE;
11     }//if
12      
13     psglink.head = NULL;
14     psglink.tail = NULL;
15     psglink.PsgNum = 0;
16 
17     if ((fp = fopen(".\\AccountData.txt", "r+")) == NULL){        //打開文件
18         MessageBox(hwndDlg, TEXT("賬戶文件讀入錯誤!"), TEXT("錯誤"), MB_OK | MB_ICONERROR);
19         EndDialog(hwndDlg, 0);
20     }//if        
21 
22     while (!feof(fp)){
23         Passenger *p = (Passenger *)malloc(sizeof(Passenger));
24         if (!p){
25             MessageBox(hwndDlg, TEXT("內存申請錯誤!"), TEXT("錯誤"), MB_OK | MB_ICONERROR);
26             EndDialog(hwndDlg, 0);
27             return FALSE;
28         }//if
29 
30         //讀入賬戶姓名、身份證號、密碼
31         if (fscanf(fp, "%s%s%s", p->Name, p->IdNum, p->PassWord) == EOF){
32             free(p); break;
33         }
34 
35         p->Next = NULL;
36         if (psglink.head == NULL)                        //讀入第一個賬戶信息時,頭、尾結點均指向p
37             psglink.head = p;
38         else
39             psglink.tail->Next = p;                        //否則,尾結點Next指向p
40         psglink.tail = p;                                //尾結點指向p
41         psglink.PsgNum++;                                //已注冊賬戶個數
42 
43     }//while
44     fclose(fp);                                            //關閉文件
45     return TRUE;
46 }//ReadAccountData(HWND)
47 
48 
49 //讀入航班信息
50 BOOL ReadFlightData(HWND hwndDlg){                        
51 
52     int flag = 0;
53     FILE *fp;
54 
55     flightlink.Flight_Number = 0;
56     flightlink.head = NULL;
57     flightlink.tail = NULL;
58 
59     if ((fp = fopen(".\\FlightData.txt", "r")) == NULL){        //打開文件
60         MessageBox(hwndDlg, TEXT("賬戶文件讀入錯誤!"), TEXT("錯誤"), MB_OK | MB_ICONERROR);
61         EndDialog(hwndDlg, 0);
62     }//if
63 
64     while (!feof(fp)){
65         Flight *p = (Flight *)malloc(sizeof(Flight));
66         if (!p){
67             MessageBox(hwndDlg, TEXT("內存申請錯誤!"), TEXT("錯誤"), MB_OK | MB_ICONERROR);
68             EndDialog(hwndDlg, 0);
69             return FALSE;
70         }//if
71         //讀入航班信息
72         if (fscanf(fp, "%s%lf%s%d%d%s%s%s%s%s",
73             p->Flight_Number,
74             &p->Fare,
75             p->Discount,
76             &p->Seat_Number,
77             &p->Vacant_Seat,
78             p->Departure,
79             p->Destination,
80             p->Date,
81             p->TakeOff_Time,
82             p->Landing_Time) == EOF)
83         {
84             free(p);
85             break;
86         }
87         p->Next = NULL;
88         if (flightlink.head == NULL)                    //添加至鏈表
89             flightlink.head = p; 
90         else
91             flightlink.tail->Next = p;
92         flightlink.tail = p;
93         flightlink.Flight_Number++;
94 
95     }//while
96     fclose(fp);                                            //關閉文件
97     return TRUE;
98 }//ReadFlightData()

因為開發環境是vs2013,所以編譯時會報錯,報錯信息是大體是說fopen()函數不安全,根據提示信息,解決辦法是在文件頭部做如下定義:

 1 #define _CRT_SECURE_NO_WARNINGS  

或者是在項目->屬性->c/c++->預處理器->預處理器定義,後面加上_CRT_SECURE_NO_WARNINGS就行了。

 

結下來又遇到問題了:程序退出把數據寫入txt文件時每行末尾都會有一個\n,讀入數據的時候用的是feof()函數,而feof()返回的其實是"最後一次讀操作的內容",與數據庫中的eof()不同,eof()讀取的是當前指針位置。因此在讀到文件末尾的時候,程序會多執行一次循環,為了防止這種情況,我采取的是檢查fscanf()返回值的方法,在此以讀入賬戶資料時為例:

1 //讀入賬戶姓名、身份證號、密碼
2 if (fscanf(fp, "%s%s%s", p->Name, p->IdNum, p->PassWord) == EOF){
3     free(p); break;
4 }

然後是用戶登錄或注冊時所用到的函數:

 1 //登陸用戶驗證
 2 BOOL AccountLogIn(HWND hwndDlg){                        
 3     
 4     int flag = 0;                                                                //flag!=0則該用戶已注冊,flag==0則該用戶尚未注冊
 5     Passenger *p;
 6 
 7     GetDlgItemText(hwndDlg, IDC_IDEDIT, passenger->IdNum, 256);                    //獲取用戶輸入ID
 8     GetDlgItemText(hwndDlg, IDC_PSWEDIT, passenger->PassWord, 256);                //獲取用戶輸入密碼
 9     
10     p = psglink.head;
11     while (p){                                                                    //在用戶鏈表查找登陸用戶ID
12         if (!lstrcmp(passenger->IdNum, p->IdNum)){        
13             flag++;                                                                //該用戶已注冊
14             if (!lstrcmp(passenger->PassWord, p->PassWord)){                    //密碼匹配,登陸成功
15                 lstrcpy(passenger->Name, p->Name);
16                 MessageBox(hwndDlg, TEXT("登陸成功,單擊確定進入程序主界面"), TEXT("登陸成功"), MB_OK | MB_ICONINFORMATION);
17                 break;
18             }//if
19             else{                                                                //密碼錯誤,退出循環
20                 MessageBox(hwndDlg, TEXT("密碼錯誤,請重新輸入"), TEXT("密碼錯誤"), MB_OK | MB_ICONERROR);
21                 return FALSE;
22             }//else
23         }//if
24         p = p->Next;
25     }//while
26     if (!flag){                                                                    //用戶尚未注冊
27         if (lstrlen(passenger->PassWord)==0)
28             MessageBox(hwndDlg, TEXT("請輸入密碼!"), TEXT("信息"), MB_OK | MB_ICONINFORMATION);
29         else
30             MessageBox(hwndDlg, TEXT("該賬戶尚未注冊,請先注冊"), TEXT("信息"), MB_OK | MB_ICONINFORMATION);
31     }//if
32     else{                                                                        //關閉登陸界面,彈出主界面
33         EndDialog(hwndDlg, TRUE);
34         if (ReadAccountOrder(hwndDlg, passenger))                                //讀取用戶訂單
35             DialogBox(hInst, MAKEINTRESOURCE(IDD_MAINDLG), NULL, (DLGPROC)MainDlgProc);
36     }//else
37 
38     return TRUE;
39 }//AccountLogIn(HWND)
 1 //用戶注冊
 2 BOOL AccountRegister(HWND hwndDlg){
 3 
 4     GetDlgItemText(hwndDlg, IDC_IDEDIT, passenger->IdNum, 256);            //獲取用戶輸入ID
 5     GetDlgItemText(hwndDlg, IDC_PSWEDIT, passenger->PassWord, 256);        //獲取用戶輸入密碼
 6 
 7     if (lstrlen(passenger->PassWord) == 0){
 8         MessageBox(hwndDlg, TEXT("請輸入注冊用戶密碼!"), TEXT("信息"), MB_OK | MB_ICONINFORMATION);
 9         return FALSE;
10     }
11 
12     DialogBox(hInst, MAKEINTRESOURCE(IDD_NAMEDLG), NULL, (DLGPROC)NameDlgProc);    //獲取新注冊用戶姓名
13 
14     if (lstrlen(passenger->Name) == 0)
15         return FALSE;
16 
17     passenger->Next = NULL;
18 
19     if (psglink.head == NULL)                                        //該注冊賬戶為第一個賬戶時,頭、尾結點均指向passenger
20         psglink.head = passenger;
21     else
22         psglink.tail->Next = passenger;                                //將新注冊賬戶資料添加至賬戶鏈表
23     psglink.tail = passenger;                                        //鏈表尾指針指向鏈表尾
24     psglink.PsgNum++;                                                //注冊用戶數目加1
25     passenger->TicketNum = 0;                            
26     passenger->OrderLink.head = NULL;                                //乘客訂單鏈表初始化
27     passenger->OrderLink.tail = NULL;
28     passenger->OrderLink.OrderNum = 0;
29 
30     MessageBox(hwndDlg, TEXT("注冊成功!請單擊確定進入主界面"), TEXT("注冊成功"), MB_OK | MB_ICONINFORMATION);
31     
32     EndDialog(hwndDlg, TRUE);                                                    //關閉登陸界面
33 
34     DialogBox(hInst, MAKEINTRESOURCE(IDD_MAINDLG), NULL, (DLGPROC)MainDlgProc); //彈出主界面
35 
36     return TRUE;
37 }//AccountRegister(HWND)

 

點擊查找、訂票、退票、航班錄入、修改航班信息按鈕都會調用相應的函數。下面來看實現各個功能的代碼:

 1 //查找航班
 2 BOOL SearchFlight(HWND hMainDlg){
 3     int flag1 = 0;                                                    //if(flag1==0) 出發地、目的地查詢航班,else:航班號查航班
 4     int flag2 = 0;
 5     Flight *p;
 6     TCHAR szBuffer[256] = {TEXT("\0")};
 7     TCHAR szDeparture[16], szDestination[16], szFltNum[16], szDate[16];
 8 
 9     HWND hEdit = GetDlgItem(hMainDlg, IDC_INFORMATION);
10 
11     GetDlgItemText(hMainDlg, IDC_DEPARTURE, szDeparture, 15);        //獲取用戶輸入出發地
12     GetDlgItemText(hMainDlg, IDC_DESTINATION, szDestination, 15);    //獲取用戶輸入目的地
13     GetDlgItemText(hMainDlg, IDC_FLIGHTNUM, szFltNum, 15);            //獲取用戶輸入航班號
14     GetDlgItemText(hMainDlg, IDC_DATE, szDate, 15);                    //獲取用戶選擇航班時間
15 
16     SetDlgItemText(hMainDlg, IDC_INFORMATION, szBuffer);            //清空航班信息區域文本
17     
18 
19     if (lstrlen(szFltNum) || (!(lstrlen(szDeparture)) && !(lstrlen(szDestination))))
20         flag1++;
21     else if (!(lstrlen(szDeparture))){
22         MessageBox(hMainDlg, TEXT("請輸入出發地!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
23         return FALSE;
24     }
25     else if (!(lstrlen(szDestination))){
26         MessageBox(hMainDlg, TEXT("請輸入目的地!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
27         return FALSE;
28     }
29     if (flag1 && !(lstrlen(szFltNum))){
30         MessageBox(hMainDlg, TEXT("請輸入出發地、目的地或航班號以查詢航班!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
31         return FALSE;
32     }
33 
34     p = flightlink.head;
35     while (p){                                            //查找航班並輸出
36         if (flag1){                                        //按航班號查找
37             if (!lstrcmp(p->Flight_Number, szFltNum) && !lstrcmp(p->Date,szDate)){//查找到符合航班,輸出
38                 if (!flag2){                            //若該航班為第一班符合條件的航班
39                     _stprintf(szBuffer, TEXT("航班號\t出發地\t目的地\t起飛時間\t降落時間\t余座\t價格\t折扣\t出發日期\n"));
40                     SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
41                     SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
42                 }//if
43                 flag2++;
44                 PrintFlight(hMainDlg, p);                //輸出航班信息
45             }//if
46         }//if
47         else{                                            //按出發地、目的地查找
48             if (!lstrcmp(p->Departure, szDeparture) && !lstrcmp(p->Destination, szDestination) && !lstrcmp(p->Date, szDate)){//查找到符合航班,輸出
49                 if (!flag2){                            //若該航班為第一班符合條件的航班
50                     _stprintf(szBuffer, TEXT("航班號\t出發地\t目的地\t起飛時間\t降落時間\t余座\t價格\t折扣\t出發日期\n"));
51                     SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
52                     SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
53                 }//if
54                 flag2++;
55                 PrintFlight(hMainDlg, p);                //輸出航班信息
56             }//if
57         }//else
58         p = p->Next;
59     }//while
60     if (!flag2)
61         MessageBox(hMainDlg, TEXT("對不起,沒有符合您要求的航班"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
62     return TRUE;
63 }//SearchFlight()
 1 //將所查航班信息輸出
 2 BOOL PrintFlight(HWND hwndDlg,Flight *p){
 3     TCHAR szBuffer[256];
 4     HWND hEdit = GetDlgItem(hwndDlg, IDC_INFORMATION);
 5 
 6     _stprintf(szBuffer, TEXT("\n%s\t%s\t%s\t%s\t%s\t%d\t%.2lf\t%s\t%s\n\n"),
 7         p->Flight_Number,                //航班號
 8         p->Departure,                    //出發地
 9         p->Destination,                    //目的地
10         p->TakeOff_Time,                //起飛時間
11         p->Landing_Time,                //降落時間
12         p->Vacant_Seat,                    //余座
13         p->Fare,                        //價格
14         p->Discount,                    //折扣
15         p->Date);                        //出發日期
16                                         
17     SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
18     SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));    
19 
20     return TRUE;
21 }
 1 //訂票
 2 BOOL BookTickets(HWND hMainDlg){
 3     int tickets_num;
 4     Flight *p;
 5     TCHAR szFltNum[16], szDate[16];
 6 
 7     GetDlgItemText(hMainDlg, IDC_FLIGHTNUM, szFltNum, 15);            //獲取用戶輸入航班號
 8     GetDlgItemText(hMainDlg, IDC_DATE, szDate, 15);                    //獲取用戶選擇航班時間
 9 
10     if (!lstrlen(szFltNum)){
11         MessageBox(hMainDlg, TEXT("請輸入航班號以訂票!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
12         return FALSE;
13     }
14 
15     DialogBox(hInst, MAKEINTRESOURCE(IDD_FLTNUM), NULL, (DLGPROC)FlightNumDlgProc);    //獲取訂票數目
16 
17     if (!lstrlen(szBuffer))
18         return FALSE;
19 
20     tickets_num = _ttoi(szBuffer);                                    //訂票數目
21 
22     p = flightlink.head;
23     while (p){                                                        //查找航班並訂票
24         if (!lstrcmp(p->Flight_Number, szFltNum) && !lstrcmp(p->Date, szDate))        //查找到符合航班
25             _Book_Tickets(hMainDlg,p,tickets_num);
26         p = p->Next;
27     }//while
28     return TRUE;
29 }//BookTickets()     
30 
31 
32 //訂票
33 BOOL _Book_Tickets(HWND hwndDlg,Flight *p,int Tickets_Num){
34     OrderForm *new_order = (OrderForm *)malloc(sizeof(OrderForm));
35     if (!new_order){
36         MessageBox(hwndDlg, TEXT("內存申請錯誤!"), TEXT("錯誤"), MB_OK | MB_ICONERROR);
37         return FALSE;
38     }
39 
40     if (Tickets_Num > p->Vacant_Seat){                    //余票不足,給出建議航班
41         Recommend(hwndDlg,p,Tickets_Num);
42         return FALSE;
43     }
44 
45     new_order->Order_Number = ++allorder.AllOrderNum;    //新訂單信息,總訂單數+1
46     new_order->Tickets_Num = Tickets_Num;
47     lstrcpy(new_order->IdNum, passenger->IdNum);
48     lstrcpy(new_order->Flight_Number, p->Flight_Number);
49     lstrcpy(new_order->Departure, p->Departure);
50     lstrcpy(new_order->Destination, p->Destination);
51     lstrcpy(new_order->Date, p->Date);
52     lstrcpy(new_order->TakeOff_Time, p->TakeOff_Time);
53     lstrcpy(new_order->Landing_Time, p->Landing_Time);
54     new_order->Next = NULL;
55     new_order->psgNext = NULL;
56 
57     if (allorder.head == NULL)                            //將結點添至訂單鏈表末尾
58         allorder.head = new_order;
59     else
60         allorder.tail->Next = new_order;
61     allorder.tail = new_order;
62 
63     if (passenger->OrderLink.head == NULL)                //將結點添至用戶訂單鏈表末尾
64         passenger->OrderLink.head = new_order;
65     else
66         passenger->OrderLink.tail->psgNext = new_order;
67     passenger->OrderLink.tail = new_order;
68     passenger->OrderLink.OrderNum++;
69     passenger->TicketNum += Tickets_Num;
70 
71     p->Vacant_Seat -= Tickets_Num;                        //航班余票-1
72 
73     MessageBox(hwndDlg, TEXT("訂票成功!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
74 
75     return TRUE;
76 }//_Book_Tickets()
 1 //給出建議航班
 2 BOOL Recommend(HWND hwndDlg, Flight *p,int Tickets_Num){
 3     SetDlgItemText(hwndDlg, IDC_INFORMATION, TEXT("\0"));    //清空航班信息欄
 4 
 5     int flag = 0;
 6     TCHAR szBuffer[256] = {TEXT("\0")};
 7     HWND hEdit = GetDlgItem(hwndDlg, IDC_INFORMATION);
 8     Flight *q;
 9 
10     q = flightlink.head;
11     while (q){
12         if (!(lstrcmp(p->Departure, q->Departure)) && 
13             !(lstrcmp(p->Destination, q->Destination)) && 
14             lstrcmp(p->Flight_Number, q->Flight_Number) && 
15             Tickets_Num<=q->Vacant_Seat)
16         {
17             if (!flag){
18                 wsprintf(szBuffer, TEXT("您所訂航班余票不足,以下為建議航班:\n"));
19                 SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
20                 SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
21                 wsprintf(szBuffer, TEXT("\n"));
22                 SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
23                 SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
24                 wsprintf(szBuffer, TEXT("航班號\t出發地\t目的地\t起飛時間\t降落時間\t余座\t價格\t折扣\t出發日期\n"));
25                 SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
26                 SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
27                 ++flag;
28             }//if
29             PrintFlight(hwndDlg, q);
30         }//if
31         q = q->Next;
32     }//while
33 
34     if (!flag){
35         wsprintf(szBuffer, TEXT("對不起,您所訂航班余票不足,當天也其它沒有符合條件的航班...\n"));
36         SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
37         SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
38     }
39     return TRUE;
40 }//Recommend()
  1 //退票
  2 BOOL ReturnTickets(HWND hwndDlg){
  3     int flag = 0;
  4     int num=0,tickets_num;
  5     Flight *flight;
  6     OrderForm *p,*q,*s,*prePoint;
  7     TCHAR szFltNum[16], szDate[16];
  8 
  9     GetDlgItemText(hwndDlg, IDC_FLIGHTNUM, szFltNum, 15);        //獲取用戶輸入航班號
 10     GetDlgItemText(hwndDlg, IDC_DATE, szDate, 15);                //獲取用戶選擇航班時間
 11 
 12     if (!lstrlen(szFltNum)){                                    //未輸入航班號
 13         MessageBox(hwndDlg, TEXT("請輸入航班號!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
 14         return FALSE;
 15     }
 16 
 17     DialogBox(hInst, MAKEINTRESOURCE(IDD_FLTNUM), NULL, (DLGPROC)FlightNumDlgProc);    //獲取退票數目
 18 
 19     if (!lstrlen(szBuffer))
 20         return FALSE;
 21 
 22     tickets_num = _ttoi(szBuffer);                                //退票數
 23         
 24     p = passenger->OrderLink.head;
 25     prePoint = p;
 26     if (!p){                                                    //用戶無訂單
 27         MessageBox(hwndDlg, TEXT("當前用戶沒有已訂票"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
 28         return FALSE;
 29     }
 30     
 31     while (p){                                                    //退票航班訂票總數
 32         if (!lstrcmp(p->Flight_Number, szFltNum) && !lstrcmp(p->Date, szDate))
 33             num += p->Tickets_Num;
 34         p = p->psgNext;
 35     }
 36     if (tickets_num > num){                                        //退票數大於該航班已訂票數
 37         wsprintf(szBuffer, TEXT("對不起,該航班您只訂了%d張機票,請重新填寫退票張數"), num);
 38         MessageBox(hwndDlg, szBuffer, TEXT("提示"), MB_OK | MB_ICONINFORMATION);
 39         return FALSE;
 40     }//if
 41 
 42     p = passenger->OrderLink.head;
 43     while (p){                                                    //退票
 44         if (!lstrcmp(p->Flight_Number, szFltNum) && !lstrcmp(p->Date, szDate) ){
 45             if (tickets_num >= p->Tickets_Num){                    //當退票數大於等於當前訂單內票數
 46 
 47                 s = p->psgNext;
 48                 passenger->TicketNum -= p->Tickets_Num;            //用戶訂票數減少
 49                 allorder.AllOrderNum--;                            //總訂單數減少
 50                 passenger->OrderLink.OrderNum--;                //用戶訂單數減少
 51                 tickets_num -= p->Tickets_Num;                    //需要退票數減少  若需要退票數為0,則退出,否則繼續循環
 52 
 53                 if (passenger->OrderLink.head == p){            //若p為頭結點,刪除頭結點
 54                     passenger->OrderLink.head = p->psgNext;        //頭指針指向下一個結點
 55                     if (!p->psgNext)                            //若p同時為尾結點,則尾指針指向NULL
 56                         passenger->OrderLink.tail = NULL;
 57                 }
 58                 else if (!p->psgNext)                            //若p非頭結點但為尾結點,尾指針指向前一個結點
 59                     passenger->OrderLink.tail = prePoint;
 60                 prePoint->psgNext = p->psgNext;                    //鏈表刪除結點p
 61 
 62                 flight = flightlink.head;
 63                 while (flight){                                    //航班余座增加
 64                     if (!lstrcmp(flight->Flight_Number, szFltNum) && !lstrcmp(flight->Date, szDate)){
 65                         flight->Vacant_Seat += p->Tickets_Num;
 66                         break;
 67                     }
 68                     flight = flight->Next;
 69                 }//while
 70 
 71                 q = allorder.head;
 72                 prePoint = q;
 73                 while (q){                                        //刪除訂單鏈表中訂單結點
 74                     if (q == p){                                //找到訂單結點
 75                         if (allorder.head == p){                //若p為頭結點,刪除頭結點
 76                             allorder.head = p->Next;            //頭指針指向下一個結點
 77                             if (!q->Next)                        //若q同時為尾結點,則尾指針指向NULL
 78                                 allorder.tail = NULL;
 79                         }
 80                         else if (!q->Next)                        //若q非頭結點但為尾結點,尾指針指向前一個結點
 81                             allorder.tail = prePoint;
 82                         prePoint->Next = q->Next;                //鏈表刪除結點q     
 83                         free(p); break;                            //釋放p結點內存
 84                     }//if
 85                     prePoint = q;
 86                     q = q->Next;
 87                 }//while
 88 
 89                 if (tickets_num){                                //若退票數非0,則繼續退票
 90                     p = s; continue;
 91                 }
 92                     
 93                 MessageBox(hwndDlg, TEXT("退票成功!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
 94                 return TRUE;
 95             }//if
 96             else{                                                //退票數小於該訂單訂票數
 97                 p->Tickets_Num -= tickets_num;                    //訂單訂票數減少
 98                 passenger->TicketNum -= tickets_num;            //用戶訂票數減少
 99 
100                 flight = flightlink.head;
101                 while (flight){                                    
102                     if (!lstrcmp(flight->Flight_Number, szFltNum) && !lstrcmp(flight->Date, szDate)){
103                         flight->Vacant_Seat += tickets_num;        //航班余票增加
104                         break;
105                     }
106                     flight = flight->Next;
107                 }//while
108 
109                 MessageBox(hwndDlg, TEXT("退票成功!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
110                 return TRUE;                                    //退票成功
111             }//else
112         }//if
113         prePoint = p;
114         p = p->psgNext;
115     }//while
116 
117     wsprintf(szBuffer, TEXT("對不起,您沒有訂該航班機票,請確認退票航班"));
118     MessageBox(hwndDlg, szBuffer, TEXT("提示"), MB_OK | MB_ICONINFORMATION);
119     return FALSE;
120 }//ReturnTickets()

 

這篇文章就先到這裡吧,其余功能的實現以及頭文件、資源文件的代碼在下篇文章中給出。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved