溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)

發(fā)布時(shí)間:2022-06-15 13:40:47 來(lái)源:億速云 閱讀:164 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

總體設(shè)計(jì)和需求分析

設(shè)計(jì)目的

1.怎樣去合理的設(shè)計(jì)一個(gè)數(shù)據(jù)結(jié)構(gòu)來(lái)存儲(chǔ)訂票信息和航班信息

2.加深對(duì)鏈表的增刪改查功能的學(xué)習(xí)和理解

3.學(xué)習(xí)如何鏈表和外部文件之間的讀取和存儲(chǔ)操作

4.學(xué)習(xí)如何將學(xué)到的知識(shí)應(yīng)用到實(shí)際的問(wèn)題中

總體設(shè)計(jì)和功能

此飛機(jī)訂票系統(tǒng)將實(shí)現(xiàn)以下功能:
 

1.初始化功能

  該功能將實(shí)現(xiàn)將航班信息文件和訂票人訂票信息文件中的數(shù)據(jù)讀取到鏈表中。

2.添加機(jī)票信息功能

  該功能將實(shí)現(xiàn)添加航班機(jī)票信息,信息包括航班號(hào)、出發(fā)地、目的地、起飛時(shí)間、降落時(shí)間、票價(jià)、折扣、剩余票數(shù),然后用鏈表將數(shù)據(jù)保存起來(lái)。

3.查詢機(jī)票信息功能

  該功能將實(shí)現(xiàn)通過(guò)航班號(hào)來(lái)查詢機(jī)票信息的功能

4.修改機(jī)票信息功能

  該功能將通過(guò)航班號(hào)調(diào)用查找機(jī)票信息功能,定位到要修改的航班信息上,然后通過(guò)航班信息子菜單確定要修改的某一項(xiàng)航班信息。

5.顯示機(jī)票信息

  該功能將實(shí)現(xiàn)把所有航班信息顯示出來(lái)

7.推薦機(jī)票功能

  該功能將實(shí)現(xiàn)通過(guò)輸入目的地,和乘客最早出發(fā)時(shí)間可以獲得符合條件的航班信息

8.訂票功能

  該功能下乘客輸入目的地,并且輸入訂票人的姓名、身份證號(hào)碼、性別、購(gòu)票數(shù)量、訂購(gòu)航班號(hào)以實(shí)現(xiàn)用戶訂票功能

9.退票功能

  該功能下乘客可以訂購(gòu)的機(jī)票進(jìn)行退回。

10.顯示時(shí)間功能

  該功能可以顯示實(shí)時(shí)時(shí)間

11.保存信息功能

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

結(jié)構(gòu)體設(shè)計(jì)

我們將定義兩個(gè)結(jié)構(gòu)體,分別為機(jī)票信息結(jié)構(gòu)體和訂票人信息結(jié)構(gòu)體

機(jī)票信息結(jié)構(gòu)體

typedef struct AirPlane
{
	char acFlight[20];//航班號(hào)
	char acOrigin[10]; //出發(fā)地
	char acDest[20];  //目的地
	char acTakeOffTime[10]; //起飛時(shí)間
	char acReverceTime[10]; //降落時(shí)間
	float fPrice;  //票價(jià)
	char acDisscount[4];  //折扣
	int iNum;  //剩余票數(shù)
}AirPlane, * PAirPlane;
//定義機(jī)票信息節(jié)點(diǎn)的結(jié)構(gòu)體
typedef struct PlaneNode
{
	struct AirPlane stDate;
	struct PlaneNode* pstNext;

}PlaneNode, * PPlaneNode;

訂票人信息結(jié)構(gòu)體

typedef struct Man
{
	char acName[20];  //姓名
	char acID[20];   //身份證號(hào)碼
	char acSex[10];   //性別 
	int  iBookNum;   //購(gòu)票數(shù)量
	char acBookFilght[10];  //訂購(gòu)航班號(hào)
}Man, * PMan;
//訂票人信息節(jié)點(diǎn)的結(jié)構(gòu)體
typedef struct ManNode
{
	struct Man stDate;
	struct ManNode* pstNext;
}ManNode, * PManNode;

主函數(shù)的設(shè)計(jì)

在主函數(shù)中,首先將通過(guò)初始化函數(shù)把文件中的數(shù)據(jù)讀取到鏈表中,然后可以通過(guò)switch選擇用戶在飛機(jī)訂票系統(tǒng)中需要執(zhí)行的操作

//作為數(shù)據(jù)改動(dòng)的標(biāo)志
int iSave = 0;

int main()
{
	struct PlaneNode pstPlaneNodeHead;  //機(jī)票信息頭結(jié)點(diǎn)
	pstPlaneNodeHead.pstNext = NULL;
	struct ManNode pstManNodeHead;    //購(gòu)票信息頭結(jié)點(diǎn)
	pstManNodeHead.pstNext = NULL;

	Init(&pstPlaneNodeHead, &pstManNodeHead);  // 將文件的數(shù)據(jù)加載到內(nèi)存中

	int iSel = 0; //用于接收用戶對(duì)功能的選擇


	char c1;
	while (1)
	{
		system("cls");
		Menu();
		printf("Input 0-9 operations:");
		scanf_s("%d", &iSel);
		getchar();
		switch (iSel)
		{
		case 1:
			printf("進(jìn)入添加機(jī)票頁(yè)面\n");
			Insert(&pstPlaneNodeHead); // 添加機(jī)票信息
			break;
		case 2:
			Search(&pstPlaneNodeHead);   //查詢機(jī)票信息
			break;
		case 3:
			Book(&pstPlaneNodeHead, &pstManNodeHead);//訂票
			break;
		case 4:
			Modify(&pstPlaneNodeHead);    //修改機(jī)票信息
			break;
		case 5:
			Show(&pstPlaneNodeHead);   //顯示機(jī)票信息  
			break;
		case 6:
			Recommend(&pstPlaneNodeHead);  //推薦機(jī)票信息
			break;
		case 7:
			Refund(&pstPlaneNodeHead, &pstManNodeHead);//退票信息
			break;
		case 8:
			NowTime();//顯示當(dāng)前時(shí)間
			break;
		case 9:
			SaveMan(&pstManNodeHead);     //數(shù)據(jù)保存
			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);    // 保存機(jī)票信息

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

各功能代碼的實(shí)現(xiàn)

前置

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

#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("**********************************************************************");
}
//打印函數(shù)
void PrintHead()
{
	printf(HEAD1);
	printf(HEAD2);
	printf(HEAD3);
}

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

}
//銷毀函數(shù)  防止內(nèi)存泄漏
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)
{
	//先加載飛機(jī)機(jī)票信息
	FILE* pfPlane; //定義飛機(jī)機(jī)票信息文件指針
	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
	{
		//把文件數(shù)據(jù)讀入到鏈表中
		while (!feof(pfPlane)) //讀到文件最后一個(gè)數(shù)據(jù)
		{
			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);  //此時(shí),釋放的是文件讀完后最后一次申請(qǐng)的指針
		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;
}

添加機(jī)票

void Insert(struct PlaneNode* pstPlaneNodeHead)
{
	assert(pstPlaneNodeHead != NULL);
	if (pstPlaneNodeHead == NULL)
	{
		return;
	}
	struct PlaneNode* pstNode, * pstHead, * pstTail, * pstCur, * pstNew;
	char acFlight[20];  //保存航班號(hào)
	pstHead = pstTail = pstPlaneNodeHead;

	//讓pstTail指向最后一個(gè)節(jié)點(diǎn)
	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;
		}
		//航班號(hào)唯一
		pstCur = pstPlaneNodeHead->pstNext;

		while (pstCur != NULL)
		{
			if (strcmp(acFlight, pstCur->stDate.acFlight) == 0)
			{
				printf("this flight %s esists!\n", acFlight);
				return;
			}
			pstCur = pstCur->pstNext;
		}
		//如果航班號(hào)沒(méi)有和現(xiàn)有記錄航班號(hào)重復(fù) ,則重新建一個(gè)鏈表節(jié)點(diǎn)
		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;

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

查找機(jī)票信息

//查詢機(jī)票信息
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("請(qǐng)輸入要查詢的航班號(hào):\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; //航班號(hào)唯一,查到就退出
			}
		}
	}
	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++;  //同一個(gè)目的地可能有多個(gè)數(shù)據(jù)

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

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

修改機(jī)票信息

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");
		}
	}
}

顯示機(jī)票信息

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;
		}
	}
}

推薦機(jī)票信息

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號(hào)相同的訂票人的
		{
			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號(hào)相同的訂票人的
		{
			return pstPlaneNodeCur;
		}
	}
	return NULL;
}
void Recommend(PPlaneNode pstPlaneNodeHead)
{
	//推薦給用用戶合適的機(jī)票
	//時(shí)間 地點(diǎn)
	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);

	//將訂票人結(jié)構(gòu)體指向尾巴
 //輸入目的地
	printf("please Input Dest City:\n");
	scanf_s("%s", &acDest, 20);
	getchar();
	//查找目的地 存入結(jié)構(gòu)體數(shù)組中
	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"); //訂購(gòu)幾張票
			scanf_s("%d", &iNum);
			astPlaneNode[k]->stDate.iNum = astPlaneNode[k]->stDate.iNum - iNum;  //還剩下的票數(shù)
			astManNodeTemp->stDate.iBookNum = iNum;  //訂購(gòu)票數(shù)

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

			pstManNodeCur = astManNodeTemp;   //移動(dòng)當(dāng)前鏈表指針位置
			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; //剩余票數(shù)
	int iBookNum;  //
	//找到訂票人的結(jié)構(gòu)體
	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)
		{
			//找到前驅(qū)
			for (pstManNodeCur = pstManNodeHead; pstManNodeCur->pstNext != pstManNodeFind;pstManNodeCur = pstManNodeCur->pstNext);
			//找到該名訂購(gòu)人的航班記錄
			pstPlaneNodeFind = FindPlane(pstPlaneNodeHead, pstManNodeFind->stDate.acBookFilght);
			//退票
			if (pstPlaneNodeFind != NULL)
			{
				iNum = pstPlaneNodeFind->stDate.iNum;//機(jī)票剩余票數(shù)
				iBookNum = pstManNodeFind->stDate.iBookNum;  //訂購(gòu)人訂購(gòu)票數(shù)
				pstPlaneNodeFind->stDate.iNum = iNum + iBookNum;
			}
			//刪除訂票人節(jié)點(diǎn)
			pstManNodeCur->pstNext = pstManNodeFind->pstNext;
			printf("successful!\n"); //成功退訂 

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

保存信息

//保存機(jī)票信息
void SavePlane(struct PlaneNode* pstPlaneNodeHead)
{
	FILE* pfPlane;  // 機(jī)票的文件指針
	struct PlaneNode* pstPlaneNodeCur;
	int count = 0; //保存信息個(gè)數(shù)
	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;
		}
	}

	//提示保存信息數(shù)目  ISave置為0
	if (iFlag)
	{
		printf("you have save %d flights\n", &count);
		iSave = 0;
	}
	//關(guān)閉文件
	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);
	}
}

顯示時(shí)間

//顯示當(dāng)前時(shí)間
void NowTime()
{
	time_t curtime;
	curtime = time(NULL);
	printf("現(xiàn)在的時(shí)間是:%s", ctime(&curtime));
}

測(cè)試

先進(jìn)行添加機(jī)票功能的測(cè)試

如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)

將添加機(jī)票信息輸入完整,然后輸入-1返回主菜單

如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)

然后對(duì)查詢機(jī)票功能進(jìn)行測(cè)試,輸入剛剛添加的航班號(hào),可以看出已經(jīng)查詢到新添加的機(jī)票信息

如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)

然后對(duì)修改機(jī)票信息功能進(jìn)行測(cè)試,我們將目的地修改為beijing

如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)

然后返回主菜單,測(cè)試顯示所有機(jī)票信息的功能

可以看出已經(jīng)成功把1005號(hào)航班的目的地修改為beijing

如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)

對(duì)推薦機(jī)票功能進(jìn)行測(cè)試

選擇你要去的地方和你最早可以到達(dá)時(shí)間,然后會(huì)打印符合條件的機(jī)票信息

如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)

然后對(duì)訂票功能進(jìn)行測(cè)試

如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)

然后對(duì)退票功能進(jìn)行測(cè)試,通過(guò)ID可以定位到剛剛訂票的信息,然后選擇退票

如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)

關(guān)于“如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“如何使用C語(yǔ)言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI