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

📄 manage.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;
	};
};

Flight flights[100];

int totalFlights;
bool toExit = false;

void Update(){
	ofstream ofile;
	
	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();
}

void Load(){
	cout<<"系统载入中,请等待……"<<endl;
	
	ifstream ifile;
	int count;

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

void ShowAllFlights(){
	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;
}

void AddFlight(){
	string str1,str2,str3;
	int int1;
	cout<<endl<<"请输入航班编号:";
	cin>>str1;
	cout<<endl<<"请输入目的地:";
	cin>>str2;
	cout<<endl<<"请输入起飞时间:";
	cin>>str3;
	cout<<endl<<"请输入最大载客数:";
	cin>>int1;
	flights[totalFlights] = Flight(str1,str2,str3,int1,int1);
	cout<<endl<<"添加成功! 相关信息如下:"<<endl;
	cout<<endl<<" 编号 "<<" 目的地 "<<"  时间 "<<"剩余票数 "<<"最大载客";
	cout<<endl<<"---------------------------------------"<<endl;
	flights[totalFlights].Print();
	totalFlights++;
	Update();
	cout<<endl<<"按 Enter 键返回主界面!"<<endl;
	cin.ignore();
	while (1) {
		if (cin.get() == '\n')
		return;
	}
}

void DeleteFlight(){
	string str;
	bool exist = false;
	int i;
	cout<<endl<<"请输入航班编号:";
	cin>>str;
	for(i = 0; i<totalFlights; i++){
		if ((str == flights[i].flightID) && (flights[i].restTickets == flights[i].capacity)){
			exist = true;
			str = flights[i].flightID;
			break;
		}
	}
	if (!exist) {
		cout<<endl<<endl<<"输入的航班不存在或已有乘客购买!按 Enter 键返回主界面!"<<endl;
		cin.ignore();
		while (1) {
			if (cin.get() == '\n')
			return;
		}
	}
	for(int j = i; j<totalFlights; j++){
		flights[j] = flights[j+1];
	}
	totalFlights--;
	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、退出系统"<<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': ShowAllFlights(); break;
		case '2': AddFlight(); break;
		case '3': DeleteFlight(); break;
		case '4': 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 + -