溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++怎么實(shí)現(xiàn)車票管理系統(tǒng)

發(fā)布時(shí)間:2022-03-14 15:49:06 來源:億速云 閱讀:190 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“C++怎么實(shí)現(xiàn)車票管理系統(tǒng)”,在日常操作中,相信很多人在C++怎么實(shí)現(xiàn)車票管理系統(tǒng)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++怎么實(shí)現(xiàn)車票管理系統(tǒng)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

一車站每天有n個(gè)發(fā)車班次,每個(gè)班次都有一班次號(1、2、3…n),固定的發(fā)車時(shí)間,
固定的路線(起始站、終點(diǎn)站),大致的行車時(shí)間,固定的額定載客量。如
班次 發(fā)車時(shí)間 起點(diǎn)站 終點(diǎn)站 行車時(shí)間 額定載量 已定票人數(shù)
1 8:00 郫縣 廣漢 2 45 30
2 6:30 郫縣 成都 0.5 40 40
3 7:00 郫縣 成都 0.5 40 20
4 10:00 郫縣 成都 0.5 40 2
…

功能要求:

(1)錄入班次信息(信息用文件保存),可不定時(shí)地增加班次數(shù)據(jù)
(2)瀏覽班次信息,可顯示出所有班次當(dāng)前狀總(如果當(dāng)前系統(tǒng)時(shí)間超過了某班次的發(fā)車時(shí)間,則顯示“此班已發(fā)出”的提示信息)。
(3)查詢路線:可按班次號查詢 ,可按終點(diǎn)站查詢
(4)售票和退票功能
A:當(dāng)查詢出已定票人數(shù)小于額定載量且當(dāng)前系統(tǒng)時(shí)間小于發(fā)車時(shí)間時(shí)才能售票,自動更新已售票人數(shù)
B:退票時(shí),輸入退票的班次,當(dāng)本班車未發(fā)出時(shí)才能退票,自動更新已售票人數(shù)

下面是 代碼。

工具是VS2019、MySQL8.0
注意:請?zhí)崆霸O(shè)置好VS 的環(huán)境,否則運(yùn)行不出來。本人是個(gè)菜鳥,代碼可能比較復(fù)雜比較low,輕噴。

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#include <mysql.h>
#include <Windows.h>
#include <time.h>
#include <iostream>
using namespace std;

//包含附加依賴項(xiàng),也可以在工程--屬性里面設(shè)置
//#pragma comment(lib,"wsock32.lib")
//#pragma comment(lib,"libmysql.lib")
MYSQL mysql; //mysql連接
MYSQL_FIELD* fd;  //字段列數(shù)組
char field[32][32];  //存字段名二維數(shù)組
MYSQL_RES* res; //這個(gè)結(jié)構(gòu)代表返回行的一個(gè)查詢結(jié)果集
MYSQL_ROW column,row; //一個(gè)行數(shù)據(jù)的類型安全(type-safe)的表示,表示數(shù)據(jù)行的列
char query[150]; //查詢語句

bool ConnectDatabase();     //函數(shù)聲明
void FreeConnect();
bool read();  //查詢1
bool search();  //查詢2
bool input();
bool piao();
void mainmenu();
bool readtest();
int main(int argc, char** argv)
{
    int m;
    int k = 1;
    ConnectDatabase();
    mainmenu();
    while(k==1)
    {
        printf("\n");
        printf(" 請選擇功能:");
        cin >> m;
        switch (m)
        {
        case  1 :input(); break;//錄入車票信息
        case  2 :read(); break;//瀏覽車票信息
        case  3 :search(); break;// 查詢車票信息
        case  4 :piao(); break;//售票和退票
        case  5 : k = 0; break;//退出系統(tǒng)
        }
    }
    FreeConnect();
    system("pause");
    return 0;
}
//連接數(shù)據(jù)庫
bool ConnectDatabase()
{
    //初始化mysql
    mysql_init(&mysql);  //連接mysql,數(shù)據(jù)庫

    //返回false則連接失敗,返回true則連接成功
    if (!(mysql_real_connect(&mysql, "localhost", "root", "123456", "chepiao", 3306, NULL, 0))) //中間分別是主機(jī),用戶名,密碼,數(shù)據(jù)庫名,端口號,可以先寫成參數(shù)再傳進(jìn)去
    {
        printf("Error connecting to database:%s\n", mysql_error(&mysql));
        return false;
    }
    else
    {
        printf("Connected  success\n");
        return true;
    }
}
void mainmenu()
{
    cout << "\n\n--------歡迎使用車票管理系統(tǒng)----------" << endl << endl;
    cout << "===========================================" << endl;
    cout << "||========================================||" << endl;
    cout << "||            1.錄入車票信息              ||" << endl;
    cout << "||            2.瀏覽車票信息              ||" << endl;
    cout << "||            3.查詢車票信息              ||" << endl;
    cout << "||            4.售票和退票                ||" << endl;
    cout << "||            5.退出系統(tǒng)                  ||" << endl;
    cout << "||========================================||" << endl;
    cout << "============================================" << endl;

}
//釋放資源
void FreeConnect()
{
    //釋放資源
    mysql_free_result(res);
    mysql_close(&mysql);
}
//數(shù)據(jù)庫操作
//其實(shí)所有的數(shù)據(jù)庫操作都是先寫個(gè)sql語句,然后用mysql_query(&mysql,query)來完成,包括創(chuàng)建數(shù)據(jù)庫或表,增刪改查
bool readtest()
{
    struct tm* local;
    time_t t;
    t = time(NULL);
    sprintf(query, "select * from testTable"); //執(zhí)行查詢語句,這里是查詢所有,user是表名,不用加引號,用strcpy也可以
    mysql_query(&mysql, "set names gbk"); //設(shè)置編碼格式(SET NAMES GBK也行),否則cmd下中文亂碼
                                          //返回0 查詢成功,返回1查詢失敗
    mysql_query(&mysql, query);     //執(zhí)行SQL語句

    //獲取結(jié)果集
    if (!(res = mysql_store_result(&mysql)))    //獲得sql語句結(jié)束后返回的結(jié)果集
    {
        printf("Couldn't get result from %s\n", mysql_error(&mysql));
        return false;
    }

    local = localtime(&t);//獲取當(dāng)前系統(tǒng)時(shí)間
    int num = local->tm_hour;
    int num1 = local->tm_min;
    char str1[25], str2[25];
    sprintf(str1, "%d", num);
    sprintf(str2, "%d", num1);
    char a[5] = ":";
    char s[1000];
    int n;

    strcpy(s, str1);
    strcat(s, a);
    strcat(s, str2);
    cout << endl;
    //printf(" 當(dāng)前系統(tǒng)時(shí)間為:%10s\t\n", s);
    //cout << endl;
    char* str_field[32];  //定義一個(gè)字符串?dāng)?shù)組存儲字段信息
    for (int i = 0; i < 8; i++)   //在已知字段數(shù)量的情況下獲取字段名
    {
        str_field[i] = mysql_fetch_field(res)->name;
    }
    /*for (int i = 0; i < 8; i++)   //打印字段
        printf("%10s\t", str_field[i]);
    printf("\n");*/

    while (column = mysql_fetch_row(res))   //在已知字段數(shù)量情況下,獲取并打印下一行
    {
        char test[1000];
        char co[1000];
        char abc[5] = "'";

        int j = 0;
        strcpy(co, column[1]);
        while (co[j] != ':')
        {
            if ((co[j] > 47) && (co[j] < 58))
            {
                test[j] = co[j];
                j++;
            }
        }
        if (j == 2)
            n = (test[0] - '0') * 10 + (test[1] - '0');
        else if (j == 1)
            n = (test[0] - '0');
        if ((local->tm_hour) < n)
        {

            sprintf(query, "update testtable set 當(dāng)前狀況='此車未發(fā)出' where 發(fā)車時(shí)間='");
            strcat(query, column[1]);
            strcat(query, abc);
            mysql_query(&mysql, query);
        }
        else
        {
            sprintf(query, "update testtable set 當(dāng)前狀況='此車已發(fā)出'where 發(fā)車時(shí)間='");
            strcat(query, column[1]);
            strcat(query, abc);
            mysql_query(&mysql, query);
        }

        //打印獲取的數(shù)據(jù)
        printf("%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n", column[0], column[1], column[2], column[3], column[4], column[5], column[6], column[7]);  //column是列數(shù)組

    }

    return true;
}
//瀏覽數(shù)據(jù)
bool read()
{
    struct tm* local;
    time_t t;
    t = time(NULL);
    sprintf(query, "select * from testTable"); //執(zhí)行查詢語句,這里是查詢所有,user是表名,不用加引號,用strcpy也可以
    mysql_query(&mysql, "set names gbk"); //設(shè)置編碼格式(SET NAMES GBK也行),否則cmd下中文亂碼
                                          //返回0 查詢成功,返回1查詢失敗
    mysql_query(&mysql, query);     //執(zhí)行SQL語句
     
    //獲取結(jié)果集
    if (!(res = mysql_store_result(&mysql)))    //獲得sql語句結(jié)束后返回的結(jié)果集
    {
        printf("Couldn't get result from %s\n", mysql_error(&mysql));
        return false;
    }

    local = localtime(&t);//獲取當(dāng)前系統(tǒng)時(shí)間
    int num = local->tm_hour;
    int num1 = local->tm_min;
    char str1[25], str2[25];
    sprintf(str1, "%d", num);
    sprintf(str2, "%d", num1);
    char a[5] = ":";
    char s[1000];
    int n;
    
    strcpy(s, str1);
    strcat(s, a);
    strcat(s, str2);
    cout << endl;
    printf(" 當(dāng)前系統(tǒng)時(shí)間為:%10s\t\n", s);
    cout << endl;
    char* str_field[32];  //定義一個(gè)字符串?dāng)?shù)組存儲字段信息
    for (int i = 0; i < 8; i++)   //在已知字段數(shù)量的情況下獲取字段名
    {
        str_field[i] = mysql_fetch_field(res)->name;
    }
    for (int i = 0; i < 8; i++)   //打印字段
        printf("%10s\t", str_field[i]);
    printf("\n");
   
    while (column = mysql_fetch_row(res))   //在已知字段數(shù)量情況下,獲取并打印下一行
    {   char test[1000];
        char co[1000];
        char abc[5]="'";

        int j = 0;
        strcpy(co, column[1]);
        while (co[j] != ':')
        {
            if ((co[j] > 47) && (co[j] < 58))
            {
                test[j] = co[j];
                j++;
            }
        }
        if (j == 2)
            n = (test[0] - '0') * 10 + (test[1] - '0');
        else if (j == 1)
            n = (test[0] - '0');
        if ((local->tm_hour )<n)
        {
           
            sprintf(query, "update testtable set 當(dāng)前狀況='此車未發(fā)出' where 發(fā)車時(shí)間='");
            strcat(query,column[1]);
            strcat(query,abc);
            mysql_query(&mysql, query);
        }
        else 
        {
            sprintf(query, "update testtable set 當(dāng)前狀況='此車已發(fā)出'where 發(fā)車時(shí)間='");
            strcat(query, column[1]);
            strcat(query, abc);
            mysql_query(&mysql, query);
        }
         
        
    }
    readtest();
    return true;
}
//查詢數(shù)據(jù)
bool search()
{
    int y;
    int m;
    char n[5];
    char s[100];
    char abc[5] = "'";
    printf(" 請輸入查詢方式:1 按班次號查詢 ;2 按終點(diǎn)站查詢\n");
    printf(" 請輸入您的操作: ");
    cin >> m;
    if (m == 1)
    {
        printf(" 請輸入您要查詢的班次號:  ");
        cin >> n;
        sprintf(query, "select * from testtable where 班次='"); //執(zhí)行查詢語句,這里是查詢所有,user是表名,不用加引號,用strcpy也可以
        strcat(query, n);
        strcat(query, abc);
        mysql_query(&mysql, "set names gbk"); //設(shè)置編碼格式(SET NAMES GBK也行),否則cmd下中文亂碼

        mysql_query(&mysql, query);    //執(zhí)行SQL語句

        //獲取結(jié)果集
        if (!(res = mysql_store_result(&mysql)))    //獲得sql語句結(jié)束后返回的結(jié)果集
        {
            printf("Couldn't get result from %s\n", mysql_error(&mysql));
            return false;
        }

        for (int i = 0; fd = mysql_fetch_field(res); i++)  //獲取字段名
            strcpy(field[i], fd->name);
        int j = mysql_num_fields(res);  // 獲取列數(shù)

        printf("\n");
        for (int i = 0; i < j; i++)  //打印字段
            printf("%10s\t", field[i]);
        printf("\n");
        while (column = mysql_fetch_row(res))
        {
            printf("%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n", column[0], column[1], column[2], column[3], column[4], column[5], column[6], column[7]);  //column是列數(shù)組

        }
    }
    else if (m == 2)
    {
        printf(" 請輸入您要查詢的終點(diǎn)站:  ");
        cin >> s;
        sprintf(query, "select * from testTable where 終點(diǎn)站='"); //執(zhí)行查詢語句,這里是查詢所有,user是表名,不用加引號,用strcpy也可以
        strcat(query, s);
        strcat(query, abc);
        mysql_query(&mysql, "set names gbk"); //設(shè)置編碼格式(SET NAMES GBK也行),否則cmd下中文亂碼

        mysql_query(&mysql, query);    //執(zhí)行SQL語句

        //獲取結(jié)果集
        if (!(res = mysql_store_result(&mysql)))    //獲得sql語句結(jié)束后返回的結(jié)果集
        {
            printf("Couldn't get result from %s\n", mysql_error(&mysql));
            return false;
        }

        for (int i = 0; fd = mysql_fetch_field(res); i++)  //獲取字段名
            strcpy(field[i], fd->name);
        int j = mysql_num_fields(res);  // 獲取列數(shù)

        printf("\n");
        for (int i = 0; i < j; i++)  //打印字段
            printf("%10s\t", field[i]);
        printf("\n");
        while (column = mysql_fetch_row(res))
        {
            printf("%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n", column[0], column[1], column[2], column[3], column[4], column[5], column[6], column[7]);  //column是列數(shù)組

        }
    }
    printf("\n");
    printf("  1 繼續(xù)查詢;2 返回主界面\n");
    printf(" 請輸入您的操作: ");
    cin >> y;
    if (y == 1)
    {
        search();
    }
    else if (y == 2)
    {
        mainmenu();
    }
    return true;

}
//錄入數(shù)據(jù)
bool input()
{
    sprintf(query, "insert into testtable values (6, '18:00', '青島','濟(jì)南',3,20,10,'NULL')");  //可以想辦法實(shí)現(xiàn)手動在控制臺手動輸入指令
    mysql_query(&mysql, query);      //執(zhí)行SQL語句
        printf(" Insert success\n");
        read();
        return true;
    
}
//售票和退票
bool piao()
{   int y;
    int m,a,b;
    char n[5];
    char k[5];
    char abc[5] = "'";
    char cc[100] = " where 班次='";
    printf(" 請選擇操作:1 售票;2 退票");
    cout << endl;
    printf(" 請輸入: ");
    cin >> m;
    if (m == 1)//售票
    {
        printf(" 請輸入您要購買的車票班次:  ");
        cin >> k;
        sprintf(query, "select * from testtable where 班次='"); //執(zhí)行查詢語句,這里是查詢所有,user是表名,不用加引號,用strcpy也可以
        strcat(query, k);
        strcat(query, abc);
        mysql_query(&mysql, "set names gbk"); //設(shè)置編碼格式(SET NAMES GBK也行),否則cmd下中文亂碼

        mysql_query(&mysql, query);    //執(zhí)行SQL語句

        //獲取結(jié)果集
        if (!(res = mysql_store_result(&mysql)))    //獲得sql語句結(jié)束后返回的結(jié)果集
        {
            printf("Couldn't get result from %s\n", mysql_error(&mysql));
            return false;
        }

        for (int i = 0; fd = mysql_fetch_field(res); i++)  //獲取字段名
            strcpy(field[i], fd->name);
        int j = mysql_num_fields(res);  // 獲取列數(shù)

        
        column = mysql_fetch_row(res);
        a = atoi(column[6]);
        b = atoi(column[5]);
        if ((a < b) && (!strcmp(column[7],"此車未發(fā)出")))
        {
            printf("\n");
            printf("售票成功\n");
            printf("\n");
        for (int i = 0; i < j; i++)  //打印字段
            printf("%10s\t", field[i]);
        printf("\n");
            a = a + 1;
            itoa(a,column[6],10);
            sprintf(query, "update testtable set 已訂票人數(shù)='"); //執(zhí)行查詢語句,這里是查詢所有,user是表名,不用加引號,用strcpy也可以
            strcat(query, column[6]);
           strcat(query, abc);
           strcat(query, cc);
             strcat(query, k);
             strcat(query, abc);
            mysql_query(&mysql, "set names gbk"); //設(shè)置編碼格式(SET NAMES GBK也行),否則cmd下中文亂碼
            mysql_query(&mysql, query);    //執(zhí)行SQL語句
      
                printf("%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n", column[0], column[1], column[2], column[3], column[4], column[5], column[6], column[7]);  //column是列數(shù)組

        }
    }
    else if (m == 2)//退票
    {
        
        printf(" 請輸入您要退訂的車票班次:  ");
        cin >> n;
        sprintf(query, "select * from testtable where 班次='"); //執(zhí)行查詢語句,這里是查詢所有,user是表名,不用加引號,用strcpy也可以
        strcat(query, n);
        strcat(query, abc);
        mysql_query(&mysql, "set names gbk"); //設(shè)置編碼格式(SET NAMES GBK也行),否則cmd下中文亂碼

        mysql_query(&mysql, query);    //執(zhí)行SQL語句

        //獲取結(jié)果集
        if (!(res = mysql_store_result(&mysql)))    //獲得sql語句結(jié)束后返回的結(jié)果集
        {
            printf("Couldn't get result from %s\n", mysql_error(&mysql));
            return false;
        }
       
        for (int i = 0; fd = mysql_fetch_field(res); i++)  //獲取字段名
            strcpy(field[i], fd->name);
        int j = mysql_num_fields(res);  // 獲取列數(shù)

        
        column = mysql_fetch_row(res);
        a = atoi(column[6]);
        if (!strcmp(column[7], "此車未發(fā)出"))
        {
            printf("\n");
            printf("退票成功\n");
            printf("\n");
        for (int i = 0; i < j; i++)  //打印字段
            printf("%10s\t", field[i]);
        printf("\n");
            a = a - 1;
            itoa(a, column[6], 10);
            sprintf(query, "update testtable set 已訂票人數(shù)='"); //執(zhí)行查詢語句,這里是查詢所有,user是表名,不用加引號,用strcpy也可以
            strcat(query, column[6]);
            strcat(query, abc);
            strcat(query, cc);
            strcat(query, n);
            strcat(query, abc);
            mysql_query(&mysql, "set names gbk"); //設(shè)置編碼格式(SET NAMES GBK也行),否則cmd下中文亂碼
            mysql_query(&mysql, query);    //執(zhí)行SQL語句

            printf("%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n", column[0], column[1], column[2], column[3], column[4], column[5], column[6], column[7]);  //column是列數(shù)組

        }
            
        
    }
    printf("\n");
    printf("  1 繼續(xù)售票/退票;2 返回主界面\n");
    printf(" 請輸入您的操作: ");
        cin >> y;
        if (y == 1)
        {
            piao();
        }
        else if (y == 2)
        {
            mainmenu();
        }
}

到此,關(guān)于“C++怎么實(shí)現(xiàn)車票管理系統(tǒng)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI