服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现车票管理系统

C++实现车票管理系统

2022-10-19 13:45程序小菜鸟111 C/C++

这篇文章主要为大家详细介绍了C++实现车票管理系统,连接数据库MySQL,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现车票管理系统的具体代码,供大家参考,具体内容如下

一车站每天有n个发车班次,每个班次都有一班次号(1、2、3…n),固定的发车时间,
固定的路线(起始站、终点站),大致的行车时间,固定的额定载客量。如
班次 发车时间 起点站 终点站 行车时间 额定载量 已定票人数
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)录入班次信息(信息用文件保存),可不定时地增加班次数据
(2)浏览班次信息,可显示出所有班次当前状总(如果当前系统时间超过了某班次的发车时间,则显示“此班已发出”的提示信息)。
(3)查询路线:可按班次号查询 ,可按终点站查询
(4)售票和退票功能
A:当查询出已定票人数小于额定载量且当前系统时间小于发车时间时才能售票,自动更新已售票人数
B:退票时,输入退票的班次,当本班车未发出时才能退票,自动更新已售票人数

下面是 代码。

工具是VS2019、MySQL8.0
注意:请提前设置好VS 的环境,否则运行不出来。本人是个菜鸟,代码可能比较复杂比较low,轻喷。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#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;
 
//包含附加依赖项,也可以在工程--属性里面设置
//#pragma comment(lib,"wsock32.lib")
//#pragma comment(lib,"libmysql.lib")
MYSQL mysql; //mysql连接
MYSQL_FIELD* fd;  //字段列数组
char field[32][32];  //存字段名二维数组
MYSQL_RES* res; //这个结构代表返回行的一个查询结果集
MYSQL_ROW column,row; //一个行数据的类型安全(type-safe)的表示,表示数据行的列
char query[150]; //查询语句
 
bool ConnectDatabase();     //函数声明
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;//退出系统
        }
    }
    FreeConnect();
    system("pause");
    return 0;
}
//连接数据库
bool ConnectDatabase()
{
    //初始化mysql
    mysql_init(&mysql);  //连接mysql,数据库
 
    //返回false则连接失败,返回true则连接成功
    if (!(mysql_real_connect(&mysql, "localhost", "root", "123456", "chepiao", 3306, NULL, 0))) //中间分别是主机,用户名,密码,数据库名,端口号,可以先写成参数再传进去
    {
        printf("Error connecting to database:%s\n", mysql_error(&mysql));
        return false;
    }
    else
    {
        printf("Connected  success\n");
        return true;
    }
}
void mainmenu()
{
    cout << "\n\n--------欢迎使用车票管理系统----------" << endl << endl;
    cout << "===========================================" << endl;
    cout << "||========================================||" << endl;
    cout << "||            1.录入车票信息              ||" << endl;
    cout << "||            2.浏览车票信息              ||" << endl;
    cout << "||            3.查询车票信息              ||" << endl;
    cout << "||            4.售票和退票                ||" << endl;
    cout << "||            5.退出系统                  ||" << endl;
    cout << "||========================================||" << endl;
    cout << "============================================" << endl;
 
}
//释放资源
void FreeConnect()
{
    //释放资源
    mysql_free_result(res);
    mysql_close(&mysql);
}
//数据库操作
//其实所有的数据库操作都是先写个sql语句,然后用mysql_query(&mysql,query)来完成,包括创建数据库或表,增删改查
bool readtest()
{
    struct tm* local;
    time_t t;
    t = time(NULL);
    sprintf(query, "select * from testTable"); //执行查询语句,这里是查询所有,user是表名,不用加引号,用strcpy也可以
    mysql_query(&mysql, "set names gbk"); //设置编码格式(SET NAMES GBK也行),否则cmd下中文乱码
                                          //返回0 查询成功,返回1查询失败
    mysql_query(&mysql, query);     //执行SQL语句
 
    //获取结果集
    if (!(res = mysql_store_result(&mysql)))    //获得sql语句结束后返回的结果集
    {
        printf("Couldn't get result from %s\n", mysql_error(&mysql));
        return false;
    }
 
    local = localtime(&t);//获取当前系统时间
    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(" 当前系统时间为:%10s\t\n", s);
    //cout << endl;
    char* str_field[32];  //定义一个字符串数组存储字段信息
    for (int i = 0; i < 8; i++)   //在已知字段数量的情况下获取字段名
    {
        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))   //在已知字段数量情况下,获取并打印下一行
    {
        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 当前状况='此车未发出' where 发车时间='");
            strcat(query, column[1]);
            strcat(query, abc);
            mysql_query(&mysql, query);
        }
        else
        {
            sprintf(query, "update testtable set 当前状况='此车已发出'where 发车时间='");
            strcat(query, column[1]);
            strcat(query, abc);
            mysql_query(&mysql, query);
        }
 
        //打印获取的数据
        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是列数组
 
    }
 
    return true;
}
//浏览数据
bool read()
{
    struct tm* local;
    time_t t;
    t = time(NULL);
    sprintf(query, "select * from testTable"); //执行查询语句,这里是查询所有,user是表名,不用加引号,用strcpy也可以
    mysql_query(&mysql, "set names gbk"); //设置编码格式(SET NAMES GBK也行),否则cmd下中文乱码
                                          //返回0 查询成功,返回1查询失败
    mysql_query(&mysql, query);     //执行SQL语句
     
    //获取结果集
    if (!(res = mysql_store_result(&mysql)))    //获得sql语句结束后返回的结果集
    {
        printf("Couldn't get result from %s\n", mysql_error(&mysql));
        return false;
    }
 
    local = localtime(&t);//获取当前系统时间
    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(" 当前系统时间为:%10s\t\n", s);
    cout << endl;
    char* str_field[32];  //定义一个字符串数组存储字段信息
    for (int i = 0; i < 8; i++)   //在已知字段数量的情况下获取字段名
    {
        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))   //在已知字段数量情况下,获取并打印下一行
    {   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 当前状况='此车未发出' where 发车时间='");
            strcat(query,column[1]);
            strcat(query,abc);
            mysql_query(&mysql, query);
        }
        else
        {
            sprintf(query, "update testtable set 当前状况='此车已发出'where 发车时间='");
            strcat(query, column[1]);
            strcat(query, abc);
            mysql_query(&mysql, query);
        }
         
        
    }
    readtest();
    return true;
}
//查询数据
bool search()
{
    int y;
    int m;
    char n[5];
    char s[100];
    char abc[5] = "'";
    printf(" 请输入查询方式:1 按班次号查询 ;2 按终点站查询\n");
    printf(" 请输入您的操作: ");
    cin >> m;
    if (m == 1)
    {
        printf(" 请输入您要查询的班次号:  ");
        cin >> n;
        sprintf(query, "select * from testtable where 班次='"); //执行查询语句,这里是查询所有,user是表名,不用加引号,用strcpy也可以
        strcat(query, n);
        strcat(query, abc);
        mysql_query(&mysql, "set names gbk"); //设置编码格式(SET NAMES GBK也行),否则cmd下中文乱码
 
        mysql_query(&mysql, query);    //执行SQL语句
 
        //获取结果集
        if (!(res = mysql_store_result(&mysql)))    //获得sql语句结束后返回的结果集
        {
            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);  // 获取列数
 
        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是列数组
 
        }
    }
    else if (m == 2)
    {
        printf(" 请输入您要查询的终点站:  ");
        cin >> s;
        sprintf(query, "select * from testTable where 终点站='"); //执行查询语句,这里是查询所有,user是表名,不用加引号,用strcpy也可以
        strcat(query, s);
        strcat(query, abc);
        mysql_query(&mysql, "set names gbk"); //设置编码格式(SET NAMES GBK也行),否则cmd下中文乱码
 
        mysql_query(&mysql, query);    //执行SQL语句
 
        //获取结果集
        if (!(res = mysql_store_result(&mysql)))    //获得sql语句结束后返回的结果集
        {
            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);  // 获取列数
 
        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是列数组
 
        }
    }
    printf("\n");
    printf("  1 继续查询;2 返回主界面\n");
    printf(" 请输入您的操作: ");
    cin >> y;
    if (y == 1)
    {
        search();
    }
    else if (y == 2)
    {
        mainmenu();
    }
    return true;
 
}
//录入数据
bool input()
{
    sprintf(query, "insert into testtable values (6, '18:00', '青岛','济南',3,20,10,'NULL')");  //可以想办法实现手动在控制台手动输入指令
    mysql_query(&mysql, query);      //执行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 班次='"); //执行查询语句,这里是查询所有,user是表名,不用加引号,用strcpy也可以
        strcat(query, k);
        strcat(query, abc);
        mysql_query(&mysql, "set names gbk"); //设置编码格式(SET NAMES GBK也行),否则cmd下中文乱码
 
        mysql_query(&mysql, query);    //执行SQL语句
 
        //获取结果集
        if (!(res = mysql_store_result(&mysql)))    //获得sql语句结束后返回的结果集
        {
            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);  // 获取列数
 
        
        column = mysql_fetch_row(res);
        a = atoi(column[6]);
        b = atoi(column[5]);
        if ((a < b) && (!strcmp(column[7],"此车未发出")))
        {
            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 已订票人数='"); //执行查询语句,这里是查询所有,user是表名,不用加引号,用strcpy也可以
            strcat(query, column[6]);
           strcat(query, abc);
           strcat(query, cc);
             strcat(query, k);
             strcat(query, abc);
            mysql_query(&mysql, "set names gbk"); //设置编码格式(SET NAMES GBK也行),否则cmd下中文乱码
            mysql_query(&mysql, query);    //执行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是列数组
 
        }
    }
    else if (m == 2)//退票
    {
        
        printf(" 请输入您要退订的车票班次:  ");
        cin >> n;
        sprintf(query, "select * from testtable where 班次='"); //执行查询语句,这里是查询所有,user是表名,不用加引号,用strcpy也可以
        strcat(query, n);
        strcat(query, abc);
        mysql_query(&mysql, "set names gbk"); //设置编码格式(SET NAMES GBK也行),否则cmd下中文乱码
 
        mysql_query(&mysql, query);    //执行SQL语句
 
        //获取结果集
        if (!(res = mysql_store_result(&mysql)))    //获得sql语句结束后返回的结果集
        {
            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);  // 获取列数
 
        
        column = mysql_fetch_row(res);
        a = atoi(column[6]);
        if (!strcmp(column[7], "此车未发出"))
        {
            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 已订票人数='"); //执行查询语句,这里是查询所有,user是表名,不用加引号,用strcpy也可以
            strcat(query, column[6]);
            strcat(query, abc);
            strcat(query, cc);
            strcat(query, n);
            strcat(query, abc);
            mysql_query(&mysql, "set names gbk"); //设置编码格式(SET NAMES GBK也行),否则cmd下中文乱码
            mysql_query(&mysql, query);    //执行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是列数组
 
        }
            
        
    }
    printf("\n");
    printf("  1 继续售票/退票;2 返回主界面\n");
    printf(" 请输入您的操作: ");
        cin >> y;
        if (y == 1)
        {
            piao();
        }
        else if (y == 2)
        {
            mainmenu();
        }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_47577137/article/details/112061333

延伸 · 阅读

精彩推荐
  • C/C++C++中新手容易犯的十种编程错误汇总

    C++中新手容易犯的十种编程错误汇总

    一段C语言代码,在编译、链接和运行的各个阶段都可能会出现问题,下面这篇文章主要给大家介绍了关于C++中新手容易犯的十种编程错误的相关资料,需要的...

    IT老张4202022-02-13
  • C/C++C语言实现通讯录程序

    C语言实现通讯录程序

    这篇文章主要为大家详细介绍了C语言实现通讯录程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    久留不见i4972022-01-20
  • C/C++详解C++中的增量运算符++和减量运算符--的用法

    详解C++中的增量运算符++和减量运算符--的用法

    这篇文章主要介绍了C++中的增量运算符++和减量运算符--的用法,分为前缀情况和后缀情况来讲,需要的朋友可以参考下...

    C++教程网7762021-03-19
  • C/C++C++线程同步实例分析

    C++线程同步实例分析

    这篇文章主要介绍了C++线程同步实例分析,以实例的形式较为深入的分析了C++的线程同步问题,是一个较为经典的线程同步问题,需要的朋友可以参考下...

    C++教程网9912021-02-06
  • C/C++详解C++中的const关键字及与C语言中const的区别

    详解C++中的const关键字及与C语言中const的区别

    这篇文章主要介绍了C++中的const关键字及与C语言中const的区别,const将所修饰的变量对象转化为常量,需要的朋友可以参考下...

    dingyuanpu8272021-03-30
  • C/C++在C语言里单引号和双引号的区别

    在C语言里单引号和双引号的区别

    这篇文章主要介绍了在C语言里单引号和双引号的区别,本文通过代码的实例和注释的详细的说明了单引号和双引号的概念与区别,以下就是详细内容,需要的朋...

    叫我谢布斯7482021-11-21
  • C/C++C语言代码实现简单扫雷游戏

    C语言代码实现简单扫雷游戏

    这篇文章主要为大家详细介绍了C语言代码实现简单扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    菜菜是真菜4152021-10-21
  • C/C++C++中for循环与while循环的区别总结

    C++中for循环与while循环的区别总结

    这篇文章主要给大家介绍了关于C++中for循环与while循环的区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习...

    愿与君同赏月6602021-09-30