windows API 开发飞机订票系统(三)

嘿嘿,今天继续来完成哥哥我的飞机订票系统。

废话不多说,先把主界面对话框窗口过程贴出来:

//主界面_窗口过程
BOOL CALLBACK MainDlgProc(HWND hMainDlg, UINT uMsg, WPARAM wParam, LPARAM lParam){
    switch (uMsg)
    {
    case WM_INITDIALOG:
    {
        if (hIcon)
            SendMessage(hMainDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
    }//WM_INITDIALOG
        return TRUE;

    case WM_CLOSE:
    {
        //关闭程序时将信息保存                                                
        if (WriteOrderData(hMainDlg) && WriteAccountData(hMainDlg) && WriteFlightData(hMainDlg))
            EndDialog(hMainDlg, 0);
        else
            MessageBox(hMainDlg, TEXT("保存信息错误!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
    }//WM_CLOSE
        return TRUE;

    case WM_COMMAND:
    {
        switch (LOWORD(wParam))
        {
        case IDC_SEARCH:
        {
            SearchFlight(hMainDlg);                        //查找航班    
            break;
        }
        case IDC_TICKET:
        {
            BookTickets(hMainDlg);                        //订票
            break;
        }
        case IDC_RETURNTICKET:
        {
            ReturnTickets(hMainDlg);                    //退票
            break;
        }
        case IDC_ENTRY:
        {                                                //录入航班对话框
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ENTRY), NULL, (DLGPROC)EntryFlightProc);
            break;
        }
        case IDC_MODIFYFLIGHT:
        {                                                //修改航班信息对话框
            DialogBox(hInst, MAKEINTRESOURCE(IDD_MODIFYFLT), NULL, (DLGPROC)ModifyFlightProc);
            break;
        }
        }//switch
    }//WM_COMMAND
        return TRUE;
    }//switch
    return FALSE;
}//MainDlgProc()

如上述代码所示,点击查找、订票、退票、航班录入、修改航班信息按钮都会调用相应的函数。下面来看实现各个功能的代码:

//查找航班
BOOL SearchFlight(HWND hMainDlg){
    int flag1 = 0;                                                    //if(flag1==0) 出发地、目的地查询航班,else:航班号查航班
    int flag2 = 0;
    Flight *p;
    TCHAR szBuffer[256] = {TEXT("\0")};
    TCHAR szDeparture[16], szDestination[16], szFltNum[16], szDate[16];

    HWND hEdit = GetDlgItem(hMainDlg, IDC_INFORMATION);

    GetDlgItemText(hMainDlg, IDC_DEPARTURE, szDeparture, 15);        //获取用户输入出发地
    GetDlgItemText(hMainDlg, IDC_DESTINATION, szDestination, 15);    //获取用户输入目的地
    GetDlgItemText(hMainDlg, IDC_FLIGHTNUM, szFltNum, 15);            //获取用户输入航班号
    GetDlgItemText(hMainDlg, IDC_DATE, szDate, 15);                    //获取用户选择航班时间

    SetDlgItemText(hMainDlg, IDC_INFORMATION, szBuffer);            //清空航班信息区域文本


    if (lstrlen(szFltNum) || (!(lstrlen(szDeparture)) && !(lstrlen(szDestination))))
        flag1++;
    else if (!(lstrlen(szDeparture))){
        MessageBox(hMainDlg, TEXT("请输入出发地!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }
    else if (!(lstrlen(szDestination))){
        MessageBox(hMainDlg, TEXT("请输入目的地!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }
    if (flag1 && !(lstrlen(szFltNum))){
        MessageBox(hMainDlg, TEXT("请输入出发地、目的地或航班号以查询航班!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    p = flightlink.head;
    while (p){                                            //查找航班并输出
        if (flag1){                                        //按航班号查找
            if (!lstrcmp(p->Flight_Number, szFltNum) && !lstrcmp(p->Date,szDate)){//查找到符合航班,输出
                if (!flag2){                            //若该航班为第一班符合条件的航班
                    _stprintf(szBuffer, TEXT("航班号\t出发地\t目的地\t起飞时间\t降落时间\t余座\t价格\t折扣\t出发日期\n"));
                    SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
                    SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
                }//if
                flag2++;
                PrintFlight(hMainDlg, p);                //输出航班信息
            }//if
        }//if
        else{                                            //按出发地、目的地查找
            if (!lstrcmp(p->Departure, szDeparture) && !lstrcmp(p->Destination, szDestination) && !lstrcmp(p->Date, szDate)){//查找到符合航班,输出
                if (!flag2){                            //若该航班为第一班符合条件的航班
                    _stprintf(szBuffer, TEXT("航班号\t出发地\t目的地\t起飞时间\t降落时间\t余座\t价格\t折扣\t出发日期\n"));
                    SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
                    SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
                }//if
                flag2++;
                PrintFlight(hMainDlg, p);                //输出航班信息
            }//if
        }//else
        p = p->Next;
    }//while
    if (!flag2)
        MessageBox(hMainDlg, TEXT("对不起,没有符合您要求的航班"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
    return TRUE;
}//SearchFlight()






//将所查航班信息输出
BOOL PrintFlight(HWND hwndDlg,Flight *p){
    TCHAR szBuffer[256];
    HWND hEdit = GetDlgItem(hwndDlg, IDC_INFORMATION);

    _stprintf(szBuffer, TEXT("\n%s\t%s\t%s\t%s\t%s\t%d\t%.2lf\t%s\t%s\n\n"),
        p->Flight_Number,                //航班号
        p->Departure,                    //出发地
        p->Destination,                    //目的地
        p->TakeOff_Time,                //起飞时间
        p->Landing_Time,                //降落时间
        p->Vacant_Seat,                    //余座
        p->Fare,                        //价格
        p->Discount,                    //折扣
        p->Date);                        //出发日期

    SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
    SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));    

    return TRUE;
}





//订票
BOOL BookTickets(HWND hMainDlg){
    int tickets_num;
    Flight *p;
    TCHAR szFltNum[16], szDate[16];

    GetDlgItemText(hMainDlg, IDC_FLIGHTNUM, szFltNum, 15);            //获取用户输入航班号
    GetDlgItemText(hMainDlg, IDC_DATE, szDate, 15);                    //获取用户选择航班时间

    if (!lstrlen(szFltNum)){
        MessageBox(hMainDlg, TEXT("请输入航班号以订票!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    DialogBox(hInst, MAKEINTRESOURCE(IDD_FLTNUM), NULL, (DLGPROC)FlightNumDlgProc);    //获取订票数目

    if (!lstrlen(szBuffer))
        return FALSE;

    tickets_num = _ttoi(szBuffer);                                    //订票数目

    p = flightlink.head;
    while (p){                                                        //查找航班并订票
        if (!lstrcmp(p->Flight_Number, szFltNum) && !lstrcmp(p->Date, szDate))        //查找到符合航班
            _Book_Tickets(hMainDlg,p,tickets_num);
        p = p->Next;
    }//while
    return TRUE;
}//BookTickets()     


//订票
BOOL _Book_Tickets(HWND hwndDlg,Flight *p,int Tickets_Num){
    OrderForm *new_order = (OrderForm *)malloc(sizeof(OrderForm));
    if (!new_order){
        MessageBox(hwndDlg, TEXT("内存申请错误!"), TEXT("错误"), MB_OK | MB_ICONERROR);
        return FALSE;
    }

    if (Tickets_Num > p->Vacant_Seat){                    //余票不足,给出建议航班
        Recommend(hwndDlg,p,Tickets_Num);
        return FALSE;
    }

    new_order->Order_Number = ++allorder.AllOrderNum;    //新订单信息,总订单数+1
    new_order->Tickets_Num = Tickets_Num;
    lstrcpy(new_order->IdNum, passenger->IdNum);
    lstrcpy(new_order->Flight_Number, p->Flight_Number);
    lstrcpy(new_order->Departure, p->Departure);
    lstrcpy(new_order->Destination, p->Destination);
    lstrcpy(new_order->Date, p->Date);
    lstrcpy(new_order->TakeOff_Time, p->TakeOff_Time);
    lstrcpy(new_order->Landing_Time, p->Landing_Time);
    new_order->Next = NULL;
    new_order->psgNext = NULL;

    if (allorder.head == NULL)                            //将结点添至订单链表末尾
        allorder.head = new_order;
    else
        allorder.tail->Next = new_order;
    allorder.tail = new_order;

    if (passenger->OrderLink.head == NULL)                //将结点添至用户订单链表末尾
        passenger->OrderLink.head = new_order;
    else
        passenger->OrderLink.tail->psgNext = new_order;
    passenger->OrderLink.tail = new_order;
    passenger->OrderLink.OrderNum++;
    passenger->TicketNum += Tickets_Num;

    p->Vacant_Seat -= Tickets_Num;                        //航班余票-1

    MessageBox(hwndDlg, TEXT("订票成功!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);

    return TRUE;
}//_Book_Tickets()





//给出建议航班
BOOL Recommend(HWND hwndDlg, Flight *p,int Tickets_Num){
    SetDlgItemText(hwndDlg, IDC_INFORMATION, TEXT("\0"));    //清空航班信息栏

    int flag = 0;
    TCHAR szBuffer[256] = {TEXT("\0")};
    HWND hEdit = GetDlgItem(hwndDlg, IDC_INFORMATION);
    Flight *q;

    q = flightlink.head;
    while (q){
        if (!(lstrcmp(p->Departure, q->Departure)) && 
            !(lstrcmp(p->Destination, q->Destination)) && 
            lstrcmp(p->Flight_Number, q->Flight_Number) && 
            Tickets_Num<=q->Vacant_Seat)
        {
            if (!flag){
                wsprintf(szBuffer, TEXT("您所订航班余票不足,以下为建议航班:\n"));
                SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
                SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
                wsprintf(szBuffer, TEXT("\n"));
                SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
                SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
                wsprintf(szBuffer, TEXT("航班号\t出发地\t目的地\t起飞时间\t降落时间\t余座\t价格\t折扣\t出发日期\n"));
                SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
                SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
                ++flag;
            }//if
            PrintFlight(hwndDlg, q);
        }//if
        q = q->Next;
    }//while

    if (!flag){
        wsprintf(szBuffer, TEXT("对不起,您所订航班余票不足,当天也其它没有符合条件的航班...\n"));
        SendMessage(hEdit, EM_SETSEL, LOWORD(-1), HIWORD(-1));
        SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(szBuffer));
    }
    return TRUE;
}//Recommend()





//退票
BOOL ReturnTickets(HWND hwndDlg){
    int flag = 0;
    int num=0,tickets_num;
    Flight *flight;
    OrderForm *p,*q,*s,*prePoint;
    TCHAR szFltNum[16], szDate[16];

    GetDlgItemText(hwndDlg, IDC_FLIGHTNUM, szFltNum, 15);        //获取用户输入航班号
    GetDlgItemText(hwndDlg, IDC_DATE, szDate, 15);                //获取用户选择航班时间

    if (!lstrlen(szFltNum)){                                    //未输入航班号
        MessageBox(hwndDlg, TEXT("请输入航班号!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    DialogBox(hInst, MAKEINTRESOURCE(IDD_FLTNUM), NULL, (DLGPROC)FlightNumDlgProc);    //获取退票数目

    if (!lstrlen(szBuffer))
        return FALSE;

    tickets_num = _ttoi(szBuffer);                                //退票数

    p = passenger->OrderLink.head;
    prePoint = p;
    if (!p){                                                    //用户无订单
        MessageBox(hwndDlg, TEXT("当前用户没有已订票"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    while (p){                                                    //退票航班订票总数
        if (!lstrcmp(p->Flight_Number, szFltNum) && !lstrcmp(p->Date, szDate))
            num += p->Tickets_Num;
        p = p->psgNext;
    }
    if (tickets_num > num){                                        //退票数大于该航班已订票数
        wsprintf(szBuffer, TEXT("对不起,该航班您只订了%d张机票,请重新填写退票张数"), num);
        MessageBox(hwndDlg, szBuffer, TEXT("提示"), MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }//if

    p = passenger->OrderLink.head;
    while (p){                                                    //退票
        if (!lstrcmp(p->Flight_Number, szFltNum) && !lstrcmp(p->Date, szDate) ){
            if (tickets_num >= p->Tickets_Num){                    //当退票数大于等于当前订单内票数

                s = p->psgNext;
                passenger->TicketNum -= p->Tickets_Num;            //用户订票数减少
                allorder.AllOrderNum--;                            //总订单数减少
                passenger->OrderLink.OrderNum--;                //用户订单数减少
                tickets_num -= p->Tickets_Num;                    //需要退票数减少  若需要退票数为0,则退出,否则继续循环

                if (passenger->OrderLink.head == p){            //若p为头结点,删除头结点
                    passenger->OrderLink.head = p->psgNext;        //头指针指向下一个结点
                    if (!p->psgNext)                            //若p同时为尾结点,则尾指针指向NULL
                        passenger->OrderLink.tail = NULL;
                }
                else if (!p->psgNext)                            //若p非头结点但为尾结点,尾指针指向前一个结点
                    passenger->OrderLink.tail = prePoint;
                prePoint->psgNext = p->psgNext;                    //链表删除结点p

                flight = flightlink.head;
                while (flight){                                    //航班余座增加
                    if (!lstrcmp(flight->Flight_Number, szFltNum) && !lstrcmp(flight->Date, szDate)){
                        flight->Vacant_Seat += p->Tickets_Num;
                        break;
                    }
                    flight = flight->Next;
                }//while

                q = allorder.head;
                prePoint = q;
                while (q){                                        //删除订单链表中订单结点
                    if (q == p){                                //找到订单结点
                        if (allorder.head == p){                //若p为头结点,删除头结点
                            allorder.head = p->Next;            //头指针指向下一个结点
                            if (!q->Next)                        //若q同时为尾结点,则尾指针指向NULL
                                allorder.tail = NULL;
                        }
                        else if (!q->Next)                        //若q非头结点但为尾结点,尾指针指向前一个结点
                            allorder.tail = prePoint;
                        prePoint->Next = q->Next;                //链表删除结点q     
                        free(p); break;                            //释放p结点内存
                    }//if
                    prePoint = q;
                    q = q->Next;
                }//while

                if (tickets_num){                                //若退票数非0,则继续退票
                    p = s; continue;
                }

                MessageBox(hwndDlg, TEXT("退票成功!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
                return TRUE;
            }//if
            else{                                                //退票数小于该订单订票数
                p->Tickets_Num -= tickets_num;                    //订单订票数减少
                passenger->TicketNum -= tickets_num;            //用户订票数减少

                flight = flightlink.head;
                while (flight){                                    
                    if (!lstrcmp(flight->Flight_Number, szFltNum) && !lstrcmp(flight->Date, szDate)){
                        flight->Vacant_Seat += tickets_num;        //航班余票增加
                        break;
                    }
                    flight = flight->Next;
                }//while

                MessageBox(hwndDlg, TEXT("退票成功!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
                return TRUE;                                    //退票成功
            }//else
        }//if
        prePoint = p;
        p = p->psgNext;
    }//while

    wsprintf(szBuffer, TEXT("对不起,您没有订该航班机票,请确认退票航班"));
    MessageBox(hwndDlg, szBuffer, TEXT("提示"), MB_OK | MB_ICONINFORMATION);
    return FALSE;
}//ReturnTickets()

点击订票或退票按钮时,会弹出一个对话框来让用户输入订票或退票数目,对话框窗口过程如下:

//获取用户输入机票数量_窗口过程
BOOL CALLBACK FlightNumDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam){
    switch (uMsg)
    {

    case WM_INITDIALOG:
    {
        if (hIcon)
            SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
    }//WM_INITDIALOG
    return TRUE;

    case WM_CLOSE:
    {
        wsprintf(szBuffer, TEXT("\0"));
        EndDialog(hwndDlg, FALSE);
    }//WM_CLOSE
    return TRUE;

    case WM_COMMAND:
    {
        switch (LOWORD(wParam))
        {
        case IDOK:
            wsprintf(szBuffer, TEXT("\0"));
            GetDlgItemText(hwndDlg, IDC_TICKETNUM, szBuffer, 256);
            EndDialog(hwndDlg, TRUE);
            break;
        }//switch
    }//WM_COMMAND
    return TRUE;

    }//switch
    return FALSE;
}

接下来是航班录入和修改航班信息功能的实现,分别对应一个对话框,窗口过程分别如下:

//录入航班_窗口过程
BOOL CALLBACK EntryFlightProc(HWND hEntryDlg, UINT uMsg, WPARAM wParam, LPARAM lParam){
    switch (uMsg)
    {
    case WM_INITDIALOG:
    {
        if (hIcon)
            SendMessage(hEntryDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
    }
    return TRUE;

    case WM_CLOSE:
    {
        EndDialog(hEntryDlg, 0);
    }//WM_CLOSE
    return TRUE;

    case WM_COMMAND:
    {
        switch (LOWORD(wParam))
        {
        case IDC_ADDFLT:
        {
            EntryFlight(hEntryDlg);                        //录入航班
            break;
        }//GETNAME
        case IDC_QUIT:
        {
            EndDialog(hEntryDlg, FALSE);
            break;
        }//GETNAMECANCEL

        }//stitch

    }//WM_COMMAND
    return TRUE;

    }//stitch

    return FALSE;
}


//修改航班信息_窗口过程
BOOL CALLBACK ModifyFlightProc(HWND hModifyDlg, UINT uMsg, WPARAM wParam, LPARAM lParam){
    switch (uMsg)
    {
    case WM_INITDIALOG:
    {
        if (hIcon)
            SendMessage(hModifyDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
    }
    return TRUE;

    case WM_CLOSE:
    {
        EndDialog(hModifyDlg, 0);
    }//WM_CLOSE
    return TRUE;

    case WM_COMMAND:
    {
        switch (LOWORD(wParam))
        {
        case IDC_MODIFY:
        {
            ModifyFlight(hModifyDlg);                    //修改航班信息
            break;
        }//GETNAME
        case IDC_QUIT:
        {
            EndDialog(hModifyDlg, FALSE);
            break;
        }//GETNAMECANCEL

        }//stitch

    }//WM_COMMAND
    return TRUE;
    }//stitch

    return FALSE;
}//ModifyFlightProc()

具体功能实现代码:

//录入航班
BOOL EntryFlight(HWND hEntryDlg){

    TCHAR szDiscount[16];
    TCHAR szFare[16],szSeat_Number[16];
    TCHAR szFlight_Number[16], szDate[16];
    TCHAR szDeparture[16], szDestination[16];
    TCHAR szTakeOff_Time[16], szLanding_Time[16];

    Flight *p = (Flight *)malloc(sizeof(Flight)),*q;
    if (!p){
        MessageBox(hEntryDlg, TEXT("内存申请错误!"), TEXT("错误"), MB_OK | MB_ICONERROR);
        return FALSE;
    }

    GetDlgItemText(hEntryDlg, IDC_DEPARTURE, szDeparture, 16);            //获取录入航班信息
    GetDlgItemText(hEntryDlg, IDC_DESTINATION, szDestination, 16);
    GetDlgItemText(hEntryDlg, IDC_FLTNUM, szFlight_Number, 16);
    GetDlgItemText(hEntryDlg, IDC_FARE, szFare, 16);
    GetDlgItemText(hEntryDlg, IDC_TAKEOFFTIME, szTakeOff_Time, 16);
    GetDlgItemText(hEntryDlg, IDC_LANDINGTIME, szLanding_Time, 16);
    GetDlgItemText(hEntryDlg, IDC_SEATNUM, szSeat_Number, 16);
    GetDlgItemText(hEntryDlg, IDC_DISCOUNT, szDiscount, 16);
    GetDlgItemText(hEntryDlg, IDC_DATE, szDate, 16);

    if (!lstrlen(szDeparture) || !lstrlen(szDestination) ||
        !lstrlen(szFlight_Number) || !lstrlen(szFare) ||
        !lstrlen(szTakeOff_Time) || !lstrlen(szLanding_Time) ||
        !lstrlen(szSeat_Number))
    {
        MessageBox(hEntryDlg, TEXT("请您填写完整航班信息!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    q = flightlink.head;
    while (q){
        if (!lstrcmp(q->Flight_Number, szFlight_Number) && !lstrcmp(q->Date,szDate)){
            MessageBox(hEntryDlg, TEXT("对不起,同一航班在一天内只能有一班"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
            free(p);
            return FALSE;
        }
        q = q->Next;
    }//while

    lstrcpy(p->Date, szDate);                            //填入录入航班结点信息
    lstrcpy(p->Discount, szDiscount);
    lstrcpy(p->Departure, szDeparture);
    lstrcpy(p->Destination, szDestination);
    lstrcpy(p->TakeOff_Time, szTakeOff_Time);
    lstrcpy(p->Landing_Time, szLanding_Time);
    lstrcpy(p->Flight_Number, szFlight_Number);
    p->Fare = _ttof(szFare);
    p->Seat_Number = _ttoi(szSeat_Number);
    p->Vacant_Seat = p->Seat_Number;
    p->Next = NULL;

    if (flightlink.head == NULL)                        //将新结点添至航班链表
        flightlink.head = p;
    else
        flightlink.tail->Next = p;
    flightlink.tail = p;
    flightlink.Flight_Number++;                            //航班数目增加

    MessageBox(hEntryDlg, TEXT("航班录入成功!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);

    return TRUE;
}//EntryFlight()





//修改航班信息
BOOL ModifyFlight(HWND hModifyDlg){
    int seat_num;
    TCHAR szDiscount[16];
    TCHAR szFare[16], szSeat_Number[16];
    TCHAR szTakeOff_Time[16], szLanding_Time[16];
    TCHAR Mdf_flt_num[16], Mdf_flt_date[16];

    OrderForm *q;
    Flight *p;

    GetDlgItemText(hModifyDlg, IDC_MDFFLTNUM, Mdf_flt_num, 16);                //获取要修改的航班
    GetDlgItemText(hModifyDlg, IDC_MDFDATE, Mdf_flt_date, 16);

    if (!lstrlen(Mdf_flt_num)){
        MessageBox(hModifyDlg, TEXT("请输入所要修改信息的航班号"), TEXT("信息"), MB_OK | MB_ICONINFORMATION);
        return FALSE;
    }

    GetDlgItemText(hModifyDlg, IDC_FARE, szFare, 16);                        //获取要修改的信息
    GetDlgItemText(hModifyDlg, IDC_DISCOUNT, szDiscount, 16);
    GetDlgItemText(hModifyDlg, IDC_SEATNUM, szSeat_Number, 16);
    GetDlgItemText(hModifyDlg, IDC_TAKEOFFTIME, szTakeOff_Time, 16);
    GetDlgItemText(hModifyDlg, IDC_LANDINGTIME, szLanding_Time, 16);

    p = flightlink.head;
    while (p){                                                                //查找要修改的航班
        if (!lstrcmp(Mdf_flt_num, p->Flight_Number) && !lstrcmp(Mdf_flt_date, p->Date)){
            if (lstrlen(szFare))                                            //改变航班票价
                p->Fare = _ttof(szFare);
            if (lstrlen(szDiscount))                                        //改变航班折扣
                wsprintf(p->Discount, szDiscount);
            if (lstrlen(szSeat_Number)){
                seat_num = _ttoi(szSeat_Number);
                if ((p->Seat_Number - seat_num) > p->Vacant_Seat){            //余座数量>=0
                    MessageBox(hModifyDlg, TEXT("对不起,座位数过少,请重新填写"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
                    return FALSE;
                }
                p->Vacant_Seat -= p->Seat_Number - seat_num;                //改变余票数量
                p->Seat_Number = seat_num;                                    //改变座位数
            }//if
            if (lstrlen(szTakeOff_Time)){                                    //改变航班起飞时间
                wsprintf(p->TakeOff_Time, szTakeOff_Time);
                q = allorder.head;                                            //改变该航班所有订单出发时间
                while (q){
                    if (!lstrcmp(q->Flight_Number, p->Flight_Number) && !lstrcmp(q->Date, p->Date))
                        wsprintf(q->TakeOff_Time, szTakeOff_Time);
                    q = q->Next;
                } 
            }//if
            if (lstrlen(szLanding_Time)){                                    //改变航班降落时间
                wsprintf(p->Landing_Time, szLanding_Time);
                q = allorder.head;                                            //改变该航班所有订单降落时间
                while (q) {
                    if (!lstrcmp(q->Flight_Number, p->Flight_Number) && !lstrcmp(q->Date, p->Date))
                        wsprintf(q->Landing_Time, szLanding_Time);
                    q = q->Next;
                }
            }//if
            MessageBox(hModifyDlg, TEXT("修改成功!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
            return TRUE;
        }//if
        p = p->Next;
    }//while

    MessageBox(hModifyDlg, TEXT("对不起,您所要修改的航班尚未录入。"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
    return FALSE;                                                            

}//ModifyFlight()

至此,订票系统的功能已经基本全部实现了,剩下的问题就是在程序退出时将账户、航班、订单数据写进文件里。

具体实现起来很简单,代码如下:

//保存账户资料
BOOL WriteAccountData(HWND hMainDlg){
    Passenger *p;
    FILE *fp;
    if ((fp = fopen(".\\AccountData.txt", "w")) == NULL){
        MessageBox(hMainDlg, TEXT("账户信息保存出现错误!"), TEXT("错误"), MB_OK | MB_ICONERROR);
        return FALSE;
    }

    p = psglink.head;
    while (p){
        fprintf(fp, "%s\n%s\n%s\n", p->Name, p->IdNum, p->PassWord);
        p = p->Next;
    }//while

    fclose(fp);
    return TRUE;
}//WriteAccountData


//保存订单信息
BOOL WriteOrderData(HWND hMainDlg){
    OrderForm *p;
    FILE *fp;
    if ((fp = fopen(".\\OrderForm.txt", "w")) == NULL){
        MessageBox(hMainDlg, TEXT("订单信息保存出现错误!"), TEXT("错误"), MB_OK | MB_ICONERROR);
        return FALSE;
    }
    p = allorder.head;
    while (p){
        fprintf(fp, "%s\n", p->IdNum);
        fprintf(fp, "%d\n", p->Order_Number);
        fprintf(fp, "%d\n", p->Tickets_Num);
        fprintf(fp, "%s\n", p->Flight_Number);
        fprintf(fp, "%s\n", p->Departure);
        fprintf(fp, "%s\n", p->Destination);
        fprintf(fp, "%s\n", p->Date);
        fprintf(fp, "%s\n", p->TakeOff_Time);
        fprintf(fp, "%s\n", p->Landing_Time);
        p = p->Next;
    }//while
    fclose(fp);
    return TRUE;
}//WriteOrderData()


//保存航班信息
BOOL WriteFlightData(HWND hMainDlg){
    Flight *p;
    FILE *fp;
    if ((fp = fopen(".\\FlightData.txt", "w")) == NULL){
        MessageBox(hMainDlg, TEXT("航班信息保存出现错误!"), TEXT("错误"), MB_OK | MB_ICONERROR);
        return FALSE;
    }
    p = flightlink.head;
    while (p){
        fprintf(fp, "%s\n", p->Flight_Number);
        fprintf(fp, "%.2lf\n", p->Fare);
        fprintf(fp, "%s\n", p->Discount);
        fprintf(fp, "%d\n%d\n", p->Seat_Number, p->Vacant_Seat);
        fprintf(fp, "%s\n", p->Departure);
        fprintf(fp, "%s\n", p->Destination);
        fprintf(fp, "%s\n", p->Date);
        fprintf(fp, "%s\n", p->TakeOff_Time);
        fprintf(fp, "%s\n", p->Landing_Time);
        p = p->Next;
    }//while
    fclose(fp);
    return TRUE;
}//WriteFlight()

至此,我的处女作就算是完成了,其中还是有很多不足之处。查找算法只是简单的遍历链表,数据结构也只是采用了单链表,倒是订单用上了十字链表来存储。查找起来效率不高,一旦数据量变大,便会很耗费时间,不过考虑到这只是一个课程设计,数据量很小,查找起来时间耗费上没有多少差别,姑且就这样吧。

坚持原创技术分享,您的支持将鼓励我继续创作!
0%