C語言實現飛機訂票系統的完整代碼

題目

本文將設計一個飛機訂票系統,主要包括機票的添加、修改、查找、訂票、退票、推薦機票功能,用C語言編程實現,並用鏈表存儲產生的航班信息和訂票信息數據,最後可以把鏈表中的數據保存到文件中。

總體設計和需求分析

設計目的

1.怎樣去合理的設計一個數據結構來存儲訂票信息和航班信息

2.加深對鏈表的增刪改查功能的學習和理解

3.學習如何鏈表和外部文件之間的讀取和存儲操作

4.學習如何將學到的知識應用到實際的問題中

總體設計和功能

此飛機訂票系統將實現以下功能:
 

1.初始化功能

  該功能將實現將航班信息文件和訂票人訂票信息文件中的數據讀取到鏈表中。

2.添加機票信息功能

  該功能將實現添加航班機票信息,信息包括航班號、出發地、目的地、起飛時間、降落時間、票價、折扣、剩餘票數,然後用鏈表將數據保存起來。

3.查詢機票信息功能

  該功能將實現通過航班號來查詢機票信息的功能

4.修改機票信息功能

  該功能將通過航班號調用查找機票信息功能,定位到要修改的航班信息上,然後通過航班信息子菜單確定要修改的某一項航班信息。

5.顯示機票信息

  該功能將實現把所有航班信息顯示出來

7.推薦機票功能

  該功能將實現通過輸入目的地,和乘客最早出發時間可以獲得符合條件的航班信息

8.訂票功能

  該功能下乘客輸入目的地,並且輸入訂票人的姓名、身份證號碼、性別、購票數量、訂購航班號以實現用戶訂票功能

9.退票功能

  該功能下乘客可以訂購的機票進行退回。

10.顯示時間功能

  該功能可以顯示實時時間

11.保存信息功能

該功能下可以把鏈表中的信息保存到文件中。

結構體設計

我們將定義兩個結構體,分別為機票信息結構體和訂票人信息結構體

機票信息結構體

typedef struct AirPlane
{
	char acFlight[20];//航班號
	char acOrigin[10]; //出發地
	char acDest[20];  //目的地
	char acTakeOffTime[10]; //起飛時間
	char acReverceTime[10]; //降落時間
	float fPrice;  //票價
	char acDisscount[4];  //折扣
	int iNum;  //剩餘票數
}AirPlane, * PAirPlane;
//定義機票信息節點的結構體
typedef struct PlaneNode
{
	struct AirPlane stDate;
	struct PlaneNode* pstNext;

}PlaneNode, * PPlaneNode;

訂票人信息結構體

typedef struct Man
{
	char acName[20];  //姓名
	char acID[20];   //身份證號碼
	char acSex[10];   //性別 
	int  iBookNum;   //購票數量
	char acBookFilght[10];  //訂購航班號
}Man, * PMan;
//訂票人信息節點的結構體
typedef struct ManNode
{
	struct Man stDate;
	struct ManNode* pstNext;
}ManNode, * PManNode;

主函數的設計

在主函數中,首先將通過初始化函數把文件中的數據讀取到鏈表中,然後可以通過switch選擇用戶在飛機訂票系統中需要執行的操作

//作為數據改動的標志
int iSave = 0;

int main()
{
	struct PlaneNode pstPlaneNodeHead;  //機票信息頭結點
	pstPlaneNodeHead.pstNext = NULL;
	struct ManNode pstManNodeHead;    //購票信息頭結點
	pstManNodeHead.pstNext = NULL;

	Init(&pstPlaneNodeHead, &pstManNodeHead);  // 將文件的數據加載到內存中

	int iSel = 0; //用於接收用戶對功能的選擇


	char c1;
	while (1)
	{
		system("cls");
		Menu();
		printf("Input 0-9 operations:");
		scanf_s("%d", &iSel);
		getchar();
		switch (iSel)
		{
		case 1:
			printf("進入添加機票頁面\n");
			Insert(&pstPlaneNodeHead); // 添加機票信息
			break;
		case 2:
			Search(&pstPlaneNodeHead);   //查詢機票信息
			break;
		case 3:
			Book(&pstPlaneNodeHead, &pstManNodeHead);//訂票
			break;
		case 4:
			Modify(&pstPlaneNodeHead);    //修改機票信息
			break;
		case 5:
			Show(&pstPlaneNodeHead);   //顯示機票信息  
			break;
		case 6:
			Recommend(&pstPlaneNodeHead);  //推薦機票信息
			break;
		case 7:
			Refund(&pstPlaneNodeHead, &pstManNodeHead);//退票信息
			break;
		case 8:
			NowTime();//顯示當前時間
			break;
		case 9:
			SaveMan(&pstManNodeHead);     //數據保存
			SavePlane(&pstPlaneNodeHead);
			break;
		case 0:
			if (iSave == 1)
			{
				printf("do you want to save (y/n)");
				scanf("%c", &c1);
				getchar();
				if (c1 == 'y' || c1 == 'Y')
				{
					SaveMan(&pstManNodeHead);     //保存訂票人的信息
					SavePlane(&pstPlaneNodeHead);    // 保存機票信息

				}
			}
			Destroy(&pstPlaneNodeHead, &pstManNodeHead);
			return 0;
		}
		printf("\nplease press any key to continue...\n");
		_getch();
	}
	Destroy(&pstPlaneNodeHead, &pstManNodeHead);
	return 0;
}

各功能代碼的實現

前置

寫入需要用到的頭文件、宏定義以及其中的打印函數等等

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<Windows.h>
#include<malloc.h>
#include<string.h>
#include<conio.h>
#include <time.h>

#define HEAD1 "*******************************************************************\n"
#define HEAD2 "|Flight|StartCity|DestCity|DepertureTime|Arrival|  price   |number|\n"
#define HEAD3 "|------|---------|--------|-------------|-------|----------|------|\n"
#define FORMET  "%-9s%-9s%-10s%-14s%-10s%-2.2f%6d\n"
#define DATA  pst->stDate.acFlight,pst->stDate.acOrigin,pst->stDate.acDest,pst->stDate.acTakeOffTime,pst->stDate.acReverceTime,pst->stDate.fPrice,pst->stDate.iNum

```c
//主菜單
void Menu()
{
	puts("**********************************************************************");
	puts("****** Welcome to the airplane tickets booking system");
	puts("----------------------------------------------------------------------");
	puts("                    choose the follow operation(0-9)                 *");
	puts("--------------------------------------------------------------------- ");
	puts("***********      1 Insert  flights              2 Search  flights     ");
	puts("**********       3 Book tickets                 4 Modify  fligts  date");
	puts("**********       5 Show flights                 6 Recommend flights   ");
	puts("************     7 Refund tickets               8 Show current time   ");
	puts("***********      9 Save to files                0 quit                ");
	puts("**********************************************************************");
}
//打印函數
void PrintHead()
{
	printf(HEAD1);
	printf(HEAD2);
	printf(HEAD3);
}

void PrintDate(PPlaneNode stLP)
{
	PPlaneNode pst = stLP;
	printf(FORMET, DATA);

}
//銷毀函數  防止內存泄漏
void Destroy(PPlaneNode pstPlaneNodeHead, PManNode pstManNodeHead)
{
	assert(pstManNodeHead != NULL);
	assert(pstPlaneNodeHead != NULL);
	PPlaneNode p = pstPlaneNodeHead->pstNext;
	PManNode q = pstManNodeHead->pstNext;
	while (p != NULL)
	{
		PPlaneNode tmp = p;
		p = p->pstNext;
		free(tmp);
	 }
	while (q != NULL)
	{
		PManNode tmp = q;
		q = q->pstNext;
		free(tmp);
	}
}
#### 初始化

```c
int Init(struct PlaneNode* pstPlaneNodeHead, struct ManNode* pstManNodeHead)
{
	//先加載飛機機票信息
	FILE* pfPlane; //定義飛機機票信息文件指針
	struct 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 = (struct PlaneNode*)malloc(sizeof(struct PlaneNode));
			if (fread(pstPlaneNodeTemp, sizeof(struct PlaneNode), 1, pfPlane) !=0)
			{
				pstPlaneNodeTemp->pstNext = NULL;
				pstPlaneNodeCur->pstNext = pstPlaneNodeTemp;
				pstPlaneNodeCur = pstPlaneNodeTemp;
			}
		}
		free(pstPlaneNodeTemp);  //此時,釋放的是文件讀完後最後一次申請的指針
		fclose(pfPlane);
	}
	//加載訂票人信息
	FILE* pfMan;  // 定義訂票人信息文件指針
	struct 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 Insert(struct PlaneNode* pstPlaneNodeHead)
{
	assert(pstPlaneNodeHead != NULL);
	if (pstPlaneNodeHead == NULL)
	{
		return;
	}
	struct PlaneNode* pstNode, * pstHead, * pstTail, * pstCur, * pstNew;
	char acFlight[20];  //保存航班號
	pstHead = pstTail = pstPlaneNodeHead;

	//讓pstTail指向最後一個節點
	while (pstTail->pstNext != NULL)
	{
		pstTail = pstTail->pstNext;

	}
	while (1)
	{
		printf("Input the flight number (-1 to end):");
		scanf_s("%s", acFlight, 20);
		getchar();
		if (strcmp(acFlight, "-1") == 0)
		{
			break;
		}
		//航班號唯一
		pstCur = pstPlaneNodeHead->pstNext;

		while (pstCur != NULL)
		{
			if (strcmp(acFlight, pstCur->stDate.acFlight) == 0)
			{
				printf("this flight %s esists!\n", acFlight);
				return;
			}
			pstCur = pstCur->pstNext;
		}
		//如果航班號沒有和現有記錄航班號重復 ,則重新建一個鏈表節點
		pstNew = (struct PlaneNode*)malloc(sizeof(struct PlaneNode));
		strcpy_s(pstNew->stDate.acFlight, 20, acFlight);

		printf("Input the Start City:\n");
		scanf_s("%s", pstNew->stDate.acOrigin, 10);
		printf("Input the Dest City:\n");
		scanf_s("%s", pstNew->stDate.acDest, 20);
		printf("Input the Departure time(Format 00:00):\n");
		scanf_s("%s", pstNew->stDate.acTakeOffTime, 10);
		printf("Input the Arrival time(Format 00:00):\n");
		scanf_s("%s", pstNew->stDate.acReverceTime, 10);
		printf("Input the Price of ticket:\n ");
		scanf_s("%f", &pstNew->stDate.fPrice);
		printf("Input the discount(Fromat(0.0):\n");
		scanf_s("%s", pstNew->stDate.acDisscount, 4);
		printf("Input the number of the tickets:\n");
		scanf_s("%d", &pstNew->stDate.iNum);

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

		//如果有新的航班信息,保存標準置為1,若退出需要提示是否保存信息(見主函數)
		iSave = 1;
	}
}

查找機票信息

//查詢機票信息
void Search(PPlaneNode pstPlaneNodeHead)
{
	assert(pstPlaneNodeHead != NULL);
	if (pstPlaneNodeHead == NULL)
	{
		return;
	}
	system("cls");
	int iSel = 0;
	int icount = 0;
	PPlaneNode pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	if (pstPlaneNodeCur == NULL)
	{
		printf("No flight record");
		return;
	}
	printf("Choose one way according to:\n 1.flight  2.Dest:\n");
	scanf_s("%d", &iSel);
	if (iSel == 1)
	{
		char acFlight[20];
		printf("請輸入要查詢的航班號:\n");
		scanf_s("%s", &acFlight, 20);
		getchar();
		//打開訂票信息文件
		for (pstPlaneNodeCur; pstPlaneNodeCur != NULL; pstPlaneNodeCur = pstPlaneNodeCur->pstNext)
		{
			if (strcmp(pstPlaneNodeCur->stDate.acFlight, acFlight) == 0)
			{

				PrintHead();
				PrintDate(pstPlaneNodeCur);

				break; //航班號唯一,查到就退出
			}
		}
	}
	else if (iSel == 2)
	{
		char acDest;
		printf("Input the Dest City:\n");
		scanf_s("%s", &acDest, 20);
		PrintHead();
		for (pstPlaneNodeCur; pstPlaneNodeCur != NULL; pstPlaneNodeCur = pstPlaneNodeCur->pstNext)
		{
			if (strcmp(pstPlaneNodeCur->stDate.acDest, &acDest) == 0)
			{


				PrintDate(pstPlaneNodeCur);
				icount++;  //同一個目的地可能有多個數據

			}
		}
		if (icount == 0)  //遍歷完一遍,記錄數為0,則沒有記錄:
		{

			printf("Sorry ,no record!\n");
		}
	}
	else
	{
		printf("sorry please input right number1-2");
	}
}

修改機票信息

void Mod_menu()
{
	puts("**********************************************************************");
	puts("----------------------------------------------------------------------");
	puts("                    choose the follow operation(0-8)                  ");
	puts("--------------------------------------------------------------------- ");
	puts("*******************       1   flights          ********************   ");
	puts("*******************       2   Origin           ********************   ");
	puts("*******************       3   Destination      ********************   ");
	puts("*******************       4   Take off time    ********************   ");
	puts("*******************       5   Reverce time     ********************   ");
	puts("*******************       6   Prices           ********************   ");
	puts("*******************       7   Disscount        ********************   ");
	puts("*******************       8   ticket number    ********************   ");
	puts("*******************       0   quits            ********************   ");
	puts("**********************************************************************");
}
void Modify(PPlaneNode pstPlaneNodeHead)
{
	assert(pstPlaneNodeHead != NULL);
	if (pstPlaneNodeHead == NULL)
	{
		return;
	}
	char acFlight[20];
	PPlaneNode 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("%s", acFlight, 20);
		for (pstPlaneNodeCur; pstPlaneNodeCur != NULL; pstPlaneNodeCur = pstPlaneNodeCur->pstNext)
		{
			if (strcmp(pstPlaneNodeCur->stDate.acFlight, acFlight) == 0)
			{
				break;
			}

		}
		if (pstPlaneNodeCur)
		{
			//子菜單

			int isel = 0;
			system("cls");
			Mod_menu();
			printf("please Input 0-8: ");
			scanf_s("%d", &isel);
			getchar();
			switch (isel)
			{
			case 1:
				printf("Input new flights!\n");
				scanf("%s", pstPlaneNodeCur->stDate.acFlight);
				break;
			case 2:
				printf("Input new Origin!\n");
				scanf("%s", pstPlaneNodeCur->stDate.acOrigin);
				break;
			case 3:
				printf("Input new Destination!\n");
				scanf("%s", pstPlaneNodeCur->stDate.acDest);
				break;
			case 4:
				printf("Input new Take off time!\n");
				scanf("%s", pstPlaneNodeCur->stDate.acTakeOffTime);
				break;
			case 5:
				printf("Input new Reverce time!\n");
				scanf("%s", pstPlaneNodeCur->stDate.acReverceTime);
				break;
			case 6:
				printf("Input new Prices!\n");
				scanf("%f", &pstPlaneNodeCur->stDate.fPrice);
				break;
			case 7:
				printf("Input new Disscount!\n");
				scanf("%s", pstPlaneNodeCur->stDate.acDisscount);
				break;
			case 8:
				printf("Input new ticket number!\n");
				scanf("%d", &pstPlaneNodeCur->stDate.iNum);
				break;
			case 0:
				printf("quit!\n");
				break;
			}
			printf("End Modifying information!\n");
		}
		else
		{
			printf("flights number not exist!\n");
		}
	}
}

顯示機票信息

void Show(PPlaneNode pstPlaneNodeHead)
{
	assert(pstPlaneNodeHead != NULL);
	PPlaneNode pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	PrintHead();
	if (pstPlaneNodeHead->pstNext == NULL)
	{
		printf("no flight ticket!\n");

	}
	else
	{
		while (pstPlaneNodeCur != NULL)
		{
			PrintDate(pstPlaneNodeCur);
			pstPlaneNodeCur = pstPlaneNodeCur->pstNext;
		}
	}
}

推薦機票信息

struct ManNode* FindMan(PManNode pstManNodeHead, char acId[20])

{
	PManNode pstManNodeCur = pstManNodeHead->pstNext;
	for (pstManNodeCur; pstManNodeCur != NULL; pstManNodeCur = pstManNodeCur->pstNext)
	{
		if (strcmp(pstManNodeCur->stDate.acID, acId) == 0) // 知道到id號相同的訂票人的
		{
			return pstManNodeCur;
		}
	}
	return NULL;
}

PPlaneNode FindPlane(PPlaneNode pstPlaneNodeHead, char* acBookFlight)
{
	PPlaneNode pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	for (pstPlaneNodeCur; pstPlaneNodeCur != NULL; pstPlaneNodeCur = pstPlaneNodeCur->pstNext)
	{
		if (strcmp(pstPlaneNodeCur->stDate.acFlight, acBookFlight) == 0) // 知道到id號相同的訂票人的
		{
			return pstPlaneNodeCur;
		}
	}
	return NULL;
}
void Recommend(PPlaneNode pstPlaneNodeHead)
{
	//推薦給用用戶合適的機票
	//時間 地點
	PPlaneNode pstPlaneNodeCur;
	char acDest[20];
	char acTime[10];
	int iNum = 0;
	pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	printf("Input your destination");
	scanf_s("%s", acDest, 20);
	printf("Input the earlist time you can take:");
	scanf_s("%s", acTime, 10);
	PrintHead();
	for (pstPlaneNodeCur; pstPlaneNodeCur != NULL; pstPlaneNodeCur = pstPlaneNodeCur->pstNext)
	{
		if (strcmp(pstPlaneNodeCur->stDate.acDest, acDest) == 0)
		{
			if (strcmp(acTime,pstPlaneNodeCur->stDate.acTakeOffTime) < 0)
			{
				PrintDate(pstPlaneNodeCur);
				iNum++;
			}
		}
	}
}

訂票

void Book(PPlaneNode pstPlaneNodeHead, PManNode pstManNodeHead)
{

	//接收訂票人頭指針
	PPlaneNode pstPlaneNodeCur, astPlaneNode[10];
	PManNode pstManNodeCur, astManNodeTemp = NULL;
	char acDest[20];
	char acId[20];
	char acName[20];
	char acSex[10];
	char acDecision[2];
	char  acFlight[20];
	int iNum = 0;
	int iRecord = 0;
	int k = 0;
	char iFlag = 0;

	pstManNodeCur = pstManNodeHead;
	for (pstManNodeCur; pstManNodeCur->pstNext != NULL; pstManNodeCur = pstManNodeCur->pstNext);

	//將訂票人結構體指向尾巴
 //輸入目的地
	printf("please Input Dest City:\n");
	scanf_s("%s", &acDest, 20);
	getchar();
	//查找目的地 存入結構體數組中
	pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	for (pstPlaneNodeCur; pstPlaneNodeCur != NULL; pstPlaneNodeCur = pstPlaneNodeCur->pstNext)
	{
		if (strcmp(pstPlaneNodeCur->stDate.acDest, acDest) == 0)
		{
			astPlaneNode[iRecord] = pstPlaneNodeCur;
			iRecord++;
		}

	}
	printf("\n there are %d flight you can choose! \n", iRecord);
	PrintHead();
	for (k = 0; k < iRecord; k++)
	{
		PrintDate(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))");
		scanf_s("%s", acDecision, 2);
		getchar();
		if (strcmp(acDecision, "y") == 0 || strcmp(acDecision, "Y") == 0)
		{
			printf("Input your information ! \n");
			astManNodeTemp = (PManNode)malloc(sizeof(ManNode));
			//assert
			printf("Input your Name :\n");
			scanf_s("%s", acName, 20);
			strcpy(astManNodeTemp->stDate.acName, acName);

			printf("Input your Id: \n");
			scanf_s("%s", acId, 20);
			strcpy(astManNodeTemp->stDate.acID, acId);

			printf("Input your sex(M/F) : \n");
			scanf_s("%s", acSex, 10);
			strcpy(astManNodeTemp->stDate.acSex, acSex);

			printf("Input your Flights :\n");
			scanf_s("%s", acFlight, 20);
			strcpy(astManNodeTemp->stDate.acBookFilght, acFlight);

			for (k = 0; k < iRecord; k++)
			{
				if (strcmp(astPlaneNode[k]->stDate.acFlight, acFlight) == 0)
				{
					if (astPlaneNode[k]->stDate.iNum < 1)
					{
						printf("No tickets!");
						return;
					}
					printf("return %d tickets!\n", astPlaneNode[k]->stDate.iNum);
					iFlag = 1;
					break;
				}
			}
			if (iFlag == 0)
			{
				printf("error");
				return;
			}
			printf("Input the book number: \n"); //訂購幾張票
			scanf_s("%d", &iNum);
			astPlaneNode[k]->stDate.iNum = astPlaneNode[k]->stDate.iNum - iNum;  //還剩下的票數
			astManNodeTemp->stDate.iBookNum = iNum;  //訂購票數

			pstManNodeCur->pstNext = astManNodeTemp;  //鏈接鏈表
			astManNodeTemp->pstNext = NULL;

			pstManNodeCur = astManNodeTemp;   //移動當前鏈表指針位置
			printf("Finish Book!\n");
		}
	}
}

退票

//退票
void Refund(PPlaneNode pstPlaneNodeHead, PManNode pstManNodeHead)
{
	PManNode pstManNodeCur, pstManNodeFind = NULL;
	PPlaneNode pstPlaneNodeFind = NULL;
	char acId[20];
	char acDecision[2];
	int iNum = 0; //剩餘票數
	int iBookNum;  //
	//找到訂票人的結構體
	printf("Input your Id!\n");
	scanf_s("%s", acId, 20);

	pstManNodeFind = FindMan(pstManNodeHead, acId);
	if (pstManNodeFind == NULL)
	{
		printf("can;t find!\n");
	}
	else//退票
	{
		printf("this is your tickets:\n");
		printf("id number:%s\n", pstManNodeFind->stDate.acID);
		printf("nmae:%s\n", pstManNodeFind->stDate.acName);
		printf("sex:%s\n", pstManNodeFind->stDate.acSex);
		printf("book flight:%s\n", pstManNodeFind->stDate.acBookFilght);
		printf("book number:%d\n", pstManNodeFind->stDate.iBookNum);

		printf("do you want to cancel it ?(y/n)");
		scanf_s("%s", acDecision, 2);
		getchar();
		if (strcmp(acDecision, "y") == 0)
		{
			//找到前驅
			for (pstManNodeCur = pstManNodeHead; pstManNodeCur->pstNext != pstManNodeFind;pstManNodeCur = pstManNodeCur->pstNext);
			//找到該名訂購人的航班記錄
			pstPlaneNodeFind = FindPlane(pstPlaneNodeHead, pstManNodeFind->stDate.acBookFilght);
			//退票
			if (pstPlaneNodeFind != NULL)
			{
				iNum = pstPlaneNodeFind->stDate.iNum;//機票剩餘票數
				iBookNum = pstManNodeFind->stDate.iBookNum;  //訂購人訂購票數
				pstPlaneNodeFind->stDate.iNum = iNum + iBookNum;
			}
			//刪除訂票人節點
			pstManNodeCur->pstNext = pstManNodeFind->pstNext;
			printf("successful!\n"); //成功退訂 

			iSave = 1;
			free(pstManNodeFind);
		}
	}
}

保存信息

//保存機票信息
void SavePlane(struct PlaneNode* pstPlaneNodeHead)
{
	FILE* pfPlane;  // 機票的文件指針
	struct PlaneNode* pstPlaneNodeCur;
	int count = 0; //保存信息個數
	int iFlag = 1;
	int error = 0;
	//pfPlane = fopen("plane.txt", "wb");
	error = fopen_s(&pfPlane, "plane.txt", "wb");

	if (error != 0)
	{
		printf("the file can't be opened!\n");
		return;
	}

	//寫入信息
	pstPlaneNodeCur = pstPlaneNodeHead->pstNext;
	while (pstPlaneNodeCur != NULL)
	{
		if (fwrite(pstPlaneNodeCur, sizeof(struct PlaneNode), 1, pfPlane) == 1)
		{
			pstPlaneNodeCur = pstPlaneNodeCur->pstNext;
			count++;
		}
		else
		{
			iFlag = 0;
			break;
		}
	}

	//提示保存信息數目  ISave置為0
	if (iFlag)
	{
		printf("you have save %d flights\n", &count);
		iSave = 0;
	}
	//關閉文件
	fclose(pfPlane);
}
//保存訂票人信息
void SaveMan(struct ManNode* pstManNodeHead)
{
	assert(pstManNodeHead != NULL);
	if (pstManNodeHead == NULL)
	{
		return;
	}
	FILE* pfMan;
	struct ManNode* pstManNodeCur;
	int count = 0;
	int iFlag = 1;
	int error = 0;
	//pfMan = fopen("man.txt", "wb");
	error = fopen_s(&pfMan, "man.txt", "wb");
	if (error != 0)
	{
		printf("the file can't be opened!\n");
	}
	//寫入信息
	pstManNodeCur = pstManNodeHead->pstNext;
	while (pstManNodeCur != NULL)
	{
		if (fwrite(pstManNodeCur, sizeof(struct ManNode), 1, pfMan) == 1)
		{
			pstManNodeCur = pstManNodeCur->pstNext;
			count++;
		}
		else
		{
			iFlag = 0;
			break;
		}

		if (iFlag)
		{
			printf("you have save %d man", count);
			iSave = 0;
		}
		fclose(pfMan);
	}
}

顯示時間

//顯示當前時間
void NowTime()
{
	time_t curtime;
	curtime = time(NULL);
	printf("現在的時間是:%s", ctime(&curtime));
}

測試

先進行添加機票功能的測試

![在這裡插入圖片描述](https://img-blog.csdnimg.cn/7d5bcf3869d14bd5a8e6e8c157474d03.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA57yW56iL5bCP56iL,size_20,color_FFFFFF,t_70,g_se,x_16

將添加機票信息輸入完整,然後輸入-1返回主菜單

然後對查詢機票功能進行測試,輸入剛剛添加的航班號,可以看出已經查詢到新添加的機票信息

然後對修改機票信息功能進行測試,我們將目的地修改為beijing

然後返回主菜單,測試顯示所有機票信息的功能

可以看出已經成功把1005號航班的目的地修改為beijing

對推薦機票功能進行測試

選擇你要去的地方和你最早可以到達時間,然後會打印符合條件的機票信息

然後對訂票功能進行測試

然後對退票功能進行測試,通過ID可以定位到剛剛訂票的信息,然後選擇退票

總結

到此這篇關於C語言實現飛機訂票系統的文章就介紹到這瞭,更多相關C語言飛機訂票系統內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: