⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sale.cpp

📁 飞机订票系统
💻 CPP
字号:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Flight{
public:
	string flightID;
	string destination;
	string leaveTime;
	int restTickets;
	int capacity;
	Flight(){
	};
	Flight(string ID, string dest, string time, int rest, int cap){
		flightID = ID;
		destination = dest;
		leaveTime = time;
		restTickets = rest;
		capacity = cap;
	}
	void Dec(){
		restTickets--;
	};
	void Print(){
		cout<<flightID<<'\t'<<destination<<'\t'<<leaveTime<<'\t'<<restTickets<<'\t'<<capacity<<endl;
	};
};

class Ticket{
public:
	string name;
	string ticketID;
	string flightID;
	string destination;
	string leaveTime;
	string sex;
	Ticket(){
	};
	Ticket(string tmpTicketID, string tmpFlightID, string dest, string time, string tmpName, string tmpSex){
		ticketID = tmpTicketID;
		flightID = tmpFlightID;
		destination = dest;
		leaveTime = time;
		name = tmpName;
		sex = tmpSex;
	};
	void Print(){
		cout<<ticketID<<'\t'<<flightID<<'\t'<<destination<<'\t'<<leaveTime<<'\t'<<name<<'\t'<<sex<<endl;
	}
};

Flight flights[100];
Ticket tickets[1000];

int totalFlights, totalTickets;
string maxTicketID;
bool toExit = false;

//用内存中数据重写数据文件
void Update(){
	ofstream ofile;
	
	//更新 flights.dat
	ofile.open("flights.dat");
	ofile<<totalFlights<<endl;
	for(int i = 0; i<totalFlights; i++){
		ofile<<flights[i].flightID<<'\t'<<flights[i].destination<<'\t'<<flights[i].leaveTime<<endl;
		ofile<<flights[i].restTickets<<'\t'<<flights[i].capacity<<endl;
	}
	ofile.close();


	//更新 tickets.dat

	ofile.open("tickets.dat");
	ofile<<maxTicketID<<endl;
	ofile<<totalTickets<<endl<<endl;
	for(int i = 0; i<totalTickets; i++){
		ofile<<tickets[i].ticketID<<endl<<tickets[i].flightID<<endl<<tickets[i].destination<<endl<<tickets[i].leaveTime<<endl<<tickets[i].name<<endl<<tickets[i].sex<<endl<<endl;
	}
	ofile.close();
}

//根据现有最大机票编号产生新机票编号
char* GenerateID(char* str){
	if (str[4] == '9') {
		str[4] = '0';
		if (str[3] == '9'){
			str[3] = '0';
			if (str[2] == '9'){
				str[2] = '0';
				if (str[1] == '9'){
					str[1] = '0';
					str[0]++;
				}
				else{
					str[1]++;
				}
			}
			else{
				str[2]++;
			}
		}
		else{
			str[3]++;
		}
	}
	else{
		str[4]++;
	}
	return str;
}

//程序开始执行时将文件中的数据载入到两个数组中
void Load(){
	cout<<"系统载入中,请等待……"<<endl;
	
	ifstream ifile;
	int count;

	//开始往 flights 数组读入 count 个 Flight 对象
	ifile.open("flights.dat");
	ifile>>count;
	ifile.ignore();

	for(int k = 0; k<count; k++){

		string str1 = "", str2 = "", str3 = "";
		int int1, int2, i;
		char buffer[100], chbuffer[100];
	
		for(i = 0; i<6; i++)
			str1 += ifile.get();
		ifile.ignore();

		for(int i=0; i<sizeof(buffer); i++)
			buffer[i] = '\0';
		for(int i=0; i<sizeof(chbuffer); i++)
			chbuffer[i] = '\0';
		ifile.getline(buffer,100,'\n');
		i = 0;
		while (buffer[i] & 0x80){
			strncat(chbuffer,buffer+i,2);
			i += 2;
		}
		str2 = chbuffer;
	
		i++;
		for(int j = 0; j<5; j++)
			str3 += buffer[i++];
	
		ifile>>int1>>int2;
		ifile.ignore();

		flights[k] = Flight(str1,str2,str3,int1,int2);
	}
	totalFlights = count;
	ifile.close();

	//往 tickets 数组读入 count 个 Ticket 对象
	ifile.open("tickets.dat");
	ifile>>maxTicketID;
	ifile>>count;
	for(int i = 0; i<count; i++){
		string strs[6];
		for(int j = 0; j<6; j++)
			ifile>>strs[j];
		tickets[i] = Ticket(strs[0],strs[1],strs[2],strs[3],strs[4],strs[5]);
	}
	totalTickets = count;
	ifile.close();
	system("cls");
}

//三种航班查询方式:按航班号查询、按目的地查询、按起飞时间查询
void SearchFlightByFlightID(){
	cout<<endl<<"请输入航班号:";
	string tmp;
	cin>>tmp;
	cin.ignore();
	cout<<endl<<"查询结果:"<<endl;
	cout<<endl<<" 编号 "<<" 目的地 "<<"  时间 "<<"剩余票数 "<<"最大载客";
	cout<<endl<<"---------------------------------------"<<endl;
	int sum = 0;
	for(int i = 0; i<totalFlights; i++){
		if (flights[i].flightID == tmp){
			flights[i].Print();
			sum++;
		}
	}
	cout<<endl<<"搜索完毕,共找到 "<<sum<<" 条记录   按 Enter 键继续……"<<endl;
	while (1) {
		if (cin.get() == '\n')
			return;
	}
}

void SearchFlightByDestination(){
	cout<<endl<<"请输入目的地:";
	string tmp;
	cin>>tmp;
	cin.ignore();
	cout<<endl<<"查询结果:"<<endl;
	cout<<endl<<" 编号 "<<" 目的地 "<<"  时间 "<<"剩余票数 "<<"最大载客";
	cout<<endl<<"---------------------------------------"<<endl;
	int sum = 0;
	for(int i = 0; i<totalFlights; i++){
		if (flights[i].destination == tmp){
			flights[i].Print();
			sum++;
		}
	}
	cout<<endl<<"搜索完毕,共找到 "<<sum<<" 条记录   按 Enter 键继续……"<<endl;
	while (1) {
		if (cin.get() == '\n')
			return;
	}
}

void SearchFlightByTime(){
	cout<<endl<<"请输入起飞时间(格式如 HH:MM 24小时制):";
	string tmp;
	cin>>tmp;
	cin.ignore();
	cout<<endl<<"查询结果:"<<endl;
	cout<<endl<<" 编号 "<<" 目的地 "<<"  时间 "<<"剩余票数 "<<"最大载客";
	cout<<endl<<"---------------------------------------"<<endl;
	int sum = 0;
	for(int i = 0; i<totalFlights; i++){
		if (flights[i].leaveTime == tmp){
			flights[i].Print();
			sum++;
		}
	}
	cout<<endl<<"搜索完毕,共找到 "<<sum<<" 条记录   按 Enter 键继续……"<<endl;
	while (1) {
		if (cin.get() == '\n')
			return;
	}
}

//航班查询方式选择
void SearchFlight(){
	system("cls");
	cout<<endl<<"--------------------------  航班信息查询  --------------------------"<<endl;
	cout<<endl<<"1、按航班号查询 2、按目的地查询 3、按起飞时间查询 4、返回主界面"<<endl<<endl<<"5、显示所有航班信息  请选择操作,输入对应数字并按 Enter 键确定:";
	string str;
	cin>>str;
	if (str[1] != '\0'){
		cout<<endl<<"输入无效,按 Enter 键返回主界面!"<<endl;
		cin.ignore();
		while (1) {
			if (cin.get() == '\n')
			return;
		}
	}
	switch (str[0]) {
		case '1': SearchFlightByFlightID(); break;
		case '2': SearchFlightByDestination(); break;
		case '3': SearchFlightByTime(); break;
		case '4': return;
		case '5': {
					cout<<endl<<" 编号 "<<" 目的地 "<<"  时间 "<<"剩余票数 "<<"最大载客";
					cout<<endl<<"---------------------------------------"<<endl;
					for(int i = 0; i<totalFlights; i++)
						flights[i].Print();
					cout<<endl<<"共 "<<totalFlights<<" 条记录   按 Enter 键继续……"<<endl;
					cin.ignore();
					cin.get();
					return;
				  }
		default: {
					cout<<endl<<"输入无效,按 Enter 键返回主界面!"<<endl;
					cin.ignore();
					while (1) {
						if (cin.get() == '\n')
						return;
					}
				 }
	}
}

//机票购买
void Buy(){
	system("cls");
	cout<<endl<<"--------------------------  机票购买  --------------------------"<<endl;
	string tmpName,tmpSex,tmpTicketID = "",tmpFlightID,tmpDest,tmpTime;
	bool exist = false;
	cout<<"请输入航班编号:";
	cin>>tmpFlightID;
	for(int i = 0; i<totalFlights; i++){
		if (flights[i].flightID == tmpFlightID){
			if (flights[i].restTickets > 0)
				exist = true;
			flights[i].Dec();
			tmpDest = flights[i].destination;
			tmpTime = flights[i].leaveTime;
			break;
		}
	}
	if (!exist) {
		cout<<endl<<endl<<"输入的机票不存在或已售完!按 Enter 键返回主界面!"<<endl;
		cin.ignore();
		while (1) {
			if (cin.get() == '\n')
			return;
		}
	}
	cout<<"请输入姓名:";
	cin>>tmpName;
	cout<<"请输入性别:";
	cin>>tmpSex;
	char tmpChar[6];
	for(int i = 0; i<maxTicketID.length(); i++){
		tmpChar[i] = maxTicketID[i];
	}
	GenerateID(tmpChar);
	for(int i = 0; i<5; i++){
		tmpTicketID += tmpChar[i];
	}
	maxTicketID = tmpTicketID;
	tickets[totalTickets] = Ticket(tmpTicketID,tmpFlightID,tmpDest,tmpTime,tmpName,tmpSex);
	totalTickets++;
	Update();
	cout<<endl<<endl<<"机票购买成功! 相关信息如下:"<<endl;
	cout<<endl<<"机票号 "<<" 航班号 "<<"目的地"<<" 起飞时间 "<<" 姓名   "<<"性别";
	cout<<endl<<"-------------------------------------------"<<endl;
	tickets[totalTickets-1].Print();
	cout<<endl<<"按 Enter 键返回主界面!"<<endl;
	cin.ignore();
	while (1) {
		if (cin.get() == '\n')
		return;
	}
}

//三种机票查询方式:按姓名查询、按机票号查询、按航班号查询
void SearchTicketByName(){
	cout<<endl<<"请输入姓名:";
	string tmp;
	cin>>tmp;
	cin.ignore();
	cout<<endl<<"查询结果:"<<endl;
	cout<<endl<<"机票号 "<<" 航班号 "<<"目的地"<<" 起飞时间 "<<" 姓名   "<<"性别";
	cout<<endl<<"-------------------------------------------"<<endl;
	int sum = 0;
	for(int i = 0; i<totalTickets; i++){
		if (tickets[i].name == tmp){
			tickets[i].Print();
			sum++;
		}
	}
	cout<<endl<<"搜索完毕,共找到 "<<sum<<" 条记录   按 Enter 键继续……"<<endl;
	while (1) {
		if (cin.get() == '\n')
			return;
	}
}

void SearchTicketByTicketID(){
	cout<<endl<<"请输入机票编号:";
	string tmp;
	cin>>tmp;
	cin.ignore();
	cout<<endl<<"查询结果:"<<endl;
	cout<<endl<<"机票号 "<<" 航班号 "<<"目的地"<<" 起飞时间 "<<" 姓名   "<<"性别";
	cout<<endl<<"-------------------------------------------"<<endl;
	int sum = 0;
	for(int i = 0; i<totalTickets; i++){
		if (tickets[i].ticketID == tmp){
			tickets[i].Print();
			sum++;
		}
	}
	cout<<endl<<"搜索完毕,共找到 "<<sum<<" 条记录   按 Enter 键继续……"<<endl;
	while (1) {
		if (cin.get() == '\n')
			return;
	}
}

void SearchTicketByFlightID(){
	cout<<endl<<"请输入航班编号:";
	string tmp;
	cin>>tmp;
	cin.ignore();
	cout<<endl<<"查询结果:"<<endl;
	cout<<endl<<"机票号 "<<" 航班号 "<<"目的地"<<" 起飞时间 "<<" 姓名   "<<"性别";
	cout<<endl<<"-------------------------------------------"<<endl;
	int sum = 0;
	for(int i = 0; i<totalTickets; i++){
		if (tickets[i].flightID == tmp){
			tickets[i].Print();
			sum++;
		}
	}
	cout<<endl<<"搜索完毕,共找到 "<<sum<<" 条记录   按 Enter 键继续……"<<endl;
	while (1) {
		if (cin.get() == '\n')
			return;
	}
}

//机票查询方式选择
void SearchTicket(){
	system("cls");
	cout<<endl<<"--------------------------  机票信息查询  --------------------------"<<endl;
	cout<<endl<<"1、按姓名查询 2、按机票编号查询 3、按航班编号查询 4、返回主界面"<<endl<<endl<<"5、显示所有机票信息  请选择操作,输入对应数字并按 Enter 键确定:";
	string str;
	cin>>str;
	if (str[1] != '\0'){
		cout<<endl<<"输入无效,按 Enter 键返回主界面!"<<endl;
		cin.ignore();
		while (1) {
			if (cin.get() == '\n')
			return;
		}
	}
	switch (str[0]) {
		case '1': SearchTicketByName(); break;
		case '2': SearchTicketByTicketID(); break;
		case '3': SearchTicketByFlightID(); break;
		case '4': return;
		case '5': {
					cout<<endl<<"机票号 "<<" 航班号 "<<"目的地"<<" 起飞时间 "<<" 姓名   "<<"性别";
					cout<<endl<<"-------------------------------------------"<<endl;
					for(int i = 0; i<totalTickets; i++)
						tickets[i].Print();
					cout<<endl<<"共 "<<totalTickets<<" 条记录   按 Enter 键继续……"<<endl;
					cin.ignore();
					cin.get();
					return;
				  }
		default: {
					cout<<endl<<"输入无效,按 Enter 键返回主界面!"<<endl;
					cin.ignore();
					while (1) {
						if (cin.get() == '\n')
						return;
					}
				 }
	}
}

//退订机票
void Refund(){
	system("cls");
	cout<<endl<<"--------------------------  机票退订  --------------------------"<<endl;
	cout<<endl<<"请输入需退订的机票编号:";
	string str;
	bool exist = false;
	int i;
	cin>>str;
	for(i = 0; i<totalTickets; i++){
		if (str == tickets[i].ticketID){
			exist = true;
			str = tickets[i].flightID;
			break;
		}
	}
	if (!exist) {
		cout<<endl<<endl<<"输入的机票不存在!按 Enter 键返回主界面!"<<endl;
		cin.ignore();
		while (1) {
			if (cin.get() == '\n')
			return;
		}
	}
	for(int j = i; j<totalTickets; j++){
		tickets[j] = tickets[j+1];
	}
	totalTickets--;
	for(int j = 0; j<totalFlights; j++){
		if (str == flights[i].flightID){
			flights[i].Dec();
			break;
		}
	}
	Update();
	cout<<endl<<endl<<"机票已成功退订!按 Enter 键返回主界面!"<<endl;
	cin.ignore();
	while (1) {
		if (cin.get() == '\n')
		return;
	}
}

//退出系统
void Exit(){
	extern bool toExit;
	toExit = true;
}

//主界面
void SelectFunction(){
	system("cls");
	cout<<endl<<"--------------------------  飞机票售票系统主选单  --------------------------"<<endl;
	cout<<endl<<"1、航班信息查询 2、机票购买 3、机票信息查询 4、机票退购 5、退出系统"<<endl<<endl<<"请选择操作,输入对应数字并按 Enter 键确定:";
	string str;
	cin>>str;
	if (str[1] != '\0'){
		cout<<endl<<"输入无效,按 Enter 键重新选择!"<<endl;
		cin.ignore();
		while (1) {
			if (cin.get() == '\n')
			return;
		}
	}
	switch (str[0]) {
		case '1': SearchFlight(); break;
		case '2': Buy(); break;
		case '3': SearchTicket(); break;
		case '4': Refund(); break;
		case '5': Exit(); return;
		default : {
					cout<<endl<<"输入无效,按 Enter 键重新选择!"<<endl;
					cin.ignore();
					while (1) {
						if (cin.get() == '\n')
						return;
					}
				 }
	}
}

int main(){
	Load();
	while (!toExit) {
		SelectFunction();
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -