飞机订票系统

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<time.h>
#define HEAD1 "*****************************************************\n"
#define HEAD2  "|Flight|StartCity|DestCity|DepertureTime|ArrivalTime|  price  |number|\n"
#define HEAD3  "|------|---------|--------|-------------|-----------|---------|------|\n"

#define FORMAT  "%-9s%-9s%-10s%-14s%-10s%-5.2f%7d\n"
#define DATA  pst->stData.acFlight,pst->stData.acOrigin,pst->stData.acDest,pst->stData.acTakeOffTime,pst->stData.acReverveTime,pst->stData.fPrice,pst->stData.iNum

typedef struct Airplane//飞机信息
{
	char acFlight[10];//航班号
	char acOrigin[10];//出发地
	char acDest[10];//目的地
	char acTakeOffTime[10];//起飞时间
	char acReverveTime[10];//降落时间
	float fPrice;//票价
	char acDiscount[4];//折扣
	int iNum;//剩余票数
}Airplane;
typedef struct Man//顾客信息
{
	char acName[20];//名字
	char acID[20];//身份证号码;
	char  acSex[10];//性别
	int  iBookNum;//购票数量
	char acBookFlight[10];//订购航班号
}Man;
//定义机票信息节点的结构体
typedef struct PlaneNode
{
	Airplane stData;
	PlaneNode* pstNext;
}PlaneNode;
//定义订票人信息节点 的结构体
typedef struct ManNode 
{
	Man stData;
	ManNode* pstNext;
}ManNode;
//定义一个判断数据是否修改的标志
int iSave = 0;

int  Init(PlaneNode* pstPlaneNodeHead, ManNode* pstManNodeHead)
{
	//1.加载飞机机票信息
	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(struct PlaneNode), 1, pfPlane) == 1)
			{
				pstPlaneNodeTemp->pstNext = NULL;
				pstPlaneNodeCur->pstNext = 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 = (struct ManNode*)malloc(sizeof(struct ManNode));
			if (fread(pstManNodeTemp, sizeof(struct ManNode), 1, pfMan) == 1)
			{
				pstManNodeTemp->pstNext = NULL;
				pstManNodeCur->pstNext = pstManNodeTemp;
				pstManNodeCur = pstManNodeTemp;
			}
		}
		free(pstManNodeTemp);
		fclose(pfMan);
	}
	return 0;
}

void Menu()
{
	puts("****************************************************************************");
	puts("*               Welcome to the airplane   tickets  booking sytem           *");
	puts("----------------------------------------------------------------------------");
	puts("*                choose  the  following   operation(0-9);                  *");
	puts("----------------------------------------------------------------------------");
	puts("* 1.Insert  flights                              2.Search  flights         *");
	puts("* 3.Book  tickets                                4.Modiy  flight  data     *");
	puts("* 5.Show  fligthts                               6.Recommend   flights     *");
	puts("* 7.Refund  tickets                              8.Show  current  time     *");
	puts("* 9.Save  to  files                              0.quit                    *");
	puts("****************************************************************************");

}
void Insert(PlaneNode* pstPlaneNodeHead)//插入航班信息
{
	PlaneNode* pstHead, * pstTail, * pstCur, * pstNew;
	char acFlight[10];//保存航班号
	pstHead =pstTail= pstPlaneNodeHead;
	//让pstTail指向最后一个节点
	while (pstTail->pstNext!=NULL)
	{
		pstTail = pstTail->pstNext;

	}
	while (1)
	{
		printf("Input  the new  flight  number(-1  to end):");
		scanf("%s", acFlight);
		getchar();
		if (strcmp(acFlight, "-1") == 0)
		{
			break;
		}
		//航班号唯一
		pstCur = pstPlaneNodeHead->pstNext;
		while (pstCur!=NULL)
		{
			if (strcmp(pstCur->stData.acFlight, acFlight) == 0)
			{
				printf("this   flight   %s  exists!\n", acFlight);
				return;
			}
			pstCur = pstCur->pstNext;
		}
		//如果航班号没有和现有记录航班号重复,则新建一个链表节点
		pstNew = (PlaneNode*)malloc(sizeof(PlaneNode));
		strcpy(pstNew->stData.acFlight, acFlight);//航班号
		printf("Input the  Start  City:\n");
		scanf("%s", pstNew->stData.acOrigin);
		printf("Input the Dest  City:\n");
		scanf("%s", pstNew->stData.acDest);
		printf("Input the Departure time(Format  00:00):\n");
		scanf("%s", pstNew->stData.acTakeOffTime);
		printf("Input the  Arrival time(Format 00:00):\n");
		scanf("%s", pstNew->stData.acReverveTime);
		printf("Input the price of ticket :\n");
		scanf("%f", &pstNew->stData.fPrice);
		printf("Input the discount(Format(0.0):\n");
		scanf("%s", pstNew->stData.acDiscount);
		printf("Input the number  of the tickets:\n");
		scanf("%d", &pstNew->stData.iNum);

		pstNew->pstNext = NULL;
		pstTail->pstNext = pstNew;
		pstTail = pstNew;

		//有新的航班信息,保存标志置为1.若退出需要提示是否保存信息(见主函数)
		iSave = 1;//数据变化
	}
}

void SavePlane(PlaneNode* pstPlaneNodeHead)//保存机票信息
{
	FILE* pfplane;//机票的文件指针
	PlaneNode *pstPlaneNodeCur;
	int icount = 0;//保存多少条信息
	int iFlag=1;//是否写进信息
	pfplane = fopen("plane.txt", "wb");
	if (pfplane == NULL)
	{
		printf("the  file  can't  be opened!\n");
		return;
	}
	pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	while (pstPlaneNodeCur!=NULL)
	{
		if (fwrite(pstPlaneNodeCur, sizeof(PlaneNode), 1, pfplane) == 1)
		{
			pstPlaneNodeCur = pstPlaneNodeCur->pstNext;
			icount++;
		}
		else
		{
			iFlag = 0;
			break;
		}
	}
	if (iFlag)
	{
		printf("you have   save %d   flights \n", icount);
		iSave = 0;
	}
	fclose(pfplane);
}

void SaveMan(ManNode* pstManNodeHead)//保存购票人信息
{
	FILE* pfMan;
	ManNode* pstManNodeCur;
	int icount = 0;
	int iFlag = 1;
	pfMan = fopen("man.txt", "wb");
	if (pfMan == NULL)
	{
		printf("the file can't be opened!\n");
		return;
	}
	pstManNodeCur = pstManNodeHead->pstNext;
	while (pstManNodeCur != NULL)
	{
		if (fwrite(pstManNodeCur, sizeof(ManNode), 1, pfMan) == 1)
		{
			pstManNodeCur = pstManNodeCur->pstNext;
			icount++;
		}
		else
		{
			iFlag = 0;
			break;
		}
	}

	fclose(pfMan);
}

void PrintHead()
{
	printf(HEAD1);
	printf(HEAD2);
	printf(HEAD3);
}

void PrintData(PlaneNode* pstPlaneNodeCur)
{
	PlaneNode* pst = pstPlaneNodeCur;
	printf(FORMAT ,DATA);
}

void Show(PlaneNode * pstPlaneNodeHead)
{
	PlaneNode* pstPlaneNodeCur;
	pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	PrintHead();
	if (pstPlaneNodeHead->pstNext == NULL)
	{
		printf("no flight ticket!\n");//没有航班号
	}
	else
	{
		while (pstPlaneNodeCur!=NULL)
		{
			PrintData(pstPlaneNodeCur);
			pstPlaneNodeCur = pstPlaneNodeCur->pstNext;
		}
	}

}

void Search(PlaneNode* pstPlaneNodeHead)
{
	PlaneNode* psPlaneNode;
	int iSel = 0;
	int iCount = 0;
	char acFlight[10], acDest[10];
	psPlaneNode = pstPlaneNodeHead->pstNext;
	if (psPlaneNode == NULL)
	{
		printf("No Flight record");
		return;
	}
	while (1){
		//system("cls");
	printf("Choose one way according to: \n1.flight number,2Dest:\n");//1.按航班号,2.目的地查询
	scanf("%d", &iSel);
	if (iSel == 1)
	{
		printf("Input the flight  number:\n");
		scanf("%s", acFlight);
		PrintHead();
		while (psPlaneNode != NULL)
		{
			if (strcmp(psPlaneNode->stData.acFlight, acFlight) == 0)
			{
				PrintData(psPlaneNode);
				break;//航班号唯一,只要找到一个就退出
			}
			else
			{
				psPlaneNode = psPlaneNode->pstNext;
			}
		}//遍历完一遍,没有中途break,则没有用户记录
		if (psPlaneNode == NULL)
		{
			printf("Sorry ,no record!\n");
		}
		break;
	}
	else if (iSel == 2)
	{
		printf("Input the Dest City:\n");
		scanf("%s", &acDest);
		PrintHead();
		while (psPlaneNode != NULL)
		{
			if (strcmp(psPlaneNode->stData.acDest, acDest) == 0)
			{
				PrintData(psPlaneNode);
				iCount++;
			}
			psPlaneNode = psPlaneNode->pstNext;
		}
		//遍历一遍,如果记录为0,则没有记录
		if (iCount == 0)
		{
			printf("Sorry ,no record");

		}
		break;
	}
	else
	{
		printf("Sorry,please input right number:1-2\n");
	}
	}

}

void Book(ManNode* pstManNodeHead, PlaneNode* pstPlaneNodeHead)
{
	PlaneNode* pstPlaneNodeCur, * astPlaneNode[10];
	ManNode* pstManNodeCur, * pstManNodeTemp = NULL;
	char acDest[10], acID[20], acName[20], acSex[2], acDecision[2], acFlight[10];
	int iNum = 0, iRecord = 0, k = 0, iFlag = 0;
	//接收订票人信息头指针
	pstManNodeCur = pstManNodeHead;
	while (pstManNodeCur->pstNext != NULL)
	{
		pstManNodeCur = pstManNodeCur->pstNext;
	}//将订购人结构体指向尾巴

	//输入目的地
	printf("Input  the Dest  City:\n");
	scanf("%s", acDest);

	//查找目的地,存入结构体数组中
	pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	while (pstPlaneNodeCur != NULL)
	{
		if (strcmp(pstPlaneNodeCur->stData.acDest, acDest) == 0)
		{
			astPlaneNode[iRecord++] = pstPlaneNodeCur;
		}
		pstPlaneNodeCur = pstPlaneNodeCur->pstNext;
	}

	//打印数据
	printf("\nthere are %d  flights you can choose!\n", iRecord);
	PrintHead();
	for (k = 0; k < iRecord; k++)
	{
		PrintData(astPlaneNode[k]);
	}

	if (iRecord == 0)
	{
		printf("Sorry,no flights you  can  book!\n");
	}

	//订票
	else
	{
		printf("do you want to  book it?(y(Y)/n(N)\n");
		scanf("%s", acDecision);
		getchar();
		if (strcmp(acDecision, "y") == 0 || strcmp(acDecision, "Y") == 0)
		{
			printf("Input your information  !\n");
			pstManNodeTemp = (struct  ManNode*)malloc(sizeof(struct ManNode));

			printf("Input your name:");
			scanf("%s", acName);
			strcpy(pstManNodeTemp->stData.acName, acName);

			printf("Input your id:");
			scanf("%s", acID);
			strcpy(pstManNodeTemp->stData.acID, acID);

			printf("Input  your sex(M/F):");
			scanf("%s", acSex);
			strcpy(pstManNodeTemp->stData.acSex, acSex);

			printf("Input the fligth number:");
			scanf("%s", acFlight);
			strcpy(pstManNodeTemp->stData.acBookFlight, acFlight);

			for (k = 0; k < iRecord; k++)
			{
				if (strcmp(astPlaneNode[k]->stData.acFlight, acFlight) == 0)
				{
					if (astPlaneNode[k]->stData.iNum < 1)
					{
						printf("no ticket!");
						return;
					}
					printf("remain  %d  tickets!\n", astPlaneNode[k]->stData.iNum);
					iFlag = 1;
					break;
				}
			}

			if (iFlag == 0)
			{
				printf("error");
				return;
			}

			printf("Input the book  number:");
			scanf("%d", &iNum);//订购几张票
			astPlaneNode[k]->stData.iNum = astPlaneNode[k]->stData.iNum - iNum;
			pstManNodeTemp->stData.iBookNum = iNum;

			pstManNodeCur->pstNext = pstManNodeTemp;
			pstManNodeTemp->pstNext = NULL;

			pstManNodeCur = pstManNodeTemp;//可以不要
			printf("success!\n");
			iSave = 1;
		}
	}
}

void Modify(PlaneNode* pstPlaneNodeHead)
{
	PlaneNode* pstPlaneNodeCur;
	char acflight[10];
	pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	if (pstPlaneNodeCur == NULL)
	{
		printf("No flight to modifty!\n ");
		return;
	}
	else
	{
		printf("Input the flight number you want to modify:\n");
		scanf("%s", acflight);
		while (pstPlaneNodeCur!=NULL)
		{
			if (strcmp(pstPlaneNodeCur->stData.acFlight, acflight) == 0)
			{
				break;
			}
			else {
				pstPlaneNodeCur = pstPlaneNodeCur->pstNext;
			}
		}
		if (pstPlaneNodeCur) {
			//子菜单
			printf("Input the  Start  City:\n");
			scanf("%s", pstPlaneNodeCur->stData.acOrigin);
			printf("Input the Dest  City:\n");
			scanf("%s", pstPlaneNodeCur->stData.acDest);
			printf("Input the Departure time(Format  00:00):\n");
			scanf("%s", pstPlaneNodeCur->stData.acTakeOffTime);
			printf("Input the  Arrival time(Format 00:00):\n");
			scanf("%s", pstPlaneNodeCur->stData.acReverveTime);
			printf("Input the price of ticket :\n");
			scanf("%f", &pstPlaneNodeCur->stData.fPrice);
			printf("Input the discount(Format(0.0):\n");
			scanf("%s", pstPlaneNodeCur->stData.acDiscount);
			printf("Input the number  of the tickets:\n");
			scanf("%d", &pstPlaneNodeCur->stData.iNum);

			printf("Successful!\n");
			iSave = 1;
		}
	}
}

//推荐给用户推荐的机票
//目的地,时间
void Recommed(PlaneNode* pstPlaneNodeHead)
{
	PlaneNode* pstPlaneNodeCur;
	char acDest[10], acTime[10];
	int iNum=0;
	pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	printf("Input your destination:");
	scanf("%s", acDest);
	printf("Input the earlist time you can take:");
	scanf("%s", acTime);
	PrintHead();
	while (pstPlaneNodeCur!=NULL)
	{
		if (strcmp(pstPlaneNodeCur->stData.acDest, acDest) == 0)
		{
			if (strcmp(pstPlaneNodeCur->stData.acTakeOffTime, acTime) < 0)
			{
				PrintData(pstPlaneNodeCur);
				iNum++;
			}
		}
		pstPlaneNodeCur = pstPlaneNodeCur->pstNext;
	}
	printf("There are %d flight you can take!\n", iNum);
	if (iNum != 0)
	{
		printf("Please choose 3rd operation to book it!\n");
	}
}

ManNode* FindMan(ManNode* pstManNodeHead, char acID[20])
{
	ManNode* pstManNodeCur = pstManNodeHead->pstNext;
	while (pstManNodeCur!=NULL)
	{
		if (strcmp(pstManNodeCur->stData.acID, acID) == 0)
		{
			return pstManNodeCur;
		}
		pstManNodeCur = pstManNodeCur->pstNext;
	}
	return NULL;
}

PlaneNode* FindPlane(PlaneNode* PlaneNodeHead, char* acBookFlight)
{
	PlaneNode* PlaneNodeCur = PlaneNodeHead->pstNext;
	while (PlaneNodeCur!=NULL)
	{
		if (strcmp(PlaneNodeCur->stData.acFlight, acBookFlight) == 0)
		{
			return PlaneNodeCur;
		}
		PlaneNodeCur = PlaneNodeCur->pstNext;
	}
	return NULL;
}

void Refund(ManNode* pstManNodeHead,PlaneNode* pstPlaneNodeHead)
{
	ManNode* pstManNodeCur, * pstManNodeFind = NULL;
	PlaneNode* pstPlaneNodeFind=NULL;
	char acID[20];
	char acDescision[2];
	int iNum,iBookNum;

	printf("Input your ID:");
	scanf("%s", acID);

	//找到订票人的结构体
	pstManNodeFind = FindMan(pstManNodeHead, acID);

	if (pstManNodeFind == NULL) {
		printf("Can't find!\n");
	}
	else//退票
	{
		printf("\t This is your tickets:\n");
		printf("id number:%s\n", pstManNodeFind->stData.acID);
		printf("name: %s", pstManNodeFind->stData.acName);
		printf("sex:%s", pstManNodeFind->stData.acSex);
		printf("book flight:%s", pstManNodeFind->stData.acBookFlight);
		printf("book number:%d", pstManNodeFind->stData.iBookNum);

		printf("Do you want to cancel it(y/n)?");
		scanf("%s", acDescision);
		if (strcmp( acDescision ,"y") == 0 || strcmp(acDescision, "Y") == 0)
		{
			//找到前驱
			pstManNodeCur = pstManNodeHead;
			while (pstManNodeCur->pstNext!=pstManNodeFind)
			{
				pstManNodeCur = pstManNodeCur->pstNext;
			}

			//找到航班记录
			pstPlaneNodeFind= FindPlane(pstPlaneNodeHead, pstManNodeFind->stData.acBookFlight);
			//退票
			if (pstPlaneNodeFind != NULL)
			{
				iNum = pstPlaneNodeFind->stData.iNum;
				iBookNum = pstManNodeFind->stData.iBookNum;
				pstPlaneNodeFind->stData.iNum = iNum + iBookNum;
			}
			//删除订票人节点
			pstManNodeCur->pstNext = pstManNodeFind->pstNext;
			free(pstManNodeFind);
			printf(" Successful!\n");//成功退票!
			iSave = 1;
		}		
	}
}
//显示当前时间
void NowTime()
{
	time_t It;
	It = time(NULL);
	printf("现在的时间是:%s", ctime(&It));
}

int main()
{
	PlaneNode pstPlaneNodeHead;//机票信息的头节点
	pstPlaneNodeHead.pstNext = NULL;
	ManNode pstManNodeHead;//购票者信息的头节点
	pstManNodeHead.pstNext = NULL;
	int res;
	res = Init(&pstPlaneNodeHead, &pstManNodeHead); //将文件的数据加载到内存(链表)中
	if (res < 0)
	{
		return 0;
	}
	int ichoice;//用户的选择
	char isave;//是否保存
	while (1)
	{
		system("cls");
		Menu();//系统主界面
		printf("Input 0-9 operations:");
		scanf("%d", &ichoice);
		getchar();
		system("cls");
		switch (ichoice)
		{
		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(y/n)");
				scanf("%s", &isave);
				getchar();
				if (isave == 'y' || isave == 'Y')
				{
					SaveMan(&pstManNodeHead);//保存订票人的信息;
					SavePlane(&pstPlaneNodeHead);//保存机票信息;
				}
			}
			//Destroy(pstManNodeHead,pstPlaneNodeHead);
			return 0;
		}
		printf("\nplease press any key  to continue...\n");
		_getch();
	}
	//Destroy(pstManNodeHead,pstPlaneNodeHead);
	return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值