代码如下,如有帮助,请采纳一下,谢谢。
#include <iostream>
#include <string>
using namespace std;
//定义航班最大数量
#define MAXNMB 20
//定义航班信息
struct HangbanInfo
{
char id[10]; //航班号
char start[20];//起点站
char end[20]; //终点站
char bq[20]; //班期
char qfsj[6]; //起飞时间
char ddsj[6]; //到达时间
char jx[10]; //机型
int price; //票价
};
//新增航班
void Add(HangbanInfo info[],int &n)
{
HangbanInfo a;
char c;
system("cls");
cout << "请输入航班号:" ;
cin >> a.id;
cout << "请输入起点站:";
cin >> a.start;
cout << "请输入终点站:";
cin >> a.end;
cout << "请输入班期:";
cin >> a.bq;
cout << "请输入起飞时间:";
cin >> a.qfsj;
cout << "请输入到达时间:";
cin >> a.ddsj;
cout << "请输入机型:";
cin >> a.jx;
cout << "请输入票价:";
cin >> a.price;
info[n] = a;
n += 1;
cout << "航班信息添加成功"<< endl;
cout << "按任意键返回上一菜单..."<< endl;
cin.get(c);
cin.get(c);
}
//修改
void Mod(HangbanInfo info[],int n)
{
int i;
string tmp;
HangbanInfo a;
char c;
system("cls");
cout << "请输入您要修改的航班号信息:";
cin >> tmp;
for (i = 0;i<n;i++)
{
if( tmp.compare(info[i].id) == 0)
{
cout << "请输入航班号:" ;
cin >> a.id;
cout << "请输入起点站:";
cin >> a.start;
cout << "请输入终点站:";
cin >> a.end;
cout << "请输入班期:";
cin >> a.bq;
cout << "请输入起飞时间:";
cin >> a.qfsj;
cout << "请输入到达时间:";
cin >> a.ddsj;
cout << "请输入机型:";
cin >> a.jx;
cout << "请输入票价:";
cin >> a.price;
info[i] = a;
cout << "修改成功!" <<endl;
break;
}
}
if(i==n)
cout << "没有找到该航班信息"<< endl;
cout << "按任意键返回上一菜单..."<< endl;
cin.get(c);
cin.get(c);
}
//删除
void DeleteHb(HangbanInfo info[],int &n)
{
int i,j;
string tmp;//[10] = {0};
char c;
system("cls");
cout << "请输入您要删除的航班号:";
cin >> tmp;
for (i = 0;i<n;i++)
{
if(tmp.compare(info[i].id) == 0)
{
for (j = i;j<n-1;j++)
{
info[j] = info[j+1];
}
n = n-1;
cout << "航班信息删除成功!"<<endl;
}
}
if(i == n)
cout << "没有找到该航班信息"<<endl;
cout << "按任意键返回上一菜单..."<< endl;
cin.get(c);
cin.get(c);
}
//查找
void Find(HangbanInfo info[],int n)
{
int i = 0;
string tmp;
char c;
bool isfind = false;
system("cls");
cout << "请输入您要查找的航班信息:";
cin >> tmp;
for (i=0;i<n;i++)
{
if( (tmp.compare(info[i].id)==0) || (tmp.compare(info[i].start)==0) || (tmp.compare(info[i].end)==0) || (tmp.compare(info[i].qfsj)==0) || (tmp.compare(info[i].ddsj)==0))
{
cout << "航班号:" << info[i].id <<endl;
cout << "起点站:" << info[i].start <<endl;
cout << "终点站:" << info[i].end << endl;
cout << "班期:" << info[i].bq << endl;
cout << "起飞时间:" <<info[i].qfsj << endl;
cout << "到达时间:" <<info[i].ddsj <<endl;
cout << "机型:" << info[i].jx << endl;
cout << "票价:" << info[i].price << endl;
isfind = true;
}
}
if(!isfind)
cout << "未找到符合信息的航班" << endl;
cout << "按任意键返回上一菜单..."<< endl;
cin.get(c);
cin.get(c);
}
//排序
void Sort(HangbanInfo info[],int n)
{
int i,j;
int opt;
HangbanInfo tmp;
char c;
string strtmp;
system("cls");
cout << "1.按照起飞时间排序 2.按照到达时间排序 3.按照价格排序" << endl;
cin >> opt;
for (i = 0; i<n-1;i++)
{
for (j = 0;j<n-1-i;j++)
{
if (opt == 1)
{
strtmp = info[j].qfsj;
if(strtmp.compare(info[j+1].qfsj) > 0 )
{
tmp = info[j];
info[j] = info[j+1];
info[j+1] = tmp;
}
}else if (opt == 2)
{
strtmp = info[j].ddsj;
if(strtmp.compare(info[j+1].ddsj) > 0 )
{
tmp = info[j];
info[j] = info[j+1];
info[j+1] = tmp;
}
}else
{
if( info[j].price >info[j+1].price )
{
tmp = info[j];
info[j] = info[j+1];
info[j+1] = tmp;
}
}
}
}
cout << "排序后航班信息:"<< endl;
for (i = 0;i<n;i++)
{
cout << info[i].id << "\t" << info[i].start << "\t" << info[i].end << "\t" << info[i].bq << "\t";
cout << info[i].qfsj << "\t"<< info[i].ddsj << "\t" << info[i].jx << "\t" << info[i].price << endl;
}
cout << "按任意键返回上一菜单..."<< endl;
cin.get(c);
cin.get(c);
}
int main()
{
HangbanInfo info[MAXNMB];//存储所有航班信息
int nmbHb = 0; //航班实际数量
int opt = 0;
int bgo = 1;
while(bgo)
{
system("cls");
cout << "------------航班信息查询系统--------------"<<endl;
cout << "| 1.新增航班 |"<< endl;
cout << "| 2.修改航班信息 |"<< endl;
cout << "| 3.删除航班 |"<< endl;
cout << "| 4.查询航班信息 |"<< endl;
cout << "| 5.航班信息排序 |"<< endl;
cout << "| 9.退出系统 |"<< endl;
cout << "------------------------------------------"<< endl;
cin >> opt;
switch(opt)
{
case 1:
Add(info,nmbHb);
break;
case 2:
Mod(info,nmbHb);
break;
case 3:
DeleteHb(info,nmbHb);
break;
case 4:
Find(info,nmbHb);
break;
case 5:
Sort(info,nmbHb);
break;
case 9:
bgo = 0;
break;
}
}
return 0;
}