win API之航空客运订票系统(课程设计)

  这学期的数据结构课结了,上个周做了课程设计,在此总结下(晒一下)


  先是老师给了选题,我选了航空客运订票系统这个选题。


  这个系统定义的也很简单,就是两个单链表,一个航班的,一个顾客的,再对它俩进行遍历、增加、查找、删除,来实现订票、取消订票、查询顾客、查询航班。


  可是课程设计为期一个星期,总不能这么就完了吧,这不就啥也没干嘛!      我转念一想,不行!我必须做出和其他人不一样的程序。


  可是栈、图、串在这个项目里好像用不到啊,开始想把单链表换成双链表,但感觉和大家比起来没啥突破。

  最后决定做win32程序。

  由于程序要在xp环境下运行,编译器用vc++6.0。由于编译器过于老旧必须下一个Strsafe相关的头文件以及链接库。

考虑到程序的规模不是很大,就用API来做。下面附上代码。

StdAfx.h头文件(关于数据结构和部分函数的定义)

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include <windows.h>
#include <conio.h>
#include<stdio.h> 
#include<malloc.h>
#include<string.h> 
#define OK 1 
#define ERROR 0 
	 typedef struct airlinezzz
 {  /* 飞机航班的结构定义 */         
	 char air_num[8];         
	 char plane_num[8];         
	 char end_place[20];         
	 int  total;         
	 int  left;          
	 struct airlinezzz *next; 
 }airlinezzz;  
 typedef struct customerzzz
 { /*顾客信息的结构定义 */         
	 char name[8];         
	 char air_num[8];  
	 int seat_num;         
	 struct customerzzz *next; 
 }customerzzz; 
airlinezzz *start_air();   /*创建航班链表 */ 
customerzzz *start_cus(); /*创建顾客链表 */ 
airlinezzz *modefy_airline(airlinezzz *l,char *air_num); /* 修改航班的空余座位信息 */ 

int insert_air(airlinezzz **p,char *air_num,char *plane_num,char *end_place,int total,int left); /* 增加航班信息*/ 

int  insert_cus(customerzzz **p,char *name,char *air_num,int seat_num); /*增加某航班的顾客信息*/ 
int book(airlinezzz *a,char *air_num,customerzzz *c,char *name); /*订票操作*/ 
int del_cus(customerzzz *c,airlinezzz *l,char *name); /*取消订票信息操作*/ 
int search_air(airlinezzz *head,HWND hwnd); /*查找航班信息操作*/ 

int search_cus(customerzzz *head,HWND hwnd);/*查找顾客信息操作*/ 

 
int creat_air(airlinezzz **l); /*预先设置航班信息*/ 

int creat_cus(customerzzz **l);/*预先设置已订票的顾客信息*/ 

typedef struct 
{ 
    TCHAR achName[MAX_PATH]; 
    TCHAR achPosition[12]; 
    int nGamesPlayed; 
    int nGoalsScored; 
} Player; 


 
// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)


StdAfx.cpp(对于头文件里面定义的函数进行实现)

// stdafx.cpp : source file that includes just the standard includes
//	asd.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
#include "resource.h" 
#include "StdAfx.h"
HWND hwnd;
airlinezzz *start_air()   /*创建航班链表 */ 
 {          
	 airlinezzz *a;
	 a=(airlinezzz*)malloc(sizeof(airlinezzz));
	 if(a==NULL)        
		 MessageBox(hwnd,"空间不足","失败",0); 
	 return a;
 }  
 customerzzz *start_cus() /*创建顾客链表 */ 
 {          
	 customerzzz *c;
	 c=(customerzzz*)malloc(sizeof(customerzzz));
	 if(c==NULL)           
		 MessageBox(hwnd,"空间不足","失败",0);         
	 return c; 
 }  
 airlinezzz *modefy_airline(airlinezzz *l,char *air_num) /* 修改航班的空余座位信息 */ 
 {          
	 airlinezzz *p;
	 p=l->next;
	 for(;p!=NULL;p=p->next)
	 {
		 if(strcmp(air_num,p->air_num)==0)
		 {                          
			 p->left++;
			 return l;
		 }
		 MessageBox(hwnd,"没有这个人搭乘的航班","取消信息",0); 
		 return 0;
	 }
 }  
 int insert_air(airlinezzz **p,char *air_num,char *plane_num,char *end_place,int total,int left) /* 增加航班信息*/ 
 {          
	 airlinezzz *q;
	 q=(airlinezzz*)malloc(sizeof(airlinezzz));
	 strcpy(q->air_num,air_num);
	 strcpy(q->plane_num,plane_num);
	 strcpy(q->end_place,end_place);
	 q->total=total;
	 q->left=left;
	 q->next=NULL;
	 (*p)->next=q; 
	 (*p)=(*p)->next;   
	 return OK;  
 }  
 int  insert_cus(customerzzz **p,char *name,char *air_num,int seat_num) /*增加某航班的顾客信息*/ 
 {       
	 customerzzz *q;    
	 q=(customerzzz*)malloc(sizeof(customerzzz));   
	 strcpy(q->name,name);  
	 strcpy(q->air_num,air_num);   
	 q->seat_num=seat_num;  
	 q->next=NULL; 
	 (*p)->next=q; 
	 (*p)=(*p)->next;   
	 return OK; 
} 
 
int book(airlinezzz *a,char *air_num,customerzzz *c,char *name) /*订票操作*/ 
{ 
	airlinezzz *p=a; 
	customerzzz *q=c->next; 
	p=a->next;  
	for(;q->next!=NULL;q=q->next){} 
	for(;p->next!=NULL;p=p->next) 
	{  
		if(p->left>0) 
		{  
			//printf("Your seat number is %d",(p->total-p->left+1)); 
			insert_cus(&q,name,air_num,p->total-p->left+1); 
	
			p->left--; 
			return OK;   
		} 
		else 
		{  
			MessageBox(hwnd,"座位满了","失败",0); 
			return 0; 
		} 
	} 
} 
  
int del_cus(customerzzz *c,airlinezzz *l,char *name) /*取消订票信息操作*/ 
{  
	customerzzz *p,*pr; 
	char air_num[8]; 
	pr=c; 
	p=pr->next; 
	while(p!=NULL) 
	{  
		if(strcmp(p->name,name)==0) 
		{  
			strcpy(air_num,p->air_num); 
			l=modefy_airline(l,air_num);  
			pr->next=p->next; 
			p=pr->next; 
			MessageBox(hwnd,"取消订票成功","成功",0); 
			return OK; 
		}  
		pr=pr->next; 
		p=pr->next; 
	}   
	MessageBox(hwnd,"没有这个人","失败",0);  
	return ERROR; 
}
  
int search_air(airlinezzz *head,HWND hwnd) /*查找航班信息操作*/ 
{
	airlinezzz *p=head->next;  
	//printf("air_num  plane_num  end_place  total  left\n"); 
	 HWND hwndList = GetDlgItem(hwnd, IDC_LISTBOX_EXAMPLE);  
	for(int i=1;p!=NULL;p=p->next,i++) 
	{
		int pos = (int)SendMessage(hwndList, LB_ADDSTRING,0, (LPARAM)p->air_num); 
        SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM) i); 
		//printf("%s%-10s %-8s%-8d%-8d\n",p->air_num,p->plane_num,p->end_place,p->total,p->left); 
	} 
	SetFocus(hwndList); 
	return OK; 
} 
 
int search_cus(customerzzz *head,HWND hwnd) /*查找顾客信息操作*/ 
{  
	customerzzz *q=head->next;  
	//printf("name   air_num  seat_num\n"); 
	HWND hwndList = GetDlgItem(hwnd, IDC_LISTBOX_EXAMPLE1);  
	for(int i=1;q!=NULL;q=q->next,i++) 
	{
		int pos = (int)SendMessage(hwndList, LB_ADDSTRING,0, (LPARAM)q->name); 
        SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM) i); 
		//printf("%-8s%-12s%-d\n",q->name,q->air_num,q->seat_num); 
	} 
	SetFocus(hwndList); 
	return OK; 
} 

 
int creat_air(airlinezzz **l) /*预先设置航班信息*/ 
{  
	airlinezzz *p=*l; 
	int i=0;  
	char *air_num[3]={"007af","008af","009af"}; 
	char *plane_num[3]={"plane1","plane2","plane3"}; 
	char *end_place[3]={"Beijing","Shanghai","Tianjin"}; 
	int total[3]={100,110,120}; 
	int left[3]={52,54,76}; 
	for(i=0;i<3;i++)  
		insert_air(&p,air_num[i],plane_num[i],end_place[i],total[i],left[i]); 
	return OK; 
}
  
int creat_cus(customerzzz **l)/*预先设置已订票的顾客信息*/ 
{ 
	customerzzz *p=*l; 
	int i=0;  
	char *name[3]={"zhsan","lisi","wangwu"};
	char *air_num[3]={"007af","008af","009af"}; 
	int seat_num[3]={2,5,7}; 
	for(i=0;i<3;i++) 
		insert_cus(&p,name[i],air_num[i],seat_num[i]);  
	return OK; 
 
} 

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file


最后是win32程序的入口asd.cpp

// asd.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "strsafe.h"
HINSTANCE hInstance;
customerzzz *cus=start_cus(); 
airlinezzz *air=start_air(); 
char name[8]="\0";
char air_num[8]="\0";
char ch; 
HBITMAP bg;
HBITMAP MM;
BOOL WINAPI MY(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam); 
BOOL WINAPI BOOK_1(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam); 
BOOL WINAPI CANCEL_1(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
BOOL WINAPI SEARCH_1(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);  
BOOL WINAPI SEARCH_2(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);  

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	bg=LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_1));

	creat_air(&air); 
	creat_cus(&cus);
	DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_1),NULL,MY,NULL); 
	return 0;
}
BOOL WINAPI MY(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{ 

	switch(msg)
	{
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			RECT rt;
			HDC hdc=BeginPaint(hwnd,&ps);
			//HBITMAP bg=LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_1));
			HBRUSH bgg=CreatePatternBrush(bg);
			SelectObject(hdc,bgg);
			GetWindowRect(hwnd,&rt);
			Rectangle(hdc,0,0,rt.right,rt.bottom);
		}
		break;
	case WM_COMMAND: 
		{     
			switch(LOWORD(wParam))     
			{     
			case IDBOOK:         
				{   
					DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_BOOK),NULL,BOOK_1,NULL);
				
					break;         
				}     
			case IDCANCEL:         
				{           
					DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_CANCEL),NULL,CANCEL_1,NULL);
					break;      
				}    
			
			case IDSEARCH1:
				{
					DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_SEARCH1),NULL,SEARCH_1,NULL);
					break;
				}
			case IDSEARCH2:
				{
					DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_SEARCH2),NULL,SEARCH_2,NULL);
					break;
				}
			}
			default:   
			break; 
		} 
	case WM_CLOSE:     
		{         
			DestroyWindow(hwnd);    
		}    
		break; 
	} 
	return 0; 


}



BOOL WINAPI BOOK_1(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{	
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			RECT rt;
			HDC hdc=BeginPaint(hwnd,&ps);
			//HBITMAP bg=LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_1));
			HBRUSH bgg=CreatePatternBrush(bg);
			SelectObject(hdc,bgg);
			GetWindowRect(hwnd,&rt);
			Rectangle(hdc,0,0,rt.right,rt.bottom);
		}
		break;
	case WM_COMMAND: 
		{     
			switch(LOWORD(wParam))     
			{
				
			case IDOK:         
				{
					char* qweqwe="\0";
						HWND a=GetDlgItem(hwnd,IDC_1);           //获取编辑框的句柄
						GetWindowText(a,air_num,8);                //把该句柄中的文字复制到cha1中
						HWND b=GetDlgItem(hwnd,IDC_2);
						GetWindowText(b,name,8);
						book(air,air_num,cus,name); 
						MessageBox(hwnd,"录入成功","成功",0);
						//}
					break;         
				}     
		
			} default:   
			break; 
		} 
	case WM_CLOSE:     
		{         
			DestroyWindow(hwnd);    
		}    
		break; 
	} 
	return 0; 


}


BOOL WINAPI CANCEL_1(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{ 
			case WM_PAINT:
		{
			PAINTSTRUCT ps;
			RECT rt;
			HDC hdc=BeginPaint(hwnd,&ps);
			//HBITMAP bg=LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_1));
			HBRUSH bgg=CreatePatternBrush(bg);
			SelectObject(hdc,bgg);
			GetWindowRect(hwnd,&rt);
			Rectangle(hdc,0,0,rt.right,rt.bottom);
		}
		break;
	case WM_COMMAND: 
		{     
			switch(LOWORD(wParam))     
			{     
			case IDOK:         
				{ 
					
						HWND b=GetDlgItem(hwnd,IDC_3);
						GetWindowText(b,name,8);
						del_cus(cus,air,name);

					break;         
				}     

			} default:   
			break; 
		} 
	case WM_CLOSE:     
		{         
			DestroyWindow(hwnd);    
		}    
		break; 
	} 
	return 0; 


}

BOOL WINAPI SEARCH_1(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	airlinezzz *p=air; 
	switch(msg)
	{ 
			case WM_PAINT:
		{
			PAINTSTRUCT ps;
			RECT rt;
			HDC hdc=BeginPaint(hwnd,&ps);
			//HBITMAP bg=LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_1));
			HBRUSH bgg=CreatePatternBrush(bg);
			SelectObject(hdc,bgg);
			GetWindowRect(hwnd,&rt);
			Rectangle(hdc,0,0,rt.right,rt.bottom);
		}
		break;
		case WM_INITDIALOG:
        {
            // Add items to list. 
			search_air(air,hwnd);                         //封装起来关于   将airnum引入列表框
			return TRUE;
        } 

    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
 
        case IDCANCEL:
            EndDialog(hwnd, LOWORD(wParam));
            return TRUE;

        case IDC_LISTBOX_EXAMPLE:                         //当鼠标点击了类表框
            {
                switch (HIWORD(wParam)) 
                { 
                case LBN_SELCHANGE:
                    {
                        HWND hwndList = GetDlgItem(hwnd, IDC_LISTBOX_EXAMPLE); 

                        int lbItem = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0); 

                        int i = (int)SendMessage(hwndList, LB_GETITEMDATA, lbItem, 0);
						for(int q=0;q<i;q++)
						{
							p=p->next;
						}
                        // Do something with the data from Roster[i]
                        char buff[90];
                        StringCbPrintf(buff, 90,  
							TEXT("航班号: %s\n目的地: %s\n座位总数: %d\n剩余座位数: %d"), 
                            p->plane_num, p->end_place, 
                            p->total,p->left);

                        SetDlgItemText(hwnd, IDC_STATISTICS, buff); 
                        return TRUE; 
                    } 
                }
            }
            return TRUE;
        }

	} 
	return 0; 


}





BOOL WINAPI SEARCH_2(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	customerzzz *p=cus; 
	switch(msg)
	{ 
			case WM_PAINT:
		{
			PAINTSTRUCT ps;
			RECT rt;
			HDC hdc=BeginPaint(hwnd,&ps);
			//HBITMAP bg=LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_1));
			HBRUSH bgg=CreatePatternBrush(bg);
			SelectObject(hdc,bgg);
			GetWindowRect(hwnd,&rt);
			Rectangle(hdc,0,0,rt.right,rt.bottom);
		}
		break;
		case WM_INITDIALOG:
        {
            // Add items to list. 
			search_cus(cus,hwnd);                         //封装起来关于   将airnum引入列表框
			return TRUE;
        } 

    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
  
        case IDCANCEL:
            EndDialog(hwnd, LOWORD(wParam));
            return TRUE;

        case IDC_LISTBOX_EXAMPLE1:                         //当鼠标点击了类表框
            {
                switch (HIWORD(wParam)) 
                { 
                case LBN_SELCHANGE:
                    {
                        HWND hwndList = GetDlgItem(hwnd, IDC_LISTBOX_EXAMPLE1); 

                        int lbItem = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0); 

                        int i = (int)SendMessage(hwndList, LB_GETITEMDATA, lbItem, 0);
						for(int q=0;q<i;q++)
						{
							p=p->next;
						}
                        // Do something with the data from Roster[i]
                        char buff[90];
                        StringCbPrintf(buff, 90,  
							TEXT("航班号: %s\n座位号: %d\n"), 
                            p->air_num, p->seat_num);

                        SetDlgItemText(hwnd, IDC_STATISTICS1, buff); 
                        return TRUE; 
                    } 
                }
            }
            return TRUE;
        }

	} 
	return 0; 


}

然后关于资源文件我就简单提一下,有五个对话窗口,一个重绘背景的图,一个图片控件的图,还有一个图标


运行程序

打开DEBUG文件夹下的


进入界面


各功能都测试过,没有问题(前提是订票之前先查看有哪些航班后,在输入航班号的时候千万不要输错,在这点上我做的不太人性化)

关于测试图我就不一一发了,我直接把debug文件夹打包后上传到资源里,感兴趣可以下载下来玩玩。

下载点击这里   http://download.csdn.net/detail/weixing11/6748081


关于对成果的不足之处:

1.在订票界面,输入航班的编辑框完全可以用一个下拉菜单来代替,毕竟用鼠标来选择当前现有航班就不会出错了。

2.按钮本来想重绘的,每个不同窗口背景也想重绘成不同背景,但是嫌麻烦,就没实现。

3.如果添加一个提示当前时间还有天气的控件能更人性化(嫌麻烦没做.....)

4.如果再添加一个“程序载入中”的模块再添点音效就更好了。


得到的收获:自娱自乐又完成了课程设计,两全其美。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值