首頁 > 軟體

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

2022-06-15 14:00:44

題目

本文將設計一個飛機訂票系統,主要包括機票的新增、修改、查詢、訂票、退票、推薦機票功能,用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%6dn"
#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:%sn", pstManNodeFind->stDate.acID);
		printf("nmae:%sn", pstManNodeFind->stDate.acName);
		printf("sex:%sn", pstManNodeFind->stDate.acSex);
		printf("book flight:%sn", pstManNodeFind->stDate.acBookFilght);
		printf("book number:%dn", 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 flightsn", &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));
}

測試

先進行新增機票功能的測試

將新增機票資訊輸入完整,然後輸入-1返回主選單

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

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

然後返回主選單,測試顯示所有機票資訊的功能

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

對推薦機票功能進行測試

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

然後對訂票功能進行測試

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

總結

到此這篇關於C語言實現飛機訂票系統的文章就介紹到這了,更多相關C語言飛機訂票系統內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com