⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lookupdlg.cpp

📁 是我们学期末的数据库课程设计
💻 CPP
字号:
// LookupDlg.cpp : implementation file
//

#include "stdafx.h"
#include "plane.h"
#include "LookupDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLookupDlg dialog


CLookupDlg::CLookupDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLookupDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLookupDlg)
	m_end = _T("");
	m_hb = _T("");
	m_start = _T("");
	m_time = _T("");
	//}}AFX_DATA_INIT
}
BOOL CLookupDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// Add "About..." menu item to system menu.
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	m_show.InsertColumn(0,"航班号");
	m_show.InsertColumn(1,"起点站");
	m_show.InsertColumn(2,"终点站");
	m_show.InsertColumn(3,"航班期");
	m_show.InsertColumn(4,"起飞时间");
	m_show.InsertColumn(5,"到达时间");
	m_show.InsertColumn(6,"机型");
	m_show.InsertColumn(7,"票价");
	RECT rect;
	m_show.GetWindowRect(&rect);
	int wid = rect.right - rect.left;
	m_show.SetColumnWidth(0,wid/8);
	m_show.SetColumnWidth(1,wid/8);
	m_show.SetColumnWidth(2,wid/8);
	m_show.SetColumnWidth(3,wid/8);
	m_show.SetColumnWidth(4,wid/8);
	m_show.SetColumnWidth(5,wid/8);
	m_show.SetColumnWidth(6,wid/8);
	m_show.SetColumnWidth(7,wid/8);
	m_show.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_show.SetBkColor(RGB(177,151,240));
	m_show.SetTextBkColor(RGB(177,151,240));

	L.keynum=6;L.length=0;

	CStdioFile file;
	char*pFileName="data.txt";
	if(!file.Open(pFileName,CFile::modeRead|CFile::typeText))
	{
      #ifdef _DEBUG
		afxDump<<"抱歉,系统无法打开数据文件,可能是文件损坏!"<<"\n";
	#endif
		exit(1);
	}
	CString hb,start,end,banji,stime,etime,jixing,price;
	BOOL lpstr=1;
	int j=0;
	for(int i=0;lpstr;)
	{
		lpstr=file.ReadString(hb);
			m_show.InsertItem(i,hb);
		lpstr=file.ReadString(start);
			m_show.SetItemText(i,1,start);
		lpstr=file.ReadString(end);
			m_show.SetItemText(i,2,end);
		lpstr=file.ReadString(banji);
			m_show.SetItemText(i,3,banji);
		lpstr=file.ReadString(stime);
			m_show.SetItemText(i,4,stime);
		lpstr=file.ReadString(etime);
			m_show.SetItemText(i,5,etime);
		lpstr=file.ReadString(jixing);
			m_show.SetItemText(i,6,jixing);
		lpstr=file.ReadString(price);	
			m_show.SetItemText(i,7,price);
		if(lpstr)
		{
			i++;
			for(j=0;j<hb.GetLength();j++)
				L.sl[i].keys[j]=hb[j];
			L.sl[i].keys[j]='\0';

			for(j=0;j<start.GetLength();j++)
				L.sl[i].others.start[j]=start[j];
			L.sl[i].others.start[j]='\0';

			for(j=0;j<end.GetLength();j++)
				L.sl[i].others.end[j]=end[j];
			L.sl[i].others.end[j]='\0';

			for(j=0;j<banji.GetLength();j++)
				L.sl[i].others.banji[j]=banji[j];
			L.sl[i].others.banji[j]='\0';

			for(j=0;j<stime.GetLength();j++)
				L.sl[i].others.stime[j]=stime[j];
			L.sl[i].others.stime[j]='\0';

			for(j=0;j<etime.GetLength();j++)
				L.sl[i].others.etime[j]=etime[j];
			L.sl[i].others.etime[j]='\0';

			for(j=0;j<jixing.GetLength();j++)
				L.sl[i].others.jixing[j]=jixing[j];
			L.sl[i].others.jixing[j]='\0';

			for(j=0;j<price.GetLength();j++)
				L.sl[i].others.price[j]=price[j];
			L.sl[i].others.price[j]='\0';
		}
	}
	L.length=i;
	file.Close();
	return TRUE;  
}

void CLookupDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLookupDlg)
	DDX_Control(pDX, IDC_SHOW, m_show);
	DDX_Text(pDX, IDC_END, m_end);
	DDV_MaxChars(pDX, m_end, 15);
	DDX_Text(pDX, IDC_HB, m_hb);
	DDV_MaxChars(pDX, m_hb, 15);
	DDX_Text(pDX, IDC_START, m_start);
	DDV_MaxChars(pDX, m_start, 15);
	DDX_Text(pDX, IDC_TIME, m_time);
	DDV_MaxChars(pDX, m_time, 15);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLookupDlg, CDialog)
	//{{AFX_MSG_MAP(CLookupDlg)
	ON_BN_CLICKED(IDC_LOOK, OnLook)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLookupDlg message handlers

////////////////////////////////////////////////////////////
void CLookupDlg::OnLook() 
{
	UpdateData(TRUE);
	if(m_hb!=""||m_start!=""||m_end!=""||m_time!="")
	{
		if(m_hb=="")
			SeqSearch(m_start,m_end,m_time);
		else
			BinSearch(m_hb);
	}
	else
	{
		m_show.DeleteAllItems();
		for(int i=0;i<L.length;i++)
		{
			m_show.InsertItem(i,(CString)L.sl[i+1].keys);
			m_show.SetItemText(i,1,(CString)L.sl[i+1].others.start);
			m_show.SetItemText(i,2,(CString)L.sl[i+1].others.end);
			m_show.SetItemText(i,3,(CString)L.sl[i+1].others.banji);
			m_show.SetItemText(i,4,(CString)L.sl[i+1].others.stime);
			m_show.SetItemText(i,5,(CString)L.sl[i+1].others.etime);
			m_show.SetItemText(i,6,(CString)L.sl[i+1].others.jixing);
			m_show.SetItemText(i,7,(CString)L.sl[i+1].others.price);
		}
	}
	m_hb="";
	m_start="";
	m_end="";
	m_time="";
	UpdateData(FALSE);
	
}
////////////////////////////////////////////////////////////////////////
void CLookupDlg::BinSearch(CString key) //二分查找函数
{
	int low,high,mid;
	low=1,high=L.length;
	m_show.DeleteAllItems();
	while(low<=high)
	{
		mid=(low+high)/2;
		if(key==L.sl[mid].keys)
		{
			m_show.InsertItem(0,key);
			m_show.SetItemText(0,1,(CString)L.sl[mid].others.start);
			m_show.SetItemText(0,2,(CString)L.sl[mid].others.end);
			m_show.SetItemText(0,3,(CString)L.sl[mid].others.banji);
			m_show.SetItemText(0,4,(CString)L.sl[mid].others.stime);
			m_show.SetItemText(0,5,(CString)L.sl[mid].others.etime);
			m_show.SetItemText(0,6,(CString)L.sl[mid].others.jixing);
			m_show.SetItemText(0,7,(CString)L.sl[mid].others.price);			
			return;
		}
		else if(key<L.sl[mid].keys)
				high=mid-1;
		else 
			low=mid+1;
	}
	MessageBox("无此航班的信息\n请检查输入是否正确!");
}//BinSearch

void CLookupDlg::SeqSearch(CString key1,CString key2,CString key3)  //顺序查找函数
{
	int l=0,flag=0;
	m_show.DeleteAllItems();
	if(key3!="")
	{
		for(int i=1;i<=L.length;i++)
			if(key3==(CString)L.sl[i].others.stime)
			{
				flag=1;
				m_show.InsertItem(l,(CString)L.sl[i].keys);
				m_show.SetItemText(l,1,(CString)L.sl[i].others.start);
				m_show.SetItemText(l,2,(CString)L.sl[i].others.end);
				m_show.SetItemText(l,3,(CString)L.sl[i].others.banji);
				m_show.SetItemText(l,4,(CString)L.sl[i].others.stime);
				m_show.SetItemText(l,5,(CString)L.sl[i].others.etime);
				m_show.SetItemText(l,6,(CString)L.sl[i].others.jixing);
				m_show.SetItemText(l,7,(CString)L.sl[i].others.price);	
				l++;
			}
		if(flag==1) return;
	}
	else 
	{
		l=0;
		if(key1!=""&&key2!="")
		{
			
			for(int i=1;i<=L.length;i++)
			if(key1==(CString)L.sl[i].others.start&&key2==(CString)L.sl[i].others.end)
			{
				flag=1;
				m_show.InsertItem(l,(CString)L.sl[i].keys);
				m_show.SetItemText(l,1,(CString)L.sl[i].others.start);
				m_show.SetItemText(l,2,(CString)L.sl[i].others.end);
				m_show.SetItemText(l,3,(CString)L.sl[i].others.banji);
				m_show.SetItemText(l,4,(CString)L.sl[i].others.stime);
				m_show.SetItemText(l,5,(CString)L.sl[i].others.etime);
				m_show.SetItemText(l,6,(CString)L.sl[i].others.jixing);
				m_show.SetItemText(l,7,(CString)L.sl[i].others.price);	
				l++;
			}
		}
		if(key1!="")		
		{			
			for(int i=1;i<=L.length;i++)
			if(key1==(CString)L.sl[i].others.start&&key2!=(CString)L.sl[i].others.end)
			{
				flag=1;
				m_show.InsertItem(l,(CString)L.sl[i].keys);
				m_show.SetItemText(l,1,(CString)L.sl[i].others.start);
				m_show.SetItemText(l,2,(CString)L.sl[i].others.end);
				m_show.SetItemText(l,3,(CString)L.sl[i].others.banji);
				m_show.SetItemText(l,4,(CString)L.sl[i].others.stime);
				m_show.SetItemText(l,5,(CString)L.sl[i].others.etime);
				m_show.SetItemText(l,6,(CString)L.sl[i].others.jixing);
				m_show.SetItemText(l,7,(CString)L.sl[i].others.price);	
				l++;
			}
		}
		if(key2!="")		
		{
			for(int i=1;i<=L.length;i++)
			if(key1!=(CString)L.sl[i].others.start&&key2==(CString)L.sl[i].others.end)
			{
				flag=1;
				m_show.InsertItem(l,(CString)L.sl[i].keys);
				m_show.SetItemText(l,1,(CString)L.sl[i].others.start);
				m_show.SetItemText(l,2,(CString)L.sl[i].others.end);
				m_show.SetItemText(l,3,(CString)L.sl[i].others.banji);
				m_show.SetItemText(l,4,(CString)L.sl[i].others.stime);
				m_show.SetItemText(l,5,(CString)L.sl[i].others.etime);
				m_show.SetItemText(l,6,(CString)L.sl[i].others.jixing);
				m_show.SetItemText(l,7,(CString)L.sl[i].others.price);	
				l++;
			}
		}
		if(flag==1) return;
	}
	MessageBox("无此航班的信息\n请检查输入是否正确!");
}

BOOL CLookupDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDialog::PreTranslateMessage(pMsg);
}
   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -