图书馆管理系统(附源码及操作视频动图演示)
搭建思路图:
文章图片
操作使用链表实现,编译器使用vs2019
成员框架搭建: 用户进入操作界面,选择添加机票信息,包括(飞机航班号,出发地,目的地,起飞时间,降落时间,飞机票价,当日折扣,机票总数)
进入订票系统,输入起飞地,目的地,调用查询函数会打印机票信息到屏幕,若没有则告知,如果有,订票人开始订票。输入(姓名,身份证号,性别,订购航班,购票数量)
文章图片
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#includestruct AirPlane
{
char acFlight[10];
//航班号
char acOrigin[10];
//出发地
char acDest[10];
//目的地
char acTakeOffTime[10];
//起飞时间
char acReverveTime[10];
//降落时间
float fPrice;
//票价
char acDiscount[4];
//折扣
int iNum;
//票数
};
struct Man
{
char acName[20];
//姓名
char acID[20];
//身份证号
char acSex[10];
//性别
int iBookNum;
//购票数量
char acBookFilight[10];
//订购的航班号
};
//定义机票信息结点的结构体
typedef struct PlaneNode
{
struct AirPlane data;
struct PlaneNode* next;
}PlaneNode, * PLNode;
//定义订票人信息结点的结构体
typedef struct ManNode
{
struct Man data;
struct ManNode* next;
}ManNode, * MANNode;
int iSave = 0;
//作为数据是否改动的标志
操作主页面及主函数:
void Menu()
{
puts("***********************************************************");
puts("****** Welcome to the airplane tickets booking system *****");
puts("-----------------------------------------------------------");
puts("******Choose the following operations(0-9)*****");
puts("-----------------------------------------------------------");
puts("**1.Insert fligths2.Search fligths**");
puts("**3.Book fligths4.Modify fligths**");
puts("**5.Show fligths6.Recommed fligths**");
puts("**7.Refund fligths8.NowTime fligths**");
puts("**9.Save Messages0.Exist**");
puts("-----------------------------------------------------------");
}
int main()
{
PlaneNode pstPlaneNodeHead;
//机票信息头结点
pstPlaneNodeHead.next = NULL;
ManNode pstManNodeHead;
//订票人头结点
pstManNodeHead.next = NULL;
int res;
res = Init(&pstPlaneNodeHead, &pstManNodeHead);
//将文件的数据加载到内存中
if (res < 0)
{
return 0;
}
int iSel;
//用于接收用户对于功能的选择
char c1;
//是否保存
while(1)
{
system("cls");
Menu();
printf("Input 0-9 operations:");
scanf("%d", &iSel);
getchar();
system("cls");
switch(iSel)
{
case 1:
//Insert(&pstPlaneNodeHead);
//添加机票信息
break;
case 2:
//Search(&pstPlaneNodeHead);
//查询机票信息
break;
case 3:
//Book(&pstManNodeHead, &pstPlaneNodeHead);
//预定机票系统
break;
case 4:
//Modify(&pstPlaneNodeHead);
//修改机票信息
break;
case 5:
//Show(&pstPlaneNodeHead);
//显示所有机票信息
break;
case 6:
//Recommed(&pstPlaneNodeHead);
//推荐机票信息
break;
case 7:
//Refund(&pstManNodeHead, &pstPlaneNodeHead);
//退票操作
break;
case 8:
//NowTime();
//显示当前时间
break;
case 9:
//SaveMan(&pstManNodeHead);
//保存订票人信息
//SavePlane(&pstPlaneNodeHead);
//保存机票信息
case 0:
if (iSave == 1)
{
printf("Do you want to save message (y/n)");
scanf("%c", &c1);
getchar();
if (c1 == 'y' || c1 == 'Y')
{
//SaveMan(&pstManNodeHead);
//保存订票人的信息
//SavePlane(&pstPlaneNodeHead);
//保存机票信息
}
}
//Destroy(pstManNodeHead,pstPlaneNodeHead);
return 0;
}
printf("please press any key continue...");
_getch();
}
//Destroy(pstManNodeHead,pstPlaneNodeHead);
return 0;
}
文章图片
函数实现: 一.添加机票信息 将添加的机票信息文件写入内存中,之后的每次操作都在内存中进行
int Init(PLNode pstPlaneNodeHead, MANNode pstManNodeHead)//将文件的数据加载到内存中
{
//先加载飞机机票信息
FILE* pfplane;
//定义飞机机票文件指针
PlaneNode* pstPlaneNodeTemp, * pstPlaneNodeCur;
pstPlaneNodeCur = pstPlaneNodeHead;
pstPlaneNodeTemp = NULL;
pfplane = fopen("plane.txt", "ab+");
if (pfplane == NULL)
{
printf("Can't open plane.txt!\n");
return -1;
}
else
{
//把文件数据读入链表中
while (!feof(pfplane))
{
pstPlaneNodeTemp = (PlaneNode*)malloc(sizeof(PlaneNode));
//申请临时结点
if (fread(pstPlaneNodeTemp, sizeof(PlaneNode), 1, pfplane) == 1)
{
pstPlaneNodeTemp->next = NULL;
pstPlaneNodeCur->next = pstPlaneNodeTemp;
pstPlaneNodeCur = pstPlaneNodeTemp;
}
}
free(pstPlaneNodeTemp);
fclose(pfplane);
}
//2.再加载订票人信息
FILE* pfman;
//定义文件指针
ManNode* pstManNodeTemp, * pstManNodeCur;
pstManNodeCur = pstManNodeHead;
pstManNodeTemp = NULL;
pfman = fopen("man.txt", "ab+");
if (pfman == NULL)
{
printf("Can't open man.txt!\n");
return -1;
}
else
{
//把文件数据读入链表中
while (!feof(pfman))
{
pstManNodeTemp = (ManNode*)malloc(sizeof(ManNode));
//申请临时结点
if (fread(pstManNodeTemp, sizeof(ManNode), 1, pfman) == 1)
{
pstManNodeTemp->next = NULL;
pstManNodeCur->next = pstManNodeTemp;
pstManNodeCur = pstManNodeTemp;
}
}
free(pstManNodeTemp);
fclose(pfman);
}
return 0;
}
void Insert(PLNode pstPlaneNodeHead)//添加机票信息
{
PlaneNode* pstHead, * pstTail, * pstCur, * pstNew;
char acFlight[10];
//保存航班号
pstHead = pstTail = pstPlaneNodeHead;
//找尾巴
//让pstTail指向最后一个结点
while (pstTail->next != NULL)
{
pstTail = pstTail->next;
}
while (1)
{
printf("Input the new flight number(-1 to end):");
scanf("%s", acFlight);
getchar();
if (strcmp(acFlight, "-1") == 0)
{
break;
}
//判断航班号是否重复
pstCur = pstPlaneNodeHead->next;
while (pstCur != NULL)
{
if (strcmp(pstCur->data.acFlight, acFlight) == 0)
{
printf("this flight %s exists!\n", acFlight);
return;
}
pstCur = pstCur->next;
}
//如果航班号没有和现有纪录重复,则新建一个链表结点
pstNew = (PlaneNode*)malloc(sizeof(PlaneNode));
strcpy(pstNew->data.acFlight, acFlight);
printf("Input the Start City:");
scanf("%s", pstNew->data.acOrigin);
printf("Input the Dest City:");
scanf("%s", pstNew->data.acDest);
printf("Input the Departure time (Format 00:00):");
scanf("%s", pstNew->data.acTakeOffTime);
printf("Input the Arrival time (Format 00:00):");
scanf("%s", pstNew->data.acReverveTime);
printf("Input the price of ticket:");
scanf("%f", &pstNew->data.fPrice);
printf("Input the discount of ticket(Format 0.0):");
scanf("%s", pstNew->data.acDiscount);
printf("Input the number of tickets:");
scanf("%d", &pstNew->data.iNum);
pstNew->next = NULL;
pstTail -> next = pstNew;
pstTail = pstNew;
iSave = 1;
//有新的航班信息,保存标志为1
}
}
二.查询机票信息 选择航班号查询,目的地,出发地查询,可自行添加其他查询
void Search(PLNode pstPlaneNodeHead)//查询机票信息
{
PlaneNode* pstPlaneNode;
int Sel = 0;
int count = 0;
//计数器
char acFlight[10], acDes[10],acstart[10];
//航班号和目的地
pstPlaneNode = pstPlaneNodeHead->next;
if (pstPlaneNode == NULL)
{
printf("is not have data");
return;
}
//按航班号查询
//按目的地查询
//按起始地查询
printf("Choose one way according to:\n1.flight number2.Dest3.Start\n");
scanf("%d", &Sel);
if (Sel == 1)
{
//按航班号查询
printf("input the right flight number:");
scanf("%s", acFlight);
PrintfHead();
//机票信息头部打印
while (pstPlaneNode != NULL)
{
if (strcmp(pstPlaneNode->data.acFlight, acFlight) == 0)
{
PrintData(pstPlaneNode);
break;
//航班号唯一
}
else
{
pstPlaneNode = pstPlaneNode->next;
}
}
//遍历结束,没有break,则没有用户记录
if (pstPlaneNode == NULL)
{
printf("Sorry,no record!\n");
return;
}
}
else if (Sel == 2)
{
//按目的地查询
printf("input the right Dest:");
scanf("%s", acDes);
PrintfHead();
//机票信息头部打印
while (pstPlaneNode != NULL)
{
if (strcmp(pstPlaneNode->data.acDest, acDes) == 0)
{
PrintData(pstPlaneNode);
count++;
}
pstPlaneNode = pstPlaneNode->next;
}
//遍历结束,计数器为0,则没有记录
if (count ==0)
{
printf("Sorry,no record!\n");
return;
}
}
else if (Sel == 3)
{
//按起始地查询
printf("input the right Start:");
scanf("%s", acstart);
PrintfHead();
//机票信息头部打印
while (pstPlaneNode != NULL)
{
if (strcmp(pstPlaneNode->data.acOrigin, acstart) == 0)
{
PrintData(pstPlaneNode);
count++;
}
pstPlaneNode = pstPlaneNode->next;
}
//遍历结束,计数器为0,则没有记录
if (count == 0)
{
printf("Sorry,no record!\n");
return;
}
}
else
{
printf("Sorry,please input right number 1-3:\n");
return;
}
}
三.订票操作 思路:输入起始地和目的地,显示是否有票若有,多条航班(存入数据到结构体中)订票:输入订票人信息
订票:选择航班号
void Book(MANNode pstManNodeHead, PLNode pstPlaneNodeHead)//订票
{
//思路:输入起始地和目的地,显示是否有票若有,多条航班(存入数据到结构体中)
//订票:输入订票人信息
//订票:选择航班号 PlaneNode * pstPlaneNodeCur,*astPlane[10];
//保存数据临时结构体数组
ManNode* pstManNodeCur,*pstManNodeTemp=NULL;
char acOrigin[10], acDest[10], acID[20], acName[20], acSex[10], acDescision[2], acFlight[10];
int iNum = 0, Record = 0, k = 0, flg = 0;
//接受订票人信息头指针
pstManNodeCur=pstManNodeHead;
while (pstManNodeCur->next != NULL)
{
pstManNodeCur = pstManNodeCur->next;
}//将订票人结构体指向尾巴 //输入起始地和目的地
printf("input the start city:");
scanf("%s",acOrigin);
printf("input the Dest city:");
scanf("%s",acDest);
//查找起始地和目的地,存入临时结构体数组中
pstPlaneNodeCur = pstPlaneNodeHead->next;
while (pstPlaneNodeCur != NULL)
{
if (strcmp(pstPlaneNodeCur->data.acOrigin,acOrigin) == 0&&strcmp(pstPlaneNodeCur->data.acDest,acDest) == 0)
{
astPlane[Record++] = pstPlaneNodeCur;
}
pstPlaneNodeCur = pstPlaneNodeCur->next;
}
//打印数据
printf("there are %d flights you can choose!\n",Record);
PrintfHead();
for (k = 0;
k < Record;
k++)
{
PrintData(astPlane[k]);
}
if (Record == 0)
{
printf("Sorry,no flights you can book\*/\n");
}
//订票
else
{
printf("do you want to book it?(y(Y))/n(N))\n");
scanf("%s", acDescision);
getchar();
if (strcmp(acDescision, "y") == 0 || strcmp(acDescision, "Y") == 0)
{
printf("input your information!\n");
pstManNodeTemp = (ManNode*)malloc(sizeof(ManNode));
printf("input your name:");
scanf("%s", acName);
strcpy(pstManNodeTemp->data.acName, acName);
printf("input your id:");
scanf("%s", acID);
strcpy(pstManNodeTemp->data.acID, acID);
printf("input your sex(M/W):");
scanf("%s", acSex);
strcpy(pstManNodeTemp->data.acSex, acSex);
printf("input your want to book flight number:");
scanf("%s", acFlight);
strcpy(pstManNodeTemp->data.acBookFilight,acFlight);
for (k = 0;
k < Record;
k++)
{
if (strcmp(astPlane[k]->data.acFlight, acFlight) == 0)
{
if (astPlane[k]->data.iNum < 1)
{
printf("no ticket!");
return;
}
printf("remain%dtickets!\n", astPlane[k]->data.iNum);
flg = 1;
break;
}
}
if (flg == 0)
{
printf("error\n");
return;
}printf("Input the booknumber:");
scanf("%d", &iNum);
//订购几张票
astPlane[k]->data.iNum = astPlane[k]->data.iNum - iNum;
pstManNodeTemp->data.iBookNum = iNum;
pstManNodeCur->next = pstManNodeTemp;
pstManNodeTemp->next = NULL;
pstManNodeCur = pstManNodeTemp;
//可以不要
printf("success!\n");
iSave = 1;
}
}
}
四.修改机票信息 为了方便,此处我并未创建新的子菜单,想要更加美观可以自行创建各个函数的子菜单页面
void Modify(PLNode pstPlaneNodeHead)//修改机票信息
{
PlaneNode* pstPlaneNodeCur;
char acFlight[10];
pstPlaneNodeCur = pstPlaneNodeHead->next;
if (pstPlaneNodeCur == NULL)
{
printf("no flight to be modify!");
return;
}
else
{
printf("Input the flight number you want to modify!:");
scanf("%s", acFlight);
while (pstPlaneNodeCur)
{
if (strcmp(pstPlaneNodeCur->data.acFlight, acFlight) == 0)
{
break;
}
else
{
pstPlaneNodeCur = pstPlaneNodeCur->next;
}
}
if (pstPlaneNodeCur)
{
printf("input the new start city:");
scanf("%s", pstPlaneNodeCur->data.acOrigin);
printf("input the new arrval city:");
scanf("%s", pstPlaneNodeCur->data.acDest);
printf("input the new dicount:");
scanf("%s", pstPlaneNodeCur->data.acDiscount);
printf("input the new acTakeOffTime:");
scanf("%s", pstPlaneNodeCur->data.acTakeOffTime);
printf("input the new acReverveTime:");
scanf("%s", pstPlaneNodeCur->data.acReverveTime);
printf("input the new fPrice:");
scanf("%f", &pstPlaneNodeCur->data.fPrice);
printf("input the new iNum:");
scanf("%d", &pstPlaneNodeCur->data.iNum);
printf("modify successful!\n");
iSave = 1;
//此处可以封装一个函数,让这块代码不这么冗杂}
}
}
五.显示机票信息
void PrintfHead()//打印机票头部信息
{
printf("|------------------------------------------------------------------------------|\n");
printf("|***********************************************************************|******|\n");
printf("|Flight|StartCity|DseCity|DepertureTime|ArrivalTime|***price***|Discount|number|\n");
printf("|******|*********|*******|*************|***********|***********|********|******|\n");
printf("|------------------------------------------------------------------------------|\n");
}
void PrintData(PLNode pstPlaneNodeCur)
{
PlaneNode* pst = pstPlaneNodeCur;
printf(" %3s%8s%8s%12s%12s%16f%7s%8d\n", pst->data.acFlight, pst->data.acOrigin, pst->data.acDest, pst->data.acTakeOffTime,
pst->data.acReverveTime, pst->data.fPrice, pst->data.acDiscount, pst->data.iNum);
}
void Show(PLNode pstPlaneNodeHead)//显示所有机票信息
{
PlaneNode* pstPlaneNodeCur = pstPlaneNodeHead->next;
PrintfHead();
if (pstPlaneNodeHead->next == NULL)
{
printf("no flight ticket!\n");
}
else
{
while (pstPlaneNodeCur != NULL)
{
PrintData(pstPlaneNodeCur);
pstPlaneNodeCur = pstPlaneNodeCur->next;
}
}
}
六.推荐机票信息 根据用户输入的出发地,目的地以及用户可以乘坐最早的时间(系统会给出这个时间后所有符合信息的机票信息)
void Recommed(PLNode pstPlaneNodeHead)//推荐机票信息
{
PlaneNode* pstPlaneNodeCur;
char acDest[10], acTime[10],acOrigin[10];
int iNum=0;
pstPlaneNodeCur = pstPlaneNodeHead->next;
printf("input your take city:");
scanf("%s", acOrigin);
printf("input your destination city:");
scanf("%s", acDest);
printf("input the earlist time you can take:");
scanf("%s", acTime);
PrintfHead();
while (pstPlaneNodeCur != NULL)
{
if (strcmp(pstPlaneNodeCur->data.acOrigin, acOrigin) == 0 && strcmp(pstPlaneNodeCur->data.acDest, acDest) == 0&& strcmp(acTime, pstPlaneNodeCur->data.acTakeOffTime) < 0)
{
PrintData(pstPlaneNodeCur);
iNum++;
}
pstPlaneNodeCur = pstPlaneNodeCur->next;
}
printf("there are %d flights you can choose!\n", iNum);
if (iNum != 0)
{
printf("please choose 3rd operation to book it!\n");
}
}
七.退票操作 输入订票人身份证号,显示相应的订票信息,选择退票
//找到id号相同的订票人记录,返回订票人节点
MANNode FindMan(MANNode pstManNodeHead, char acID[20])
{
ManNode* pstManNodeCur = pstManNodeHead->next;
while (pstManNodeCur)
{
if (strcmp(pstManNodeCur->data.acID, acID) == 0)
{
return pstManNodeCur;
}
pstManNodeCur = pstManNodeCur->next;
}
return NULL;
}
//找到订票人信息中的航班号记录,找到相应机票信息节点
PLNode FindPlane(PLNode pstPlaneNodeHead, char* acBookFlight)
{
PlaneNode* pstPlaneNodeCur = pstPlaneNodeHead->next;
while (pstPlaneNodeCur)
{
if (strcmp(pstPlaneNodeCur->data.acFlight, acBookFlight) == 0)
{
return pstPlaneNodeCur;
}
pstPlaneNodeCur = pstPlaneNodeCur->next;
}
return NULL;
}
void Refund(MANNode pstManNodeHead, PLNode pstPlaneNodeHead)//退票
{
ManNode* pstManNodeCur, * pstManNodeFind = NULL;
PlaneNode* pstPlaneNodeFind = NULL;
char acID[20],acdescison[2];
int iNum;
//剩余票数
int iBooknum;
//定了几个票
printf("input your id number :");
scanf("%s", acID);
//找到订票人的结构体
pstManNodeFind = FindMan(pstManNodeHead, acID);
if (pstManNodeFind == NULL)
{
printf("can't find!\n");
}
else//退票
{
printf("this is your tickets:\n");
printf("id number is :%s\n", pstManNodeFind->data.acID);
printf("name is :%s\n", pstManNodeFind->data.acName);
printf("sex is :%s\n", pstManNodeFind->data.acSex);
printf("book flights :%s\n", pstManNodeFind->data.acBookFilight);
printf("book number :%d\n", pstManNodeFind->data.iBookNum);
printf("if you want to cancel it (y/n)?: ");
scanf("%s", acdescison);
if (strcmp(acdescison, "y") == 0 || strcmp(acdescison, "Y") == 0)
{
//找到前驱
pstManNodeCur = pstManNodeHead;
while (pstManNodeCur->next != pstManNodeFind)
{
pstManNodeCur = pstManNodeCur->next;
}//找到该订票人对应航班记录
pstPlaneNodeFind =FindPlane(pstPlaneNodeHead, pstManNodeFind->data.acBookFilight);
//退票
if (pstPlaneNodeFind != NULL)
{
iNum = pstPlaneNodeFind->data.iNum;
//机票剩余票数
iBooknum = pstManNodeFind->data.iBookNum;
//订购人定票数
pstPlaneNodeFind->data.iNum = iNum + iBooknum;
}
//删除订票人节点
pstManNodeCur->next = pstManNodeFind->next;
free(pstManNodeFind);
printf("successful refund!\n");
iSave = 1;
}
}
}
八.显示当前时间
void NowTime()//显示当前时间
{
time_t It;
It = time(NULL);
printf("现在的时间是:%s ", ctime(&It));
}
九.保存机票信息,订票人信息 如果没有在操作界面选择保存信息,在退出系统时,主函数调用该函数,问用户是否选择保存刚刚录入内存的信息
void SaveMan(MANNode pstManNodeHead)//保存订票人信息
{
FILE* pfMan;
ManNode* pstManNodeCur;
int count = 0;
//保存多少条信息
int iflg = 1;
//是否写入信息
pfMan = fopen("man.txt", "wb");
if (pfMan == NULL)
{
printf("the file can't be opened!");
return;
}
pstManNodeCur = pstManNodeHead->next;
while (pstManNodeCur != NULL)
{
if (fwrite(pstManNodeCur, sizeof(ManNode), 1, pfMan) == 1)
{
pstManNodeCur = pstManNodeCur->next;
count++;
}
else
{
iflg = 0;
break;
}
}
fclose(pfMan);
}
void SavePlane(PLNode pstPlaneNodeHead)//保存机票信息
{
FILE* pfPlane;
//机票文件信息
PlaneNode* pstPlaneNodeCur;
int count = 0;
//保存多少条信息
int iflg = 1;
//是否写入信息
pfPlane = fopen("plane.txt", "wb");
if (pfPlane == NULL)
{
printf("the file can't be opened!");
return;
}
pstPlaneNodeCur = pstPlaneNodeHead->next;
while (pstPlaneNodeCur != NULL)
{
if (fwrite(pstPlaneNodeCur,sizeof(PlaneNode) ,1, pfPlane)==1)
{
pstPlaneNodeCur = pstPlaneNodeCur->next;
count++;
}
else
{
iflg = 0;
break;
}
}
if (iflg)
{
printf("you have save %d flights", count);
iSave = 0;
}
fclose(pfPlane);
}
文章图片
觉得前面太啰嗦,完整代码在这里:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#includestruct AirPlane
{
char acFlight[10];
//航班号
char acOrigin[10];
//出发地
char acDest[10];
//目的地
char acTakeOffTime[10];
//起飞时间
char acReverveTime[10];
//降落时间
float fPrice;
//票价
char acDiscount[4];
//折扣
int iNum;
//票数
};
struct Man
{
char acName[20];
//姓名
char acID[20];
//身份证号
char acSex[10];
//性别
int iBookNum;
//购票数量
char acBookFilight[10];
//订购的航班号
};
//定义机票信息结点的结构体
typedef struct PlaneNode
{
struct AirPlane data;
struct PlaneNode* next;
}PlaneNode, * PLNode;
//定义订票人信息结点的结构体
typedef struct ManNode
{
struct Man data;
struct ManNode* next;
}ManNode, * MANNode;
int iSave = 0;
//作为数据是否改动的标志int Init(PLNode pstPlaneNodeHead, MANNode pstManNodeHead)//将文件的数据加载到内存中
{
//先加载飞机机票信息
FILE* pfplane;
//定义飞机机票文件指针
PlaneNode* pstPlaneNodeTemp, * pstPlaneNodeCur;
pstPlaneNodeCur = pstPlaneNodeHead;
pstPlaneNodeTemp = NULL;
pfplane = fopen("plane.txt", "ab+");
if (pfplane == NULL)
{
printf("Can't open plane.txt!\n");
return -1;
}
else
{
//把文件数据读入链表中
while (!feof(pfplane))
{
pstPlaneNodeTemp = (PlaneNode*)malloc(sizeof(PlaneNode));
//申请临时结点
if (fread(pstPlaneNodeTemp, sizeof(PlaneNode), 1, pfplane) == 1)
{
pstPlaneNodeTemp->next = NULL;
pstPlaneNodeCur->next = pstPlaneNodeTemp;
pstPlaneNodeCur = pstPlaneNodeTemp;
}
}
free(pstPlaneNodeTemp);
fclose(pfplane);
}
//2.再加载订票人信息
FILE* pfman;
//定义文件指针
ManNode* pstManNodeTemp, * pstManNodeCur;
pstManNodeCur = pstManNodeHead;
pstManNodeTemp = NULL;
pfman = fopen("man.txt", "ab+");
if (pfman == NULL)
{
printf("Can't open man.txt!\n");
return -1;
}
else
{
//把文件数据读入链表中
while (!feof(pfman))
{
pstManNodeTemp = (ManNode*)malloc(sizeof(ManNode));
//申请临时结点
if (fread(pstManNodeTemp, sizeof(ManNode), 1, pfman) == 1)
{
pstManNodeTemp->next = NULL;
pstManNodeCur->next = pstManNodeTemp;
pstManNodeCur = pstManNodeTemp;
}
}
free(pstManNodeTemp);
fclose(pfman);
}
return 0;
}
void Insert(PLNode pstPlaneNodeHead)//添加机票信息
{
PlaneNode* pstHead, * pstTail, * pstCur, * pstNew;
char acFlight[10];
//保存航班号
pstHead = pstTail = pstPlaneNodeHead;
//找尾巴
//让pstTail指向最后一个结点
while (pstTail->next != NULL)
{
pstTail = pstTail->next;
}
while (1)
{
printf("Input the new flight number(-1 to end):");
scanf("%s", acFlight);
getchar();
if (strcmp(acFlight, "-1") == 0)
{
break;
}
//判断航班号是否重复
pstCur = pstPlaneNodeHead->next;
while (pstCur != NULL)
{
if (strcmp(pstCur->data.acFlight, acFlight) == 0)
{
printf("this flight %s exists!\n", acFlight);
return;
}
pstCur = pstCur->next;
}
//如果航班号没有和现有纪录重复,则新建一个链表结点
pstNew = (PlaneNode*)malloc(sizeof(PlaneNode));
strcpy(pstNew->data.acFlight, acFlight);
printf("Input the Start City:");
scanf("%s", pstNew->data.acOrigin);
printf("Input the Dest City:");
scanf("%s", pstNew->data.acDest);
printf("Input the Departure time (Format 00:00):");
scanf("%s", pstNew->data.acTakeOffTime);
printf("Input the Arrival time (Format 00:00):");
scanf("%s", pstNew->data.acReverveTime);
printf("Input the price of ticket:");
scanf("%f", &pstNew->data.fPrice);
printf("Input the discount of ticket(Format 0.0):");
scanf("%s", pstNew->data.acDiscount);
printf("Input the number of tickets:");
scanf("%d", &pstNew->data.iNum);
pstNew->next = NULL;
pstTail -> next = pstNew;
pstTail = pstNew;
iSave = 1;
//有新的航班信息,保存标志为1
}
}
void Menu()
{
puts("***********************************************************");
puts("****** Welcome to the airplane tickets booking system *****");
puts("-----------------------------------------------------------");
puts("******Choose the following operations(0-9)*****");
puts("-----------------------------------------------------------");
puts("**1.Insert fligths2.Search fligths**");
puts("**3.Book fligths4.Modify fligths**");
puts("**5.Show fligths6.Recommed fligths**");
puts("**7.Refund fligths8.NowTime fligths**");
puts("**9.Save Messages0.Exist**");
puts("-----------------------------------------------------------");
}
void SaveMan(MANNode pstManNodeHead)//保存订票人信息
{
FILE* pfMan;
ManNode* pstManNodeCur;
int count = 0;
//保存多少条信息
int iflg = 1;
//是否写入信息
pfMan = fopen("man.txt", "wb");
if (pfMan == NULL)
{
printf("the file can't be opened!");
return;
}
pstManNodeCur = pstManNodeHead->next;
while (pstManNodeCur != NULL)
{
if (fwrite(pstManNodeCur, sizeof(ManNode), 1, pfMan) == 1)
{
pstManNodeCur = pstManNodeCur->next;
count++;
}
else
{
iflg = 0;
break;
}
}
fclose(pfMan);
}
void SavePlane(PLNode pstPlaneNodeHead)//保存机票信息
{
FILE* pfPlane;
//机票文件信息
PlaneNode* pstPlaneNodeCur;
int count = 0;
//保存多少条信息
int iflg = 1;
//是否写入信息
pfPlane = fopen("plane.txt", "wb");
if (pfPlane == NULL)
{
printf("the file can't be opened!");
return;
}
pstPlaneNodeCur = pstPlaneNodeHead->next;
while (pstPlaneNodeCur != NULL)
{
if (fwrite(pstPlaneNodeCur,sizeof(PlaneNode) ,1, pfPlane)==1)
{
pstPlaneNodeCur = pstPlaneNodeCur->next;
count++;
}
else
{
iflg = 0;
break;
}
}
if (iflg)
{
printf("you have save %d flights", count);
iSave = 0;
}
fclose(pfPlane);
}
void PrintfHead()
{
printf("|------------------------------------------------------------------------------|\n");
printf("|***********************************************************************|******|\n");
printf("|Flight|StartCity|DseCity|DepertureTime|ArrivalTime|***price***|Discount|number|\n");
printf("|******|*********|*******|*************|***********|***********|********|******|\n");
printf("|------------------------------------------------------------------------------|\n");
}
void PrintData(PLNode pstPlaneNodeCur)
{
PlaneNode* pst = pstPlaneNodeCur;
printf(" %3s%8s%8s%12s%12s%16f%7s%8d\n", pst->data.acFlight, pst->data.acOrigin, pst->data.acDest, pst->data.acTakeOffTime,
pst->data.acReverveTime, pst->data.fPrice, pst->data.acDiscount, pst->data.iNum);
}
void Show(PLNode pstPlaneNodeHead)//显示所有机票信息
{
PlaneNode* pstPlaneNodeCur = pstPlaneNodeHead->next;
PrintfHead();
if (pstPlaneNodeHead->next == NULL)
{
printf("no flight ticket!\n");
}
else
{
while (pstPlaneNodeCur != NULL)
{
PrintData(pstPlaneNodeCur);
pstPlaneNodeCur = pstPlaneNodeCur->next;
}
}
}
void Search(PLNode pstPlaneNodeHead)//查询机票信息
{
PlaneNode* pstPlaneNode;
int Sel = 0;
int count = 0;
//计数器
char acFlight[10], acDes[10],acstart[10];
//航班号和目的地
pstPlaneNode = pstPlaneNodeHead->next;
if (pstPlaneNode == NULL)
{
printf("is not have data");
return;
}
//按航班号查询
//按目的地查询
//按起始地查询
printf("Choose one way according to:\n1.flight number2.Dest3.Start\n");
scanf("%d", &Sel);
if (Sel == 1)
{
//按航班号查询
printf("input the right flight number:");
scanf("%s", acFlight);
PrintfHead();
//机票信息头部打印
while (pstPlaneNode != NULL)
{
if (strcmp(pstPlaneNode->data.acFlight, acFlight) == 0)
{
PrintData(pstPlaneNode);
break;
//航班号唯一
}
else
{
pstPlaneNode = pstPlaneNode->next;
}
}
//遍历结束,没有break,则没有用户记录
if (pstPlaneNode == NULL)
{
printf("Sorry,no record!\n");
return;
}
}
else if (Sel == 2)
{
//按目的地查询
printf("input the right Dest:");
scanf("%s", acDes);
PrintfHead();
//机票信息头部打印
while (pstPlaneNode != NULL)
{
if (strcmp(pstPlaneNode->data.acDest, acDes) == 0)
{
PrintData(pstPlaneNode);
count++;
}
pstPlaneNode = pstPlaneNode->next;
}
//遍历结束,计数器为0,则没有记录
if (count ==0)
{
printf("Sorry,no record!\n");
return;
}
}
else if (Sel == 3)
{
//按起始地查询
printf("input the right Start:");
scanf("%s", acstart);
PrintfHead();
//机票信息头部打印
while (pstPlaneNode != NULL)
{
if (strcmp(pstPlaneNode->data.acOrigin, acstart) == 0)
{
PrintData(pstPlaneNode);
count++;
}
pstPlaneNode = pstPlaneNode->next;
}
//遍历结束,计数器为0,则没有记录
if (count == 0)
{
printf("Sorry,no record!\n");
return;
}
}
else
{
printf("Sorry,please input right number 1-3:\n");
return;
}
}void Book(MANNode pstManNodeHead, PLNode pstPlaneNodeHead)//订票
{
//思路:输入起始地和目的地,显示是否有票若有,多条航班(存入数据到结构体中)
//订票:输入订票人信息
//订票:选择航班号 PlaneNode * pstPlaneNodeCur,*astPlane[10];
//保存数据临时结构体数组
ManNode* pstManNodeCur,*pstManNodeTemp=NULL;
char acOrigin[10], acDest[10], acID[20], acName[20], acSex[10], acDescision[2], acFlight[10];
int iNum = 0, Record = 0, k = 0, flg = 0;
//接受订票人信息头指针
pstManNodeCur=pstManNodeHead;
while (pstManNodeCur->next != NULL)
{
pstManNodeCur = pstManNodeCur->next;
}//将订票人结构体指向尾巴 //输入起始地和目的地
printf("input the start city:");
scanf("%s",acOrigin);
printf("input the Dest city:");
scanf("%s",acDest);
//查找起始地和目的地,存入临时结构体数组中
pstPlaneNodeCur = pstPlaneNodeHead->next;
while (pstPlaneNodeCur != NULL)
{
if (strcmp(pstPlaneNodeCur->data.acOrigin,acOrigin) == 0&&strcmp(pstPlaneNodeCur->data.acDest,acDest) == 0)
{
astPlane[Record++] = pstPlaneNodeCur;
}
pstPlaneNodeCur = pstPlaneNodeCur->next;
}
//打印数据
printf("there are %d flights you can choose!\n",Record);
PrintfHead();
for (k = 0;
k < Record;
k++)
{
PrintData(astPlane[k]);
}
if (Record == 0)
{
printf("Sorry,no flights you can book\*/\n");
}
//订票
else
{
printf("do you want to book it?(y(Y))/n(N))\n");
scanf("%s", acDescision);
getchar();
if (strcmp(acDescision, "y") == 0 || strcmp(acDescision, "Y") == 0)
{
printf("input your information!\n");
pstManNodeTemp = (ManNode*)malloc(sizeof(ManNode));
printf("input your name:");
scanf("%s", acName);
strcpy(pstManNodeTemp->data.acName, acName);
printf("input your id:");
scanf("%s", acID);
strcpy(pstManNodeTemp->data.acID, acID);
printf("input your sex(M/W):");
scanf("%s", acSex);
strcpy(pstManNodeTemp->data.acSex, acSex);
printf("input your want to book flight number:");
scanf("%s", acFlight);
strcpy(pstManNodeTemp->data.acBookFilight,acFlight);
for (k = 0;
k < Record;
k++)
{
if (strcmp(astPlane[k]->data.acFlight, acFlight) == 0)
{
if (astPlane[k]->data.iNum < 1)
{
printf("no ticket!");
return;
}
printf("remain%dtickets!\n", astPlane[k]->data.iNum);
flg = 1;
break;
}
}
if (flg == 0)
{
printf("error\n");
return;
}printf("Input the booknumber:");
scanf("%d", &iNum);
//订购几张票
astPlane[k]->data.iNum = astPlane[k]->data.iNum - iNum;
pstManNodeTemp->data.iBookNum = iNum;
pstManNodeCur->next = pstManNodeTemp;
pstManNodeTemp->next = NULL;
pstManNodeCur = pstManNodeTemp;
//可以不要
printf("success!\n");
iSave = 1;
}
}
}
void Modify(PLNode pstPlaneNodeHead)//修改机票信息
{
PlaneNode* pstPlaneNodeCur;
char acFlight[10];
pstPlaneNodeCur = pstPlaneNodeHead->next;
if (pstPlaneNodeCur == NULL)
{
printf("no flight to be modify!");
return;
}
else
{
printf("Input the flight number you want to modify!:");
scanf("%s", acFlight);
while (pstPlaneNodeCur)
{
if (strcmp(pstPlaneNodeCur->data.acFlight, acFlight) == 0)
{
break;
}
else
{
pstPlaneNodeCur = pstPlaneNodeCur->next;
}
}
if (pstPlaneNodeCur)
{
printf("input the new start city:");
scanf("%s", pstPlaneNodeCur->data.acOrigin);
printf("input the new arrval city:");
scanf("%s", pstPlaneNodeCur->data.acDest);
printf("input the new dicount:");
scanf("%s", pstPlaneNodeCur->data.acDiscount);
printf("input the new acTakeOffTime:");
scanf("%s", pstPlaneNodeCur->data.acTakeOffTime);
printf("input the new acReverveTime:");
scanf("%s", pstPlaneNodeCur->data.acReverveTime);
printf("input the new fPrice:");
scanf("%f", &pstPlaneNodeCur->data.fPrice);
printf("input the new iNum:");
scanf("%d", &pstPlaneNodeCur->data.iNum);
printf("modify successful!\n");
iSave = 1;
}
}
}
void Recommed(PLNode pstPlaneNodeHead)//推荐机票信息
{
PlaneNode* pstPlaneNodeCur;
char acDest[10], acTime[10],acOrigin[10];
int iNum=0;
pstPlaneNodeCur = pstPlaneNodeHead->next;
printf("input your take city:");
scanf("%s", acOrigin);
printf("input your destination city:");
scanf("%s", acDest);
printf("input the earlist time you can take:");
scanf("%s", acTime);
PrintfHead();
while (pstPlaneNodeCur != NULL)
{
if (strcmp(pstPlaneNodeCur->data.acOrigin, acOrigin) == 0 && strcmp(pstPlaneNodeCur->data.acDest, acDest) == 0&& strcmp(acTime, pstPlaneNodeCur->data.acTakeOffTime) < 0)
{
PrintData(pstPlaneNodeCur);
iNum++;
}
pstPlaneNodeCur = pstPlaneNodeCur->next;
}
printf("there are %d flights you can choose!\n", iNum);
if (iNum != 0)
{
printf("please choose 3rd operation to book it!\n");
}
}
//找到id号相同的订票人记录,返回订票人节点
MANNode FindMan(MANNode pstManNodeHead, char acID[20])
{
ManNode* pstManNodeCur = pstManNodeHead->next;
while (pstManNodeCur)
{
if (strcmp(pstManNodeCur->data.acID, acID) == 0)
{
return pstManNodeCur;
}
pstManNodeCur = pstManNodeCur->next;
}
return NULL;
}
//找到订票人信息中的航班号记录,找到相应机票信息节点
PLNode FindPlane(PLNode pstPlaneNodeHead, char* acBookFlight)
{
PlaneNode* pstPlaneNodeCur = pstPlaneNodeHead->next;
while (pstPlaneNodeCur)
{
if (strcmp(pstPlaneNodeCur->data.acFlight, acBookFlight) == 0)
{
return pstPlaneNodeCur;
}
pstPlaneNodeCur = pstPlaneNodeCur->next;
}
return NULL;
}
void Refund(MANNode pstManNodeHead, PLNode pstPlaneNodeHead)//退票
{
ManNode* pstManNodeCur, * pstManNodeFind = NULL;
PlaneNode* pstPlaneNodeFind = NULL;
char acID[20],acdescison[2];
int iNum;
//剩余票数
int iBooknum;
//定了几个票
printf("input your id number :");
scanf("%s", acID);
//找到订票人的结构体
pstManNodeFind = FindMan(pstManNodeHead, acID);
if (pstManNodeFind == NULL)
{
printf("can't find!\n");
}
else//退票
{
printf("this is your tickets:\n");
printf("id number is :%s\n", pstManNodeFind->data.acID);
printf("name is :%s\n", pstManNodeFind->data.acName);
printf("sex is :%s\n", pstManNodeFind->data.acSex);
printf("book flights :%s\n", pstManNodeFind->data.acBookFilight);
printf("book number :%d\n", pstManNodeFind->data.iBookNum);
printf("if you want to cancel it (y/n)?: ");
scanf("%s", acdescison);
if (strcmp(acdescison, "y") == 0 || strcmp(acdescison, "Y") == 0)
{
//找到前驱
pstManNodeCur = pstManNodeHead;
while (pstManNodeCur->next != pstManNodeFind)
{
pstManNodeCur = pstManNodeCur->next;
}//找到该订票人对应航班记录
pstPlaneNodeFind =FindPlane(pstPlaneNodeHead, pstManNodeFind->data.acBookFilight);
//退票
if (pstPlaneNodeFind != NULL)
{
iNum = pstPlaneNodeFind->data.iNum;
//机票剩余票数
iBooknum = pstManNodeFind->data.iBookNum;
//订购人定票数
pstPlaneNodeFind->data.iNum = iNum + iBooknum;
}
//删除订票人节点
pstManNodeCur->next = pstManNodeFind->next;
free(pstManNodeFind);
printf("successful refund!\n");
iSave = 1;
}
}
}
void NowTime()//显示当前时间
{
time_t It;
It = time(NULL);
printf("现在的时间是:%s ", ctime(&It));
}
int main()
{
PlaneNode pstPlaneNodeHead;
//机票信息头结点
pstPlaneNodeHead.next = NULL;
ManNode pstManNodeHead;
//订票人头结点
pstManNodeHead.next = NULL;
int res;
res = Init(&pstPlaneNodeHead, &pstManNodeHead);
//将文件的数据加载到内存中
if (res < 0)
{
return 0;
}
int iSel;
//用于接收用户对于功能的选择
char c1;
//是否保存
while(1)
{
system("cls");
Menu();
printf("Input 0-9 operations:");
scanf("%d", &iSel);
getchar();
system("cls");
switch(iSel)
{
case 1:
Insert(&pstPlaneNodeHead);
//添加机票信息
break;
case 2:
Search(&pstPlaneNodeHead);
//查询机票信息
break;
case 3:
Book(&pstManNodeHead, &pstPlaneNodeHead);
//预定机票系统
break;
case 4:
Modify(&pstPlaneNodeHead);
//修改机票信息
break;
case 5:
Show(&pstPlaneNodeHead);
//显示所有机票信息
break;
case 6:
Recommed(&pstPlaneNodeHead);
//推荐机票信息
break;
case 7:
Refund(&pstManNodeHead, &pstPlaneNodeHead);
//退票操作
break;
case 8:
NowTime();
//显示当前时间
break;
case 9:
SaveMan(&pstManNodeHead);
//保存订票人信息
SavePlane(&pstPlaneNodeHead);
//保存机票信息
case 0:
if (iSave == 1)
{
printf("Do you want to save message (y/n)");
scanf("%c", &c1);
getchar();
if (c1 == 'y' || c1 == 'Y')
{
SaveMan(&pstManNodeHead);
//保存订票人的信息
SavePlane(&pstPlaneNodeHead);
//保存机票信息
}
}
//Destroy(pstManNodeHead,pstPlaneNodeHead);
return 0;
}
printf("please press any key continue...");
_getch();
}
//Destroy(pstManNodeHead,pstPlaneNodeHead);
return 0;
}
动图操作演示 (视频过长压缩有限)
如有错误还望指正!
【数据结构|课程设计(飞机订票系统) 超全】
推荐阅读
- 程序员|编程详谈(程序员真的是高薪吗(那些你不了解的内幕))
- C/C++预处理浅析使用形式
- 项目|基于springboot的特产购物系统
- 一本通oj|一本通OJ 1376
- 面试|【美团面经】测试开发一面
- 数据结构和算法|数据结构和算法基础(6)——常用十种算法
- 数据结构和算法|数据结构和算法基础(2)——递归
- 链表|[java刷算法]牛客—剑指offer链表有环的入口、反转链表、合并排序链表
- 链表|手写一个泛型双向链表