写在前面
毕业设计:机票订购系统
运行环境:Linux环境
运行前提:已安装MySQL数据库
特别感谢:QQ:593····07
注意事项:本系统仅供参考、仅作代码展示。有一些隐藏功能自行发掘。
主函数(main)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <mysql/mysql.h>
//#include "MySqlFunc.h"
#include "mysqlfunc.h"
void showLoginMenu(); //显示主界面
int RegisterUser(MYSQL * conn, pPerson pPersonHead); //注册用户
int Login(MYSQL * conn, pPerson pPersonHead, pPlane planeHead, pOrder orderHead); //登录操作
int AdminFunc(MYSQL * conn, pPerson pPersonHead,pPlane planeHead, pOrder orderHead); //管理员功能
void AdminMenu(); //打印管理员菜单
int addPlaneInfo(MYSQL * conn, pPlane planeHead); //添加航班
int deletePlaneInfo(MYSQL * conn, pPlane planeHead); //删除航班
int lookPlaneInfo(MYSQL * conn, pPlane planeHead); //浏览航班
int lookOrderInfo(MYSQL * conn, pOrder orderHead); //浏览订票信息
int lookWaitInfo(MYSQL * conn, pOrder orderHead); //浏览候补信息
int UserFunc(MYSQL * conn, pPlane planeHead, pOrder orderHead,PersonNode currPer);//用户功能
int UserMenu();//用户界面主菜单
int addOrder(MYSQL * conn, pPlane planeHead, pOrder orderHead, PersonNode currPer);//订票
int delOrder(MYSQL * conn, pPlane planeHead, pOrder orderHead, PersonNode currPer);//退票
int changeOrder(MYSQL * conn, pPlane planeHead, pOrder orderHead, PersonNode currPer);//改签
int searchOrder(MYSQL * conn, pPlane planeHead, pOrder orderHead, PersonNode currPer);//查单订票信息
int main()
{
pPerson perHead = (pPerson)malloc(sizeof(PersonNode));
pPlane planeHead = (pPlane)malloc(sizeof(PlaneNode));
pOrder orderHead = (pOrder)malloc(sizeof(OrderNode));
int choseNum = 0;
//数据库
MYSQL * conn = mysql_init(NULL);
perHead->next = NULL;
planeHead->next = NULL;
orderHead->next = NULL;
connectMysql(conn); //连接数据库
createPersonTable(conn); //创建乘客表
createPlaneTable(conn); //创建航班表
createOrdeTable(conn); //创建订单表
searchPerson(conn, perHead); //查询所有的人员信息
searchPlane(conn, planeHead); //查询所有的航班信息
while (1)
{
showLoginMenu(); // 调用显示主界面函数
scanf("%d",&choseNum);
switch (choseNum)
{
case 1:
RegisterUser(conn, perHead);
break;
case 2:
Login(conn, perHead, planeHead, orderHead);
break;
case 0:
mysql_close(conn);
return;
default:
printf("选择服务编号错误!\n");
break;
}
}
return 0;
}
void showLoginMenu() //显示主界面函数
{
printf("|------------------ Jy--------------------------|\n");
printf("|------------欢迎进入机票订购系统----------------|\n");
printf("|-----------------------------------------------|\n");
printf("|-----------1.注册------------|\n");
printf("|-----------2.登录------------|\n");
printf("|-----------0.退出------------|\n");
printf("请输入:");
}
int UserFunc(MYSQL * conn, pPlane planeHead, pOrder orderHead, PersonNode currPer) //定义用户主界面函数
{
int chose = 0;
while (1)
{
UserMenu();
scanf("%d", &chose);
switch (chose)
{
case 1:
addOrder(conn, planeHead, orderHead, currPer);//订票
break;
case 2:
delOrder(conn, planeHead, orderHead, currPer);//退票
break;
case 3:
changeOrder(conn, planeHead, orderHead, currPer);//改签
break;
case 4:
lookPlaneInfo(conn,planeHead);//查询航班信息
break;
case 5:
searchOrder(conn, planeHead, orderHead, currPer);//查看订票信息
break;
case 0:
return 0;
break;
default:
printf("选择服务编号错误!\n");
break;
}
}
}
int UserMenu()
{
printf("|------1:订票------------------|\n");
printf("|------2:退票------------------|\n");
printf("|------3:改签------------------|\n");
printf("|------4:查询航班信息----------|\n");
printf("|------5:查询订票信息----------|\n");
printf("|------0:退出系统--------------|\n");
printf("请选择:");
return 0;
}
int addOrder(MYSQL * conn, pPlane planeHead, pOrder orderHead, PersonNode currPer)
{
char planeID[BUFFSIZE];
pPlane find = NULL;
OrderNode node;
int orderSize = 0; //预定数量
int isWait = 0; //是否候补
printf("请输入航班编号:");
scanf("%s",planeID);
find = findPlaneExistByID(planeHead, planeID);
if (find == NULL)
{
printf("没有找到相应的航班!\n");
return 0;
}
printf("请输入购票数量:");
scanf("%d", &orderSize);
if (orderSize > (find->Max_num - find->Buy_num))
{
printf("剩余票数不足,是否进行候补?\n1:候补\n其他:不候补\n");
printf("请输入你的选择:");
scanf("%d",&isWait);
printf("isWait = %d\n", isWait);
switch (isWait)
{
case 1:
memcpy(node.PlaneID, find->PlaneID, strlen(find->PlaneID));
node.PlaneID[strlen(find->PlaneID)] = '\0';
memcpy(node.person_ID, currPer.person_ID, strlen(currPer.person_ID));
node.person_ID[strlen(currPer.person_ID)] = '\0';
node.orderSize = orderSize;
node.OrderStat = 2;
insertOrderNode(orderHead, node);
insertOrder(conn, node);
printf("候补成功!\n");
break;
default:
break;
}
}
else
{
memcpy(node.PlaneID, find->PlaneID, strlen(find->PlaneID));
node.PlaneID[strlen(find->PlaneID)] = '\0';
memcpy(node.person_ID, currPer.person_ID, strlen(currPer.person_ID));
node.person_ID[strlen(currPer.person_ID)] = '\0';
node.orderSize = orderSize;
node.OrderStat = 1;
insertOrderNode(orderHead, node);
insertOrder(conn, node);
//修改票信息
find->Buy_num += orderSize;
updatePlane(conn, *find);
printf("购票成功!\n");
}
return 1;
}
int delOrder(MYSQL * conn, pPlane planeHead, pOrder orderHead, PersonNode currPer)
{
int id;
pOrder findOrder = NULL;
pPlane findPlane = NULL;
OrderNode node;
pOrder p = (pOrder)malloc(sizeof(OrderNode));
p->next = NULL;
searchOrderByPersonID(conn, p, currPer.person_ID);
printf("请输入订购ID:");
scanf("%d",&id);
findOrder = findOrderExistByID(p, id);
if (findOrder == NULL)
{
printf("没有找到订单编号!\n");
return 0;
}
deleteOrder(conn, id);
node = *findOrder;
findPlane = findPlaneExistByID(planeHead, node.PlaneID);
findPlane->Buy_num -= node.orderSize;
updatePlane(conn, *findPlane);
clearOrderNode(p);
printf("退票成功!\n");
return 1;
}
int changeOrder(MYSQL * conn, pPlane planeHead, pOrder orderHead, PersonNode currPer)
{
int id = 0, orderSize = 0;
char planeID[BUFFSIZE];
pOrder findOrder = NULL;
pPlane findPlane = NULL;
pPlane sourPlane = NULL;
OrderNode node;
pOrder p = (pOrder)malloc(sizeof(OrderNode));
p->next = NULL;
searchOrderByPersonID(conn, p, currPer.person_ID);
printf("请输入要改签的订购ID:");
scanf("%d", &id);
findOrder = findOrderExistByID(p, id);
if (findOrder == NULL)
{
printf("没有找到订单编号!\n");
return 0;
}
node = *findOrder;
printf("请输入要改签的航班编号:");
scanf("%s", planeID);
findPlane = findPlaneExistByID(planeHead, planeID);
if (findPlane == NULL)
{
printf("没有找到要改签的航班编号!\n");
return 0;
}
printf("请输入要改签的购票数量:");
scanf("%d", &orderSize);
if (orderSize > (findPlane->Max_num - findPlane->Buy_num))
{
printf("余票不足,改签失败!\n");
return 0;
}
sourPlane = findPlaneExistByID(planeHead, node.PlaneID);
sourPlane->Buy_num -= node.orderSize;
findPlane->Buy_num += orderSize;
memcpy(node.PlaneID, findPlane->PlaneID, strlen(findPlane->PlaneID));
node.PlaneID[strlen(findPlane->PlaneID)] = '\0';
node.orderSize = orderSize;
updatePlane(conn, *sourPlane);
updatePlane(conn, *findPlane);
updateOrder(conn, node);
printf("改签成功!\n");
return 1;
}
int searchOrder(MYSQL * conn, pPlane planeHead, pOrder orderHead, PersonNode currPer) //查看订票信息
{
pOrder p = (pOrder)malloc(sizeof(OrderNode));
p->next = NULL;
searchOrderByPersonID(conn, p, currPer.person_ID);
printf("订单编号:\t航班编号:\t购票数量:\n");
p = p->next;
while (p)
{
printf("%-16d", p->id);
printf("%-16s", p->PlaneID);
printf("%-16d\n", p->orderSize);
p = p->next;
}
clearOrderNode(p);
return 1;
}
int AdminFunc(MYSQL * conn, pPerson pPersonHead, pPlane planeHead, pOrder orderHead) //管理员功能
{
int chose = 0;
while (1)
{
AdminMenu();
scanf("%d",&chose);
switch (chose)
{
case 1:
addPlaneInfo(conn, planeHead);
break;
case 2:
deletePlaneInfo(conn, planeHead);
break;
case 3:
lookPlaneInfo(conn, planeHead);
break;
case 4:
lookOrderInfo(conn, orderHead);
break;
case 5:
lookWaitInfo(conn, orderHead);
break;
case 0:
return 0;
break;
default:
printf("选择服务编号错误!\n");
break;
}
}
}
void AdminMenu() //打印管理员菜单
{
printf("|------1:增加航班信息----------|\n");
printf("|------2:删除航班信息----------|\n");
printf("|------3:查询航班信息----------|\n");
printf("|------4:浏览已定票信息--------|\n");
printf("|------5:查看候补名单----------|\n");
printf("|------0:退出系统--------------|\n");
printf("请选择:");
}
int addPlaneInfo(MYSQL * conn, pPlane planeHead) //添加航班
{
PlaneNode node;
pPlane find = NULL;
node.Buy_num = 0;
printf("输入航班编号:");
scanf("%s", node.PlaneID);
find = findPlaneExistByID(planeHead, node.PlaneID);
if (find != NULL)
{
printf("航班编号已存在,添加失败!\n");
return 0;
}
printf("输入始发地:");
scanf("%s", node.Start_Place);
printf("输入目的地:");
scanf("%s", node.End_Place);
printf("输入航班起飞时间:");
scanf("%s", node.time);
printf("输入价格:");
scanf("%f", &node.price);
printf("输入航班可乘坐最大人数:");
scanf("%d", &node.Max_num);
insertPlane(conn, node); //插入数据库
insertPlaneNode(planeHead,node); //插入链表节点
printf("插入航班成功!\n");
return 1;
}
int deletePlaneInfo(MYSQL * conn, pPlane planeHead) //删除航班
{
PlaneNode node;
pPlane find = NULL;
node.Buy_num = 0;
printf("输入航班编号:");
scanf("%s", node.PlaneID);
find = findPlaneExistByID(planeHead, node.PlaneID);
if (find == NULL)
{
printf("航班编号不存在,操作失败!\n");
return 0;
}
deletePlane(conn, node.PlaneID); //从数据库中删除
delPlaneNode(&planeHead, node.PlaneID);
deleteOrderByPlaneID(conn, node.PlaneID);
printf("删除航班成功!\n");
return 1;
}
int lookPlaneInfo(MYSQL * conn, pPlane planeHead) //浏览航班
{
pPlane p = (pPlane)malloc(sizeof(PlaneNode));
p->next = NULL;
searchPlane(conn, p);
printf("航班编号:\t起始地点:\t到达地点:\t票价:\t\t已售数量:\t最大数量:\t起飞时间:\n");
p = p->next;
while (p)
{
printf("%-16s", p->PlaneID);
printf("%-16s", p->Start_Place);
printf("%-16s", p->End_Place);
printf("%-16f", p->price);
printf("%-16d", p->Buy_num);
printf("%-16d", p->Max_num);
printf("%-16s\n", p->time);
p = p->next;
}
clearPlaneNode(p);
return 1;
}
int lookOrderInfo(MYSQL * conn, pOrder orderHead) //浏览订票信息
{
pOrder p = (pOrder)malloc(sizeof(OrderNode));
p->next = NULL;
searchOrderByStat(conn, p, 1);
printf("订单编号:\t航班编号:\t购票人编号:\t购票数量:\n");
p = p->next;
while (p)
{
printf("%-16d", p->id);
printf("%-16s", p->PlaneID);
printf("%-16s", p->person_ID);
printf("%-16d\n", p->orderSize);
p = p->next;
}
clearOrderNode(p);
return 1;
}
int lookWaitInfo(MYSQL * conn, pOrder orderHead) //浏览候补信息
{
pOrder p = (pOrder)malloc(sizeof(OrderNode));
p->next = NULL;
searchOrderByStat(conn, p, 2);
printf("订单编号:\t航班编号:\t购票人编号:\t购票数量:\n");
p = p->next;
while (p)
{
printf("%-16d", p->id);
printf("%-16s", p->PlaneID);
printf("%-16s", p->person_ID);
printf("%-16d\n", p->orderSize);
p = p->next;
}
clearOrderNode(p);
return 1;
}
int RegisterUser(MYSQL * conn, pPerson pPersonHead) //注册用户
{
PersonNode node;
pPerson find;
printf("请输入ID号:");
scanf("%s", node.person_ID);
find = findPersonExistByID(pPersonHead, node.person_ID);
if (find != NULL)
{
printf("ID已存在,注册失败!\n");
return 0;
}
printf("请输入姓名:");
scanf("%s", node.person_name);
printf("请输入密码:");
scanf("%s", node.person_Pwd);
insertPerson(conn, node);//写入数据库
insertPersonNode(pPersonHead, node); //插入链表节点
printf("注册成功!\n");
return 1;
}
int Login(MYSQL * conn, pPerson pPersonHead, pPlane planeHead, pOrder orderHead)
{
PersonNode node;
pPerson find = NULL;
int userSize = 0;
printf("请输入ID号:");
scanf("%s", node.person_ID);
printf("请输入密码:");
scanf("%s", node.person_Pwd);
if ((strcmp(node.person_ID,"12306") == 0) && (strcmp(node.person_Pwd, "12306") == 0)) //管理员
{
AdminFunc(conn, pPersonHead, planeHead, orderHead);
}
else
{
find = findPersonExistByID(pPersonHead, node.person_ID);
if (find == NULL)
{
printf("ID不存在,登录失败!\n");
return 0;
}
if ((strcmp(node.person_ID, find->person_ID) == 0) && (strcmp(node.person_Pwd, find->person_Pwd) == 0)) //乘客
{
UserFunc(conn, planeHead, orderHead, node);
}
else
{
printf("密码错误,登录失败!\n");
return 0;
}
}
return 1;
}
头文件<linkfunc.h>
#ifndef LINKFUNC_H
#define LINKFUNC_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 32
//定义航班信息结构体
typedef struct _PlaneNode
{
int id; //主键ID
char PlaneID[BUFFSIZE]; //定义飞机编号
char Start_Place[BUFFSIZE]; //定义起始地点
char End_Place[BUFFSIZE]; //定义最终地点
float price; //定义机票价格
int Buy_num; //定义已售的票数
int Max_num; //定义最大票数
char time[BUFFSIZE]; //定义起飞时间
struct _PlaneNode *next;
}PlaneNode,*pPlane;
//定义乘客信息结构体
typedef struct _PersonNode
{
int id;
char person_ID[BUFFSIZE]; //定义客户ID(联系方式,唯一值)
char person_name[BUFFSIZE]; //定义乘客姓名
char person_Pwd[BUFFSIZE]; //密码
struct _PersonNode *next;
}PersonNode,*pPerson;
//定义订单结构(退票,订票)
typedef struct _OrderNode
{
int id; //编号
char PlaneID[BUFFSIZE]; //定义飞机编号
char person_ID[BUFFSIZE]; //用户ID
int orderSize; //订票/候补/退票,数量
int OrderStat; //订单状态(0:退票,1:订票,2:候补)
struct _OrderNode *next;
}OrderNode,*pOrder;
int insertPlaneNode(pPlane pPlaneHead, PlaneNode plane); //插入航班节点
pPlane findPlaneExistByID(pPlane head, char *val);
int clearPlaneNode(pPlane pPlaneHead); //清空航班节点
void deletePlaneHead(pPlane *pHead);//删除头
pPlane delPlaneNode(pPlane *pHead, char *num); //根据学号删除节点
int insertPersonNode(pPerson pPersonHead, PersonNode person); //插入乘客节点
pPerson findPersonExistByID(pPerson head, char *val);
int clearPersonNode(pPerson pPersonHead); //清空乘客节点
int insertOrderNode(pOrder pOrderHead, OrderNode order); //插入订单节点
int clearOrderNode(pOrder pOrderHead); //清空订单节点
void deleteOrderHead(pOrder *pHead);//删除头
pOrder delOrderNode(pOrder *pHead, int id); //根据学号删除节点
pOrder findOrderExistByID(pOrder head, int id);
int insertPlaneNode(pPlane pPlaneHead, PlaneNode val) //插入航班节点
{
PlaneNode *pre = NULL, *p = NULL, *n = NULL;
pre = pPlaneHead;
p = pPlaneHead->next;
while (p != NULL)
{
pre = p;
p = p->next;
}
n = (pPlane)malloc(sizeof(PlaneNode));
n->id = val.id;
strcpy(n->PlaneID, val.PlaneID);
n->PlaneID[strlen(val.PlaneID)] = '\0';
strcpy(n->Start_Place, val.Start_Place);
n->Start_Place[strlen(val.Start_Place)] = '\0';
strcpy(n->End_Place, val.End_Place);
n->End_Place[strlen(val.End_Place)] = '\0';
strcpy(n->time, val.time);
n->time[strlen(val.time)] = '\0';
n->price = val.price;
n->Buy_num = val.Buy_num;
n->Max_num = val.Max_num;
n->next = pre->next;
pre->next = n;
return 1;
}
pPlane findPlaneExistByID(pPlane head, char *val)
{
pPlane cur = head;
for (; cur != NULL; cur = cur->next)
{
if (strcmp(cur->PlaneID, val) == 0)
{
return cur; //查找到返回当前
}
}
return NULL; //没有查找到返回NULL;
}
void deletePlaneHead(pPlane *pHead)//删除头
{
pPlane t;
if (*pHead == NULL)
{
return;
}
else
{
t = *pHead;
*pHead = (*pHead)->next;
free(t);
}
}
pPlane delPlaneNode(pPlane *pHead, char *num) //根据学号删除节点
{
pPlane t, n;
if (*pHead == NULL)//如果空表退出
{
return NULL;
}
else
{
if (strcmp((*pHead)->PlaneID, num) == 0)//如果第一个就是,删头
{
deletePlaneHead(pHead);
}
else
{
t = *pHead;
while (t->next != NULL && strcmp(t->next->PlaneID, num) != 0)//遍历,若t的next为空或者next是要找的节点则退出
{
t = t->next;
}
if (t->next)//如果t指向要找的结点的上一个节点
{
n = t->next->next;//删除要找的结点
free(t->next);
t->next = n;
}
}
}
return NULL;
}
int clearPlaneNode(pPlane pPlaneHead) //清空航班节点
{
pPlane p = NULL;
while (pPlaneHead != NULL)
{
p = pPlaneHead;
pPlaneHead = pPlaneHead->next;
p->next = NULL;
free(p);
p = NULL;
}
return 0;
}
int insertPersonNode(pPerson pPersonHead, PersonNode val) //插入乘客节点
{
PersonNode *pre = NULL, *p = NULL, *n = NULL;
pre = pPersonHead;
p = pPersonHead->next;
while (p != NULL)
{
pre = p;
p = p->next;
}
n = (pPerson)malloc(sizeof(PersonNode));
n->id = val.id;
strcpy(n->person_ID, val.person_ID);
n->person_ID[strlen(val.person_ID)] = '\0';
strcpy(n->person_name, val.person_name);
n->person_name[strlen(val.person_name)] = '\0';
strcpy(n->person_Pwd, val.person_Pwd);
n->person_Pwd[strlen(val.person_Pwd)] = '\0';
n->next = pre->next;
pre->next = n;
return 1;
}
int clearPersonNode(pPerson pPersonHead) //清空乘客节点
{
pPerson p = NULL;
while (pPersonHead != NULL)
{
p = pPersonHead;
pPersonHead = pPersonHead->next;
p->next = NULL;
free(p);
p = NULL;
}
return 0;
}
pPerson findPersonExistByID(pPerson head, char *val) //查找乘客编号是否存在
{
pPerson cur = head;
for (; cur != NULL; cur = cur->next)
{
if (strcmp(cur->person_ID, val) == 0)
{
return cur; //查找到返回当前
}
}
return NULL; //没有查找到返回NULL;
}
int insertOrderNode(pOrder pOrderHead, OrderNode val) //插入订单节点
{
OrderNode *pre = NULL, *p = NULL, *n = NULL;
pre = pOrderHead;
p = pOrderHead->next;
while (p != NULL)
{
pre = p;
p = p->next;
}
n = (pOrder)malloc(sizeof(OrderNode));
n->id = val.id;
strcpy(n->person_ID, val.person_ID);
n->person_ID[strlen(val.person_ID)] = '\0';
strcpy(n->PlaneID, val.PlaneID);
n->PlaneID[strlen(val.PlaneID)] = '\0';
n->orderSize = val.orderSize;
n->OrderStat = val.OrderStat;
n->next = pre->next;
pre->next = n;
return 1;
}
void deleteOrderHead(pOrder *pHead)//删除头
{
pOrder t;
if (*pHead == NULL)
{
return;
}
else
{
t = *pHead;
*pHead = (*pHead)->next;
free(t);
}
}
pOrder delOrderNode(pOrder *pHead,int id) //根据学号删除节点
{
pOrder t, n;
if (*pHead == NULL)//如果空表退出
{
return NULL;
}
else
{
if ((*pHead)->id == id)//如果第一个就是,删头
{
deleteOrderHead(pHead);
}
else
{
t = *pHead;
while (t->next != NULL && (*pHead)->id != id)//遍历,若t的next为空或者next是要找的节点则退出
{
t = t->next;
}
if (t->next)//如果t指向要找的结点的上一个节点
{
n = t->next->next;//删除要找的结点
free(t->next);
t->next = n;
}
}
}
return NULL;
}
pOrder findOrderExistByID(pOrder head, int id)
{
pOrder cur = head;
for (; cur != NULL; cur = cur->next)
{
if (cur->id == id)
{
return cur; //查找到返回当前
}
}
return NULL; //没有查找到返回NULL;
}
int clearOrderNode(pOrder pOrderHead) //清空订单节点
{
pOrder p = NULL;
while (pOrderHead != NULL)
{
p = pOrderHead;
pOrderHead = pOrderHead->next;
p->next = NULL;
free(p);
p = NULL;
}
return 0;
}
#endif
头文件<mysqlfunc.h>
#ifndef MYSQLFUNC_H
#define MYSQLFUNC_H
#include "linkfunc.h"
#include <mysql/mysql.h>
//#include "mysql.h"
int connectMysql(MYSQL *conn); //连接数据库
int createPersonTable(MYSQL *conn); //创建乘客表
int insertPerson(MYSQL *conn, PersonNode val); //插入乘客信息
int searchPerson(MYSQL *conn, pPerson pPersonHead); //查询乘客信息
int showPersonResult(MYSQL_RES * result, pPerson pPersonHead); //显示查询结果
int createPlaneTable(MYSQL *conn); //创建航班表
int insertPlane(MYSQL *conn, PlaneNode val); //插入航班信息
int deletePlane(MYSQL *conn, char *num); //删除航班信息
int updatePlane(MYSQL *conn, PlaneNode val); //更新航班信息
int searchPlane(MYSQL *conn, pPlane pPlaneHead); //查询航班信息
int showPlaneResult(MYSQL_RES * result, pPlane pPersonHead); //显示查询结果
int createOrdeTable(MYSQL *conn); //创建订单表
int insertOrder(MYSQL *conn, OrderNode val); //插入航班信息
int searchOrderByStat(MYSQL *conn, pOrder pPlaneHead,int stat); //查询订单信息
int deleteOrder(MYSQL *conn, int id); //删除订单信息
int deleteOrderByPlaneID(MYSQL *conn, char *planeid); //删除订单信息
int updateOrder(MYSQL *conn, OrderNode val); //更新订单信息
int searchOrderByPersonID(MYSQL *conn, pOrder pPlaneHead,char *userID); //查询订单信息
int showOrderResult(MYSQL_RES * result, pOrder pPersonHead); //显示查询结果
int connectMysql(MYSQL *conn) //连接数据库
{
char server[] = "localhost";
char user[] = "root";
char password[] = "111111";
char database[] = "project";
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int createPersonTable(MYSQL *conn) //创建乘客表
{
char *createTable = "create table if not exists t_Person (ID int AUTO_INCREMENT NOT NULL PRIMARY KEY,\
PersonID varchar(32),PersonName varchar(32),PersonPwd varchar(32))";
if (mysql_query(conn, createTable))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int insertPerson(MYSQL *conn, PersonNode val) //插入乘客信息
{
//char *insert1 = "insert into t_Person (PersonID,PersonName,PersonPwd) values (\"H01288\",\"CAN\",\"SHA\",4815,\"2022-01-09 19:35:00\")";
char *temp = "insert into t_Person(PersonID, PersonName, PersonPwd) values( ";
char buff[500];
sprintf(buff,"%s \"%s\",\"%s\",\"%s\")", temp, val.person_ID, val.person_name, val.person_Pwd);
//memcpy(buff, temp, strlen(temp));
if (mysql_query(conn, buff))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int searchPerson(MYSQL *conn, pPerson pPersonHead) //查询乘客信息
{
MYSQL_RES *res;
char *search = "select * from t_Person";
if (mysql_query(conn, search))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
res = mysql_store_result(conn);
if (res != NULL) {
//需要打印结果集
showPersonResult(res, pPersonHead);
mysql_free_result(res);//释放结果集
}
return 1;
}
int showPersonResult(MYSQL_RES * result, pPerson pPersonHead) //显示查询结果
{
//打印表头
unsigned int num_fields;
PersonNode node;
int i = 0;
// MYSQL_FIELD 该结构包含关于字段的信息,如字段名、类型和大小
//MYSQL_FIELD *fields;
// 返回结果集中的行数。
//num_fields = mysql_num_fields(result);
// mysql_fetch_fields 对于结果集,返回所有MYSQL_FIELD结构的数组。每个结构提供了结果集中1列的字段定义。
/*fields = mysql_fetch_fields(result);
// 输出结果
for (i = 0; i < num_fields; i++)
{
printf("%-16s", fields[i].name);
}
printf("\n");*/
// 行数
MYSQL_ROW row;
num_fields = mysql_num_fields(result);//取字段个数
while ((row = mysql_fetch_row(result)))//循环取一行
{
if (num_fields == 4)
{
node.id = atoi(row[0]);
memcpy(node.person_ID, row[1], strlen(row[1]));
node.person_ID[strlen(row[1])] = '\0';
memcpy(node.person_name, row[2], strlen(row[2]));
node.person_name[strlen(row[2])] = '\0';
memcpy(node.person_Pwd, row[3], strlen(row[3]));
node.person_Pwd[strlen(row[3])] = '\0';
insertPersonNode(pPersonHead, node);
}
/*for (i = 0; i < num_fields; i++)
{
// 看是否为空
printf("%-16s", row[i] ? row[i] : "NULL");
}
printf("\n");*/
}
return 1;
}
int createPlaneTable(MYSQL *conn) //创建航班表
{
char *createTable = "create table if not exists t_air (ID int AUTO_INCREMENT NOT NULL PRIMARY KEY,\
CODE varchar(32),Stare_location varchar(32),End_location varchar(32),Price float,Buy_num int,Max_num int,PlaneDate varchar(32))";
if (mysql_query(conn, createTable))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int insertPlane(MYSQL *conn, PlaneNode val) //插入航班信息
{
char *temp = "insert into t_air(CODE, Stare_location, End_location,Price,Buy_num,Max_num,PlaneDate) values( ";
char buff[500];
sprintf(buff, "%s \"%s\",\"%s\",\"%s\",%f,%d,%d,\"%s\")", temp, val.PlaneID, val.Start_Place, val.End_Place,\
val.price,val.Buy_num,val.Max_num,val.time);
//memcpy(buff, temp, strlen(temp));
if (mysql_query(conn, buff))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int deletePlane(MYSQL *conn, char *num) //删除航班信息
{
char *temp = "delete from t_air where CODE = ";
char buff[500];
sprintf(buff, "%s \"%s\"", temp, num);
//printf("buff == %s\n",buff);
//memcpy(buff, temp, strlen(temp));
if (mysql_query(conn, buff))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int updatePlane(MYSQL *conn, PlaneNode val) //更新航班信息
{
char buff[500];
sprintf(buff, "update t_air set Buy_num = %d where CODE = \"%s\"", val.Buy_num, val.PlaneID);
//memcpy(buff, temp, strlen(temp));
if (mysql_query(conn, buff))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int searchPlane(MYSQL *conn, pPlane pPlaneHead) //查询航班信息
{
MYSQL_RES *res;
char *search = "select * from t_air";
if (mysql_query(conn, search))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
res = mysql_store_result(conn);
if (res != NULL) {
//需要打印结果集
showPlaneResult(res, pPlaneHead);
mysql_free_result(res);//释放结果集
}
return 1;
}
int showPlaneResult(MYSQL_RES * result, pPlane pPersonHead)
{
//打印表头
unsigned int num_fields;
PlaneNode node;
int i = 0;
// MYSQL_FIELD 该结构包含关于字段的信息,如字段名、类型和大小
//MYSQL_FIELD *fields;
// 返回结果集中的行数。
//num_fields = mysql_num_fields(result);
// mysql_fetch_fields 对于结果集,返回所有MYSQL_FIELD结构的数组。每个结构提供了结果集中1列的字段定义。
/*fields = mysql_fetch_fields(result);
// 输出结果
for (i = 0; i < num_fields; i++)
{
printf("%-16s", fields[i].name);
}
printf("\n");*/
// 行数
MYSQL_ROW row;
num_fields = mysql_num_fields(result);//取字段个数
while ((row = mysql_fetch_row(result)))//循环取一行
{
if (num_fields == 8)
{
node.id = atoi(row[0]);
memcpy(node.PlaneID, row[1], strlen(row[1]));
node.PlaneID[strlen(row[1])] = '\0';
memcpy(node.Start_Place, row[2], strlen(row[2]));
node.Start_Place[strlen(row[2])] = '\0';
memcpy(node.End_Place, row[3], strlen(row[3]));
node.End_Place[strlen(row[3])] = '\0';
node.price = atof(row[4]);
node.Buy_num = atoi(row[5]);
node.Max_num = atoi(row[6]);
memcpy(node.time, row[7], strlen(row[7]));
node.time[strlen(row[7])] = '\0';
insertPlaneNode(pPersonHead, node);
}
}
return 1;
}
int createOrdeTable(MYSQL *conn) //创建订单表
{
char *createTable = "create table if not exists t_Order (ID int AUTO_INCREMENT NOT NULL PRIMARY KEY,\
PlaneID varchar(32),person_ID varchar(32),orderSize int,OrderStat int)";
if (mysql_query(conn, createTable))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int insertOrder(MYSQL *conn, OrderNode val) //插入航班信息
{
char *temp = "insert into t_Order(PlaneID, person_ID, orderSize,OrderStat) values( ";
char buff[500];
sprintf(buff, "%s \"%s\",\"%s\",%d,%d)", temp, val.PlaneID, val.person_ID, val.orderSize, val.OrderStat);
//memcpy(buff, temp, strlen(temp));
if (mysql_query(conn, buff))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int searchOrderByStat(MYSQL *conn, pOrder pPlaneHead, int stat)
{
MYSQL_RES *res;
char *temp = "select * from t_Order where OrderStat = ";
char buff[500];
sprintf(buff, "%s %d", temp, stat);
if (mysql_query(conn, buff))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
res = mysql_store_result(conn);
if (res != NULL) {
//需要打印结果集
showOrderResult(res, pPlaneHead);
mysql_free_result(res);//释放结果集
}
return 1;
}
int deleteOrder(MYSQL *conn, int id) //删除订单信息
{
char *temp = "delete from t_Order where id = ";
char buff[500];
sprintf(buff, "%s %d", temp, id);
if (mysql_query(conn, buff))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int deleteOrderByPlaneID(MYSQL *conn, char *planeid) //删除订单信息
{
char *temp = "delete from t_Order where PlaneID = ";
char buff[500];
sprintf(buff, "%s \"%s\"", temp, planeid);
if (mysql_query(conn, buff))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int updateOrder(MYSQL *conn, OrderNode val) //更新订单信息
{
char buff[500];
sprintf(buff, "update t_Order set PlaneID = \"%s\",orderSize = %d,OrderStat = %d where id = %d", val.PlaneID, val.orderSize, val.OrderStat, val.id);
//memcpy(buff, temp, strlen(temp));
if (mysql_query(conn, buff))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
return 1;
}
int searchOrderByPersonID(MYSQL *conn, pOrder pPlaneHead, char *userID) //查询订单信息
{
MYSQL_RES *res;
char *temp = "select * from t_Order where person_ID = ";
char buff[500];
sprintf(buff, "%s \"%s\" and OrderStat = 1", temp, userID);
if (mysql_query(conn, buff))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}
res = mysql_store_result(conn);
if (res != NULL) {
//需要打印结果集
showOrderResult(res, pPlaneHead);
mysql_free_result(res);//释放结果集
}
return 1;
}
int showOrderResult(MYSQL_RES * result, pOrder pPersonHead) //显示查询结果
{
//打印表头
unsigned int num_fields;
OrderNode node;
int i = 0;
// MYSQL_FIELD 该结构包含关于字段的信息,如字段名、类型和大小
//MYSQL_FIELD *fields;
// 返回结果集中的行数。
//num_fields = mysql_num_fields(result);
// mysql_fetch_fields 对于结果集,返回所有MYSQL_FIELD结构的数组。每个结构提供了结果集中1列的字段定义。
/*fields = mysql_fetch_fields(result);
// 输出结果
for (i = 0; i < num_fields; i++)
{
printf("%-16s", fields[i].name);
}
printf("\n");*/
// 行数
MYSQL_ROW row;
num_fields = mysql_num_fields(result);//取字段个数
while ((row = mysql_fetch_row(result)))//循环取一行
{
if (num_fields == 5)
{
node.id = atoi(row[0]);
memcpy(node.person_ID, row[2], strlen(row[2]));
node.person_ID[strlen(row[2])] = '\0';
memcpy(node.PlaneID, row[1], strlen(row[1]));
node.PlaneID[strlen(row[1])] = '\0';
node.orderSize = atoi(row[3]);
node.OrderStat = atoi(row[4]);
insertOrderNode(pPersonHead, node);
}
}
return 1;
}
#endif