#include<iostream>
#include<fstream>
#include<conio.h>
#include<string>
#include<cstdio>
#include<windows.h>
using namespace std;
struct node //标记结构体-用于标记地铁自助售票机模拟系统基本信息
{
string Num; //地铁线路
string SetoutTime; //发车时间
string BegPlace; //起始站
string EndPlace; //终点站
int Price; //票价
int sum; //售票机总收入
int Capacity; //载客量
int HumNum; //订票人数
node* Next; //下一次操作指针
};
class Busman //操作人员
{
private:
node* BusHead;//结构体指针
public:
Busman() {
BusHead = NULL;
}
void FoundInfo(); //录入地铁信息
void SearchInfo();//浏览地铁信息
void ShowInfo(); //查询路线信息
void SellTicket();//售票功能
void ExitTicket();//退票功能
};
void Busman::FoundInfo()
{
node* p = NULL, * q;//结构体指针
int i, n;//自增变量,线路总数
cout << "\n 录入地铁线路 :\n";
cout << " 记录总数 :";
cin >> n; //记录地铁线路总数
for (i = 0; i < n; i++)
{
q = p;
p = new node;
cout << " 地铁线路 :";
cin >> p->Num;
cout << " 发车时间 : ";
cin >> p->SetoutTime;
cout << " 起始站 :";
cin >> p->BegPlace;
cout << " 终点站 :";
cin >> p->EndPlace;
cout << " 票价 :";
cin >> p->Price;
cout << " 剩余座位 :";
cin >> p->Capacity;
p->HumNum = 0;
// p->sum=p->Price;
p->sum=0;//总收入初始化为0
if (Busman::BusHead == NULL) {
Busman::BusHead = p;
}
else {
q->Next = p;
}
cout << endl;
}
p->Next = NULL;
cout << " 建立完毕 !" << endl;
}
void Busman::ShowInfo()
{
string time; //记录当前时间
node* p;
int number;
//机器编号-假设有5台购票机
while (true) {
cout << "请选择自助售票机:[1-5]" << endl;
cin >> number;
if (1 <= number && number <= 5) {
cout << "正在进入,请等待..." << endl;
cout << "启动成功,请输入..."<<endl;
break;
}
else {
cout << "您的选择有误,请重新输入" << endl;
continue;
}
}
cout << " 当前时间为: ";
cin >> time;
cout << " 地铁线路 " << " " << " 发车时间 " << " " << " 起始站 " << "\t" << " 终点站 " << " " << " 票价 " << "\t" << " 载客量 " << " " << " 订票人数 " << " 发出 " << endl;
for (p = Busman::BusHead; p != NULL; p = p->Next)
{
cout <<" "<< p->Num << "\t" << p->SetoutTime << "\t" << p->BegPlace << "\t" << p->EndPlace << "\t" << p->Price << "\t" << p->Capacity << "\t" << p->HumNum;
if (time >= p->SetoutTime)
{
cout << "( 已发车 )" << endl;
}
else
{
cout << "( 未发车 )" << endl;
}
}
}
void Busman::SearchInfo()
{
int sel; //选择变量
string s; // 查找的地铁线路
node* p;
cout << "(1) 按线路查找 " << endl;
cout << "(2) 起点站查找 " << endl;
cout << "(3) 终电站查找 " << endl;
cout << ">>>> 选择: ";
cin >> sel;
if (sel == 1)
{
cout << " 输入查找的地铁线路: ";
cin >> s;
}
else if (sel == 2)
{
cout << " 输入查找的起始站名称: ";
cin >> s;
}
else if (sel == 3) {
cout << " 输入查找的终点站名称: ";
cin >> s;
}
else {
cout << "输入错误!请重新输入" << endl;
}
cout << " 地铁线路 " << "\t" << " 发车时间 " << " " << " 起始站 " << "\t" << " 终点站 " << " " << " 票价 " << "\t" << " 载客量 " << " " << " 订票人数 " << " 发出 " << endl;
for(p = Busman::BusHead; p != NULL; p = p->Next)
{
if ((sel == 1 && p->Num == s) || (sel == 2 && p->BegPlace == s) || (sel == 3 && p->EndPlace == s))
{
cout << "\t" << p->Num << "\t" << p->SetoutTime << "\t" << p->BegPlace << "\t" << p->EndPlace << "\t" << p->Price << "\t" << p->Capacity << "\t" << p->HumNum;
break;
}
}
if (p == NULL) {
cout << " 未找到! " << endl;
}
}
void Busman::SellTicket()
{
string t, s;
node* p;
cout << " 售票的线路是: ";
cin >> s;
for (p = BusHead; p != NULL; p = p->Next)
{
if (p->Num == s){
break;
}
}
if (p == NULL)
{
cout << " 未找到该线路! " << endl;
return;
}
cout << " 当前时间是: ";
cin >> t;
if (t < p->SetoutTime && p->HumNum + 1 <= p->Capacity)
{
p->HumNum++;
p->sum+=p->Price;
int yourmoney;
//付钱
while (true) {
cout << "请投币(仅支持5元,10元,20元的钞票):";
cin >> yourmoney;
if (yourmoney != 5 && yourmoney != 10 && yourmoney != 20) {
cout << "输入金额有误请重新输入" << endl;
continue;
}
else
break;
}
//添加钱
while (true) {
int addmoney;
if (yourmoney < p->Price) {
while (true) {
cout << "您输入的金额不足,请再添加金额:";
cin >> addmoney;
if (addmoney != 5 && addmoney != 10 && addmoney != 20) {
cout << "输入金额有误请重新输入" << endl;
continue;
}
else
break;
}
yourmoney += addmoney;
}
else
break;
}
int chagemoney = yourmoney - p->Price;
int one = 0;
int five = 0;
int ten = 0;
int twenty = 0;
//计算找零
if (chagemoney != 0) {
while (true) {
if (chagemoney - 20 < 0) {
break;
}
twenty++;
chagemoney -= 20;
}
while (true) {
if (chagemoney - 10 < 0) {
break;
}
ten++;
chagemoney -= 10;
}
while (true) {
if (chagemoney - 5 < 0) {
break;
}
five++;
chagemoney -= 5;
}
while (true) {
if (chagemoney - 1 < 0) {
break;
}
one++;
chagemoney -= 1;
}
int exitmoney=0;
cout << "找您:";
//分步骤找零
/* if (twenty != 0)
cout << twenty << "张20元 ";
if (ten != 0)
cout << ten << "张10元 ";
if (five != 0)
cout << five << "张5元 ";
if (one != 0)
cout << one << "张1元 ";
*/
// 一步找零
if(twenty != 0||ten != 0||one != 0){
exitmoney=twenty*20+ten*10+one*1;
cout<<exitmoney<<"元"<<endl;
}
}
cout <<endl<< " 出票成功!祝您路途愉快 " << endl;
int newseat;
newseat = rand()%1000000; //产生票号
cout<<endl<<"票号:["<<newseat<<"]"<<endl;
cout<<"|------------ ******************** -----------|"<<endl
<<"|------------ 售 票 机 盈 亏 报 表 -----------|"<<endl
<<"|------------ 票机总收入:"<<p->sum<<" -----------|"<<endl
<<"|------------ ******************** -----------|\n"<<endl;
}
else
{
cout << " 该车已发或客载量已满! " << endl;
}
}
void Busman::ExitTicket()
{
string s, t; //退票线路,退票时间
node* p;
cout << " 退票的线路是: ";
cin >> s;
for (p = BusHead; p != NULL; p = p->Next)
if (p->Num == s) break;
if (p == NULL)
{
cout << " 未找到该线路! " << endl;
return;
}
cout << " 当前时间是: ";
cin >> t;
if (t < p->SetoutTime)
{
p->HumNum--;
cout << " 请前往人工服务窗口进行退票! " << endl;
}
else
{
cout << " 该车已发车,无法退票! " << endl;
}
}
//显示菜单
void ShowForm()
{
cout << " 欢迎使用地铁自助售票机模拟系统 " << endl;
cout << " " << endl;
cout << " *******************************" << endl;
cout << " * (1) 录入地铁信息 *" << endl;
cout << " * (2) 浏览地铁信息 *" << endl;
cout << " * (3) 查询路线信息 *" << endl;
cout << " * (4) 地铁自助售票 *" << endl;
cout << " * (5) 地铁自助退票 *" << endl;
cout << " * (0) 退出系统程序 *" << endl;
cout << " *******************************" << endl;
cout << " " << endl;
cout << " 请输入数字进行选择: ";
}
int main()
{
int sel;
Busman BusAdmin;
while (1) //循环
{
system("cls");
ShowForm();
cin >> sel;//选择功能
switch (sel)//选择开关
{
case 1:
BusAdmin.FoundInfo();
break;
case 2:
BusAdmin.ShowInfo();
break;
case 3:
BusAdmin.SearchInfo();
break;
case 4:
BusAdmin.SellTicket();
break;
case 5:
BusAdmin.ExitTicket();
break;
case 0:
return 0;
}
cout << endl;
fflush(stdin);
cout << " 按任意键返回主菜单: " << endl;
getch();//从控制台读取一个字符,但不显示在屏幕上
}
}
课程设计-地铁自助售票机模拟系统
最新推荐文章于 2024-06-17 11:01:02 发布