#include <iostream>
#include <fstream>
#include <string>
#include<time.h>
using namespace std;
void welcome(); //欢迎
void choose(); //选择
void write(); //班次录入
void selectAll(); //浏览班次信息
void buy(); //售票
void quit(); //退出
void select(); //查询
void update(); //修改信息
int loading(); //文件载入子函数
const int MAX = 20; //使数组最大值可调
const int N = 50;
struct time //时间结构体
{
int hour;
int minutes;
};
struct ticket //车票结构体,包含车次,时间结构体,起点,终点,行车时间,额定载客量,已订票人数
{
int Number;
struct time Setout;
char Qidian[MAX];
char Zhongdian[MAX];
float Lasttime;
int Fixnumber;
int Fixednumber;
}car[N];
//主函数
int main()
{
welcome();
return 0;
}
//欢迎子函数
void welcome()
{
cout << "________________________________________________________" << endl;
cout << "| 火 车 票 管 理 系 统 |" << endl;
cout << "| |" << endl;
cout << "| 管理系统有以下功能: |" << endl;
cout << "| |" << endl;
cout << "| 1 班次录入 |" << endl;
cout << "| 2 浏览班次信息 |" << endl;
cout << "| 3 查询火车信息 |" << endl;
cout << "| 4 购买火车票 |" << endl;
cout << "| 5 修改火车信息 |" << endl;
cout << "| 6 退出系统 |" << endl;
cout << "|______________________________________________________|" << endl;
cout << "请输入所要执行功能前的数字: " << endl;
choose();
}
//选择子函数
void choose()
{
string i;
cin >> i;
if (i == "1")
write();
if (i == "2")
selectAll();
if (i == "3")
select();
if (i == "4")
buy();
if (i == "5")
update();
if (i == "6")
quit();
else
{
cout << "输入有误!请在数字1-5之间重新您的输入选择!" << endl;
choose();
}
}
//班次录入子函数
void write()
{
string t = "1";
ofstream outfile;
outfile.open("班次信息.txt", ios::app);
for (int i = 0; t == "1"; i++)
{
cout << "请输入需要添加的火车班次:" << endl;
cin >> car[i].Number;
cout << "请输入此班次发车时间(格式:先输入小时,回车,再输入分钟):" << endl;
cin >> car[i].Setout.hour;
cin >> car[i].Setout.minutes;
cout << "请输入出发站:" << endl;
cin >> car[i].Qidian;
cout << "请输入终点站:" << endl;
cin >> car[i].Zhongdian;
cout << "请输入行车时间:" << endl;
cin >> car[i].Lasttime;
cout << "请输入额定载客量:" << endl;
cin >> car[i].Fixnumber;
cout << "请输入已订票人数:" << endl;
cin >> car[i].Fixednumber;
outfile << car[i].Number << " " << car[i].Setout.hour << " " << car[i].Setout.minutes << " " << car[i].Qidian << " " << car[i].Zhongdian << " " << car[i].Lasttime << " " << car[i].Fixnumber << " " << car[i].Fixednumber << endl;
cout << "是否继续录入?(是请输1,否请输任意键返回主菜单)" << endl;
cin >> t;
if (t != "1")
{
welcome();
}
}
outfile.close();
cout << "输入回车键返回" << endl;
getchar();
getchar();
welcome();
}
//浏览班次子函数
void selectAll()
{
int c;
c = loading();
cout << "班次" << " " << "发车时间" << " " << "起点站" << " " << "终点站" << " " << "行车时间" << " " << "额定载客量" << " " << "已订票人数" << endl;
for (int i = 0; i<c; i++)
{
cout << car[i].Number << " " << car[i].Setout.hour << ":" << car[i].Setout.minutes << " " << car[i].Qidian << " " << car[i].Zhongdian << " " << car[i].Lasttime << " " << car[i].Fixnumber << " " << car[i].Fixednumber << endl;
time_t tval;
struct tm *now;
tval = time(NULL);
now = localtime(&tval);
if ((now->tm_hour == car[i].Setout.hour&&now->tm_min>car[i].Setout.minutes) || (now->tm_hour>car[i].Setout.hour))
cout << "此车已发出" << endl;
}
cout << "输入回车键返回" << endl;
getchar();
getchar();
welcome();
}
//车次查询子函数
void Numbersearch();
void Finalsearch();
void select()
{
string t;
cout << "1.按班次查询" << endl;
cout << "2.按终点站查询" << endl;
cout << "请输入您想选择的查询方法前的数字:" << endl;
cin >> t;
if (t == "1")
Numbersearch();
if (t == "2")
Finalsearch();
if (t != "1" || t != "2")
{
cout << "输入有误,请重新输入" << endl;
select();
}
}
//班次查询子函数
void Numbersearch()
{
loading();
int SearchNumber;
cout << "请输入您需要查找的班次号:" << endl;
cin >> SearchNumber;
cout << "班次" << " " << "发车时间" << " " << "起点站" << " " << "终点站" << " " << "行车时间" << " " << "额定载客量" << " " << "已订票人数" << endl;
cout << car[SearchNumber - 1].Number << " " << car[SearchNumber - 1].Setout.hour << ":" << car[SearchNumber - 1].Setout.minutes << " " << car[SearchNumber - 1].Qidian << " " << car[SearchNumber - 1].Zhongdian << " " << car[SearchNumber - 1].Lasttime << " " << car[SearchNumber - 1].Fixnumber << " " << car[SearchNumber - 1].Fixednumber << endl;
cout << "输入回车键返回" << endl;
getchar();
getchar();
welcome();
}
//终点站查询子函数
void Finalsearch()
{
int R;
R = loading();
int i;
char Address[MAX];
cout << "请输入终点站名称:" << endl;
cin >> Address;
cout << "班次" << " " << "发车时间" << " " << "起点站" << " " << "终点站" << " " << "行车时间" << " " << "额定载客量" << " " << "已订票人数" << endl;
for (i = 0; i<R; i++)
{
if ((strcmp(Address, car[i].Zhongdian)) == 0)
cout << car[i].Number << " " << car[i].Setout.hour << ":" << car[i].Setout.minutes << " " << car[i].Qidian << " " << car[i].Zhongdian << " " << car[i].Lasttime << " " << car[i].Fixnumber << " " << car[i].Fixednumber << endl;
}
cout << "输入回车键返回" << endl;
getchar();
getchar();
welcome();
}
//修改火车信息
void update()
{
ofstream outfile;
outfile.open("班次信息.txt", ios::out);
int SearchNumber;
cout << "请输入需要修改的火车班次:" << endl;
cin >> SearchNumber;
cout << "请输入此班次发车时间(格式:先输入小时,回车,再输入分钟):" << endl;
cin >> car[SearchNumber-1].Setout.hour;
cin >> car[SearchNumber-1].Setout.minutes;
cout << "请输入出发站:" << endl;
cin >> car[SearchNumber-1].Qidian;
cout << "请输入终点站:" << endl;
cin >> car[SearchNumber - 1].Zhongdian;
cout << "请输入行车时间:" << endl;
cin >> car[SearchNumber - 1].Lasttime;
cout << "请输入额定载客量:" << endl;
cin >> car[SearchNumber - 1].Fixnumber;
cout << "请输入已订票人数:" << endl;
cin >> car[SearchNumber - 1].Fixednumber;
for (int i = 0; !outfile.eof(); i++)
{
outfile << car[i].Number << " " << car[i].Setout.hour << " " << car[i].Setout.minutes << " " << car[i].Qidian
<< " " << car[i].Zhongdian << " " << car[i].Lasttime << " " << car[i].Fixnumber << " " << car[i].Fixednumber << endl;
}
outfile.close();
cout << "输入回车键返回" << endl;
getchar();
getchar();
welcome();
}
//购买火车票子函数
void buy()
{
int R;
R = loading();
int i;
cout << "请输入要订购的车票的班次:" << endl;
cin >> i;
if (i <= 0 || i>R)
{
cout << "对不起,今天没有这趟车,请明天再来,谢谢使用!" << endl;
cout << "输入回车键返回" << endl;
getchar();
getchar();
welcome();
}
time_t tval;
struct tm *now;
tval = time(NULL);
now = localtime(&tval);
if ((now->tm_hour == car[i - 1].Setout.hour&&now->tm_min<car[i - 1].Setout.minutes) || (now->tm_hour<car[i - 1].Setout.hour))
{
if (car[i - 1].Fixednumber<car[i - 1].Fixnumber)
{
(car[i - 1].Fixednumber)++;
ofstream outfile;
outfile.open("班次信息.txt", ios::out);
for (i = 0; i<R; i++)
{
outfile << car[i].Number << " " << car[i].Setout.hour << " " << car[i].Setout.minutes << " " << car[i].Qidian << " " << car[i].Zhongdian << " " << car[i].Lasttime << " " << car[i].Fixnumber << " " << car[i].Fixednumber << endl;
}
outfile.close();
cout << "你的订票成功,请按时上车,谢谢使用!" << endl;
}
else cout << "对不起,今天的这趟车的票已卖完,请明天再来,谢谢合作" << endl;
}
else cout << "对不起,今天的这趟车已发出,请明天再来,谢谢合作" << endl;
cout << "输入回车键返回" << endl;
getchar();
getchar();
welcome();
}
//退出子函数
void quit()
{
cout << "*********************谢谢使用本系统,欢迎下次继续使用***********************" << endl;
}
//文件载入子函数
int loading()
{
int i;
ifstream infile;
infile.open("班次信息.txt", ios::in);
if (!infile)
{
cout << "请查看原文件是否存在!" << endl;
cout << "按任意键返回";
getchar();
getchar();
welcome();
}
for (i = 0; !infile.eof(); i++)
{
infile >> car[i].Number >> car[i].Setout.hour >> car[i].Setout.minutes >> car[i].Qidian >> car[i].Zhongdian >> car[i].Lasttime >> car[i].Fixnumber >> car[i].Fixednumber;
}
return i - 1;
}