公交查询程序
2008-10-25 22:31
148 查看
#include "writeoff_inc.h"
int Show()
{
int selection;
cout << "/n/n =========================" << endl;
cout << " 1. 线路查询" << endl;
cout << " 2. 站点查询" << endl;
cout << " 3. 站点---站点 查询" << endl;
cout << " 4. 管理员查询" << endl;
cout << " 0. 退出" << endl;
cout << " =========================" << endl;
cout << "/n 请选择查询编号:";
cin >> selection;
return selection;
}
int RoutQuery(char* code, char* sql, CRecords &record)
{
sprintf(sql, "select s.STOP_NAME, b.ORDER_NO "
" from stop_infor s, bus_infor b"
" where s.STOP_NO = b.STOP_NO and b.ROUT = '%s' order by b.ORDER_NO", code);
int j = record.Query(sql);
if (j <= 0)
{
cout << DefaultConnect.GetMsg() << endl;
return 0;
}
sprintf(sql, "线路%s经过的站点有: /n", code);
cout << sql << endl;
while (j > 0)
{
cout << record.Field(0).Char() << " " ;
// cout << record.Field(1).Float() << endl;
j = record.Next();
}
return 1;
}
int StopQuery(CRecords &record)
{
char sql[1024];
char stop[64];
char rout[32];
CRecords RoutRecord(DefaultConnect, 1);
cout << " 输入站点名称:";
cin >> stop;
sprintf(sql, "SELECT DISTINCT ROUT FROM BUS_INFOR WHERE STOP_NO = "
" (select STOP_NO from stop_infor WHERE STOP_NAME = '%s')", stop);
long j = record.Query(sql);
if (j <= 0)
{
cout << DefaultConnect.GetMsg() << endl;
return 0;
}
sprintf(sql, "/n 经过%s的线路有:", stop);
cout << sql << endl;
while (j > 0)
{
sprintf(rout, record.Field(0).Char());
cout << rout << endl;
RoutQuery(rout, sql, RoutRecord);
cout << endl;
j = record.Next();
}
return 1;
}
int OneRout(char* start, char* target, CRecords &stopRecord)
{
char sql[1024] = "";
sprintf(sql, "SELECT a.rout from BUS_INFOR a, BUS_INFOR b "
" where a.rout = b.rout and a.STOP_NO = '%s' "
" and b.STOP_NO = '%s'", start, target);
long j = stopRecord.Query(sql);
if (j <= 0)
{
return 0;
}
return 1;
}
int TwoRout(char* start, char* target, CRecords &rec)
{
char sql[1024] = "";
sprintf(sql, "select distinct a.stop_no v from bus_infor a where "
" (select rout from bus_infor where stop_no = '1') "
" in "
" (select rout from bus_infor where stop_no = a.stop_no ) "
" and (select rout from bus_infor where stop_no = '9') "
" in (select rout from bus_infor where stop_no = a.stop_no) ", start, target);
long j = rec.Query(sql);
if (j <= 0)
{
cout << DefaultConnect.GetMsg() << endl;
return 0;
}
return 1;
}
void GetStopName(char* stop_no, char* name)
{
CRecords record(DefaultConnect, 1);
char sql[256] = "";
sprintf(sql, "select STOP_NAME FROM STOP_INFOR WHERE STOP_NO = '%s'", stop_no);
long j = record.Query(sql);
sprintf(name, "%s", record.Field(0).Char());
}
int StopToStop()
{
CRecords stopRecord(DefaultConnect, 1);
CRecords record2(DefaultConnect, 1);
char start[32];
char target[32];
char sql[1024];
L1: cout << " 输入 起点站:";
cin >> start;
sprintf(sql, "select STOP_NO FROM STOP_INFOR WHERE STOP_NAME = '%s'", start);
long j = stopRecord.Query(sql);
if (j <= 0)
{
cout << "没有该站点" << endl;
goto L1;
}
sprintf(start, "%s", stopRecord.Field(0).Char());
L2: cout << " 输入 终点站:";
cin >> target;
sprintf(sql, "select STOP_NO FROM STOP_INFOR WHERE STOP_NAME = '%s'", target);
j = stopRecord.Query(sql);
if (j <= 0)
{
cout << "没有该站点" << endl;
goto L2;
}
sprintf(target, "%s", stopRecord.Field(0).Char());
// 直达查询
j = OneRout(start, target, stopRecord);
if (j > 0)
{
while (j > 0)
{
cout << stopRecord.Field(0).Char() << endl;
j = stopRecord.Next();
}
return 1;
}
j = TwoRout(start, target, record2);
// char rout[16];
char stop[16];
char stopName[32];
if (j <= 0)
{
cout << "转乘1次查询不出" << endl;
return 0;
}
long i = 0;
while (j > 0)
{
sprintf(stop, "%s", record2.Field(0).Char() );
GetStopName(stop, stopName);
cout << "/n可以在 "<<stopName << " 站转车 " << endl;
i = OneRout(start, stop, stopRecord);
cout << "先乘坐: "<< endl;
while (i > 0)
{
cout << stopRecord.Field(0).Char() << endl;
i = stopRecord.Next();
}
i = OneRout(stop, target, stopRecord);
cout << "再转乘: " << endl;
while (i > 0)
{
cout << stopRecord.Field(0).Char() << endl;
i = stopRecord.Next();
}
j = record2.Next();
}
return 1;
}
int Query(char* sql, CRecords &record)
{
cout << " 输入操作语句:;作为结束标识: " << endl;
char p;
int i = 0;
while ((p = getchar()) != ';')
{
sql[i++] = p;
}
sql[i] = '/0';
long j = record.Query(sql);
if (j <= 0)
{
cout << DefaultConnect.GetMsg() << endl;
return 0;
}
for (i = 0; i < record.CountCol(); i++)
{
cout << record.Field(i).Name() << " ";
}
cout << endl;
while (j > 0)
{
for (i = 0; i < record.CountCol(); i++)
{
switch(record.Field(i).Type()) {
case CharT:
cout << record.Field(i).Char() << " ";
break;
case FloatT:
cout << record.Field(i).Float() << " ";
break;
default:
break;
}
}
j = record.Next();
cout << endl;
}
return 1;
}
int main()
{
long j = Login("ORACLE");
if (j <= 0)
{
cout << "数据库连接失败" << endl;
return -1;
}
CRecords record(DefaultConnect, 1);
char code[32];
char sql[1024];
while (j != 0)
{
j = Show();
switch(j)
{
case 0:
Logout();
return 0;
break;
case 1:
cout << "输入线路编号:";
cin >> code ;
RoutQuery(code, sql, record);
break;
case 2:
StopQuery(record);
break;
case 3:
StopToStop();
break;
case 4:
Query(sql, record);
break;
default:
break;
}
}
Logout();
return 0;
}
int Show()
{
int selection;
cout << "/n/n =========================" << endl;
cout << " 1. 线路查询" << endl;
cout << " 2. 站点查询" << endl;
cout << " 3. 站点---站点 查询" << endl;
cout << " 4. 管理员查询" << endl;
cout << " 0. 退出" << endl;
cout << " =========================" << endl;
cout << "/n 请选择查询编号:";
cin >> selection;
return selection;
}
int RoutQuery(char* code, char* sql, CRecords &record)
{
sprintf(sql, "select s.STOP_NAME, b.ORDER_NO "
" from stop_infor s, bus_infor b"
" where s.STOP_NO = b.STOP_NO and b.ROUT = '%s' order by b.ORDER_NO", code);
int j = record.Query(sql);
if (j <= 0)
{
cout << DefaultConnect.GetMsg() << endl;
return 0;
}
sprintf(sql, "线路%s经过的站点有: /n", code);
cout << sql << endl;
while (j > 0)
{
cout << record.Field(0).Char() << " " ;
// cout << record.Field(1).Float() << endl;
j = record.Next();
}
return 1;
}
int StopQuery(CRecords &record)
{
char sql[1024];
char stop[64];
char rout[32];
CRecords RoutRecord(DefaultConnect, 1);
cout << " 输入站点名称:";
cin >> stop;
sprintf(sql, "SELECT DISTINCT ROUT FROM BUS_INFOR WHERE STOP_NO = "
" (select STOP_NO from stop_infor WHERE STOP_NAME = '%s')", stop);
long j = record.Query(sql);
if (j <= 0)
{
cout << DefaultConnect.GetMsg() << endl;
return 0;
}
sprintf(sql, "/n 经过%s的线路有:", stop);
cout << sql << endl;
while (j > 0)
{
sprintf(rout, record.Field(0).Char());
cout << rout << endl;
RoutQuery(rout, sql, RoutRecord);
cout << endl;
j = record.Next();
}
return 1;
}
int OneRout(char* start, char* target, CRecords &stopRecord)
{
char sql[1024] = "";
sprintf(sql, "SELECT a.rout from BUS_INFOR a, BUS_INFOR b "
" where a.rout = b.rout and a.STOP_NO = '%s' "
" and b.STOP_NO = '%s'", start, target);
long j = stopRecord.Query(sql);
if (j <= 0)
{
return 0;
}
return 1;
}
int TwoRout(char* start, char* target, CRecords &rec)
{
char sql[1024] = "";
sprintf(sql, "select distinct a.stop_no v from bus_infor a where "
" (select rout from bus_infor where stop_no = '1') "
" in "
" (select rout from bus_infor where stop_no = a.stop_no ) "
" and (select rout from bus_infor where stop_no = '9') "
" in (select rout from bus_infor where stop_no = a.stop_no) ", start, target);
long j = rec.Query(sql);
if (j <= 0)
{
cout << DefaultConnect.GetMsg() << endl;
return 0;
}
return 1;
}
void GetStopName(char* stop_no, char* name)
{
CRecords record(DefaultConnect, 1);
char sql[256] = "";
sprintf(sql, "select STOP_NAME FROM STOP_INFOR WHERE STOP_NO = '%s'", stop_no);
long j = record.Query(sql);
sprintf(name, "%s", record.Field(0).Char());
}
int StopToStop()
{
CRecords stopRecord(DefaultConnect, 1);
CRecords record2(DefaultConnect, 1);
char start[32];
char target[32];
char sql[1024];
L1: cout << " 输入 起点站:";
cin >> start;
sprintf(sql, "select STOP_NO FROM STOP_INFOR WHERE STOP_NAME = '%s'", start);
long j = stopRecord.Query(sql);
if (j <= 0)
{
cout << "没有该站点" << endl;
goto L1;
}
sprintf(start, "%s", stopRecord.Field(0).Char());
L2: cout << " 输入 终点站:";
cin >> target;
sprintf(sql, "select STOP_NO FROM STOP_INFOR WHERE STOP_NAME = '%s'", target);
j = stopRecord.Query(sql);
if (j <= 0)
{
cout << "没有该站点" << endl;
goto L2;
}
sprintf(target, "%s", stopRecord.Field(0).Char());
// 直达查询
j = OneRout(start, target, stopRecord);
if (j > 0)
{
while (j > 0)
{
cout << stopRecord.Field(0).Char() << endl;
j = stopRecord.Next();
}
return 1;
}
j = TwoRout(start, target, record2);
// char rout[16];
char stop[16];
char stopName[32];
if (j <= 0)
{
cout << "转乘1次查询不出" << endl;
return 0;
}
long i = 0;
while (j > 0)
{
sprintf(stop, "%s", record2.Field(0).Char() );
GetStopName(stop, stopName);
cout << "/n可以在 "<<stopName << " 站转车 " << endl;
i = OneRout(start, stop, stopRecord);
cout << "先乘坐: "<< endl;
while (i > 0)
{
cout << stopRecord.Field(0).Char() << endl;
i = stopRecord.Next();
}
i = OneRout(stop, target, stopRecord);
cout << "再转乘: " << endl;
while (i > 0)
{
cout << stopRecord.Field(0).Char() << endl;
i = stopRecord.Next();
}
j = record2.Next();
}
return 1;
}
int Query(char* sql, CRecords &record)
{
cout << " 输入操作语句:;作为结束标识: " << endl;
char p;
int i = 0;
while ((p = getchar()) != ';')
{
sql[i++] = p;
}
sql[i] = '/0';
long j = record.Query(sql);
if (j <= 0)
{
cout << DefaultConnect.GetMsg() << endl;
return 0;
}
for (i = 0; i < record.CountCol(); i++)
{
cout << record.Field(i).Name() << " ";
}
cout << endl;
while (j > 0)
{
for (i = 0; i < record.CountCol(); i++)
{
switch(record.Field(i).Type()) {
case CharT:
cout << record.Field(i).Char() << " ";
break;
case FloatT:
cout << record.Field(i).Float() << " ";
break;
default:
break;
}
}
j = record.Next();
cout << endl;
}
return 1;
}
int main()
{
long j = Login("ORACLE");
if (j <= 0)
{
cout << "数据库连接失败" << endl;
return -1;
}
CRecords record(DefaultConnect, 1);
char code[32];
char sql[1024];
while (j != 0)
{
j = Show();
switch(j)
{
case 0:
Logout();
return 0;
break;
case 1:
cout << "输入线路编号:";
cin >> code ;
RoutQuery(code, sql, record);
break;
case 2:
StopQuery(record);
break;
case 3:
StopToStop();
break;
case 4:
Query(sql, record);
break;
default:
break;
}
}
Logout();
return 0;
}
相关文章推荐
- 微信小程序--后端管理平台业务4--Mybatis模糊查询相关
- 微信小程序 火车票查询实例讲解
- 写一段Jdbc连Oracle的程序,并实现数据查询.
- 【C++ Primer】文本查询程序再探
- 使用百度API实现实时公交站点查询及地图显示
- android开发实战之做手机号和邮编查询小程序
- 图-公交查询问题乱弹(三)
- 公交路线查询
- 在windows操作系统中,查询端口占用和清除端口占用的程序
- 基于swing的手机号运营商查询程序
- JAVA一个查询sqlserver网页程序,大家或许会体会一些东西
- c++ primer 5ed 15.9文本查询程序再探
- 利用struts2,mybatis,spring,三大框架写的一个简单的查询程序
- 编写高质量代码改善C#程序的157个建议——建议26:使用匿名类型存储LINQ查询结果
- 微信公众平台开发(47)公交查询
- 我的第五个程序 java的JDBC连接mysql数据库 实现输入查询
- GIS公交查询-flex/java
- 结合EF5.0讲MVC4(二)--为先前程序添加查询及主外键关系
- 自己写的oci查询程序
- 烟台大学体测成绩查询程序