您的位置:首页 > 数据库 > Oracle

公交查询程序

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息