📄 page3.cpp
字号:
// page3.cpp : implementation file
//
#include "stdafx.h"
#include "BusStop.h"
#include "page3.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CBusStopApp theApp;
/////////////////////////////////////////////////////////////////////////////
// page3 dialog
page3::page3(CWnd* pParent /*=NULL*/)
: CDialog(page3::IDD, pParent)
{ado=0;
//{{AFX_DATA_INIT(page3)
m_from = _T("");
m_to = _T("");
//}}AFX_DATA_INIT
}
void page3::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(page3)
DDX_Control(pDX, IDC_EDIT3, m_solutions);
DDX_Text(pDX, IDC_EDIT1, m_from);
DDX_Text(pDX, IDC_EDIT2, m_to);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(page3, CDialog)
//{{AFX_MSG_MAP(page3)
ON_BN_CLICKED(IDC_Search, OnSearch)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void page3::OnSearch()
{
m_solutions.SetWindowText("");
UpdateData(TRUE);
from.stop_name=m_from;
to.stop_name=m_to;
node busline;
if(ado==0)
{
try
{m_pRecordset->Open("SELECT * FROM bus",
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
try
{m_pRecordset1->Open("SELECT * FROM bus",
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
try
{m_pRecordset2->Open("SELECT * FROM bus",
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
ado=1;
}
int k1=set(&from);
int k2=set(&to);
if(k1&&k2)
{
int distance=0;
CString distanc;
m_solution1="";
if(find(from, to, busline))
{for(int i=0;i<busline.length;i++)
{ distance=Distance(from.stop_name,to.stop_name,busline.bus_line[i]);
distanc.Format("%d",distance);
m_solution1=m_solution1+"一个方案:\r\n\t直接乘"+busline.bus_line[i]+"\t-------经过"+distanc+"站\r\n\r\n";
}
m_solutions.SetWindowText(m_solution1);
}
else if(find1(from,to))
m_solutions.SetWindowText(m_solution1);
else
m_solutions.SetWindowText("转车1次以上的功能未实现!");
}
else
m_solutions.SetWindowText("");
}
int page3::set(node *t)
{
t->length=0;
int flag=0;
CString strSql;
strSql.Format("SELECT * FROM bus WHERE stop_name='%s' AND flag=0",
t->stop_name);
try
{
m_pRecordset1->Close();
m_pRecordset1->Open(strSql.AllocSysString(),
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
int i=0;
_variant_t var;
CString busline;
try
{
if(!m_pRecordset1->BOF)
{m_pRecordset1->MoveFirst();flag=1;}
else
{
AfxMessageBox("没有"+t->stop_name+"这个站点!");
return 0;
}
while(!m_pRecordset1->adoEOF)
{
var = m_pRecordset1->GetCollect("bus_number_text");
if(var.vt != VT_NULL)
busline=(LPCSTR)_bstr_t(var);
t->bus_line[i++]=busline;
m_pRecordset1->MoveNext();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
t->length=i;
return flag;
}
BOOL page3::OnInitDialog()
{
CDialog::OnInitDialog();
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset1.CreateInstance(__uuidof(Recordset));
m_pRecordset2.CreateInstance(__uuidof(Recordset));
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
int page3::find(node &from, node &to, node &busline)
{int flag=0;busline.length=0;int k=0;
for(int i=0;i<from.length;i++)
for(int j=0;j<to.length;j++)
if(!from.bus_line[i].Compare(to.bus_line[j]))
{busline.bus_line[k++]=to.bus_line[j];
flag=1;
}
busline.length=k;
return flag;
}
int page3::find1(node &from, node &to)
{int flag=0;
int distance_1;
int distance_2;
int distance_all;
CString distance_All;
CString distance_11;
CString distance_22;
node temp,busline;
for(int i=0;i<from.length;i++)
{CString strSql;
strSql.Format("SELECT * FROM bus WHERE bus_number_text = '%s' AND flag=1",
from.bus_line[i]);
try
{
m_pRecordset->Close();
m_pRecordset->Open(strSql.AllocSysString(),
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
_variant_t var;
try
{
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return 0;
}
while(!m_pRecordset->adoEOF)
{var = m_pRecordset->GetCollect("stop_name");
if(var.vt != VT_NULL)
temp.stop_name= (LPCSTR)_bstr_t(var);
set(&temp);
if(find(temp,to,busline))
{ flag=1;
CString tempp="";
tempp="一个方案:\r\n\t乘"+from.bus_line[i]+"到"+temp.stop_name+"下车;再乘";
for(int j=0;j<busline.length;j++)
{ m_solution1=m_solution1+tempp+busline.bus_line[j]+"到"+to.stop_name+"\r\n";
distance_1=Distance(from.stop_name, temp.stop_name, from.bus_line[i]);
distance_2=Distance(temp.stop_name, to.stop_name, busline.bus_line[j]);
distance_all= distance_1+ distance_2;
distance_11.Format("%d",distance_1);
distance_22.Format("%d",distance_2);
distance_All.Format("%d",distance_all) ;
m_solution1=m_solution1+"\t-------------先经过"+distance_11+"站;"+"再经过"+distance_22+"站;一共经过"+distance_All+"站\r\n\r\n";
}
}
m_pRecordset->MoveNext();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
return flag;
}
int page3::Distance(CString From, CString To, CString BusLine)
{CString strSql;
CString stop_number1;
CString stop_number2;
strSql.Format("SELECT * FROM bus WHERE bus_number_text = '%s' AND flag=1 AND stop_name='%s'",
BusLine,From);
try
{
m_pRecordset2->Close();
m_pRecordset2->Open(strSql.AllocSysString(),
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
_variant_t var;
try
{
if(!m_pRecordset2->BOF)
m_pRecordset2->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return 0;
}
// 读入库中各字段并加入列表框中
while(!m_pRecordset2->adoEOF)
{var = m_pRecordset2->GetCollect("stop_number");
if(var.vt != VT_NULL)
stop_number1=(LPCSTR)_bstr_t(var);
//得到From在BusLine中的站号
m_pRecordset2->MoveNext();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
//***********************************************************
//***********************************************************
//得到To在BusLine中的站号
strSql.Format("SELECT * FROM bus WHERE bus_number_text = '%s' AND flag=1 AND stop_name='%s'",
BusLine,To);
try
{
//先关闭已经打开的记录集对象
m_pRecordset2->Close();
//根据新的Sql查询语句,重新打开记录集对象
m_pRecordset2->Open(strSql.AllocSysString(),
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
//在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
//因为它有时会经常出现一些想不到的错误。
try
{
if(!m_pRecordset2->BOF)
m_pRecordset2->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return 0;
}
// 读入库中各字段并加入列表框中
while(!m_pRecordset2->adoEOF)
{var = m_pRecordset2->GetCollect("stop_number");
if(var.vt != VT_NULL)
stop_number2=(LPCSTR)_bstr_t(var);
//得到To在BusLine中的站号
m_pRecordset2->MoveNext();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
//*******************************************
//*******************************************
//得到2站的距离
int k1=atoi(stop_number1);
int k2=atoi(stop_number2);
int distance=k1-k2;
if(distance>=0)
return distance;
else
return -distance;
//*******************************************
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -