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

📄 manager.cpp

📁 模拟铁路售票系统
💻 CPP
字号:
#include "manager.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string kong;
void manager::read(void){
	ifstream file1;
	char c;
	rn=new railnum[20];
	int i=0;
	string str1=kong;
	city * current;
	current=rn[num].shifa;
	file1.open("1.txt");//打开1.txt
	if(!file1){
		cerr<<"打开文件错误,可能是该文件不存在"<<endl;
		exit (1);
	}
	else{
		while(file1.get(c)){
			if(c!='\n'){
				if(c!=' '){
					str1=str1+c;//读出一个字段
				}
				else
				{
					if(i==0)
						rn[num].railname=str1;//建立车次名称
					if(i==1)
						rn[num].tickets=str1[0]-48;//建立车次的票数
					if(i!=0&&i!=1){
						if(i%4==2){
							city * newcity=new city;//新建停靠站节点
							newcity->name=str1;
							if(i==2)
								rn[num].shifa=newcity;
							else
								current->next=newcity;
							current=newcity;
						}
						if(i%4==3)
							current->price=(str1[0]-48)*100+(str1[1]-48)*10+(str1[2]-48);//录入票价
						if(i%4==0)
							current->reachtime=str1;//录入到站时间
						if(i%4==1)
							current->launchtime=str1;//录入离站时间
					}
					i=i+1;
					str1=kong;//写入后清空str1
				}
			}
			else{
				num=num+1;
				i=0;
				current=rn[num].shifa;
			}
		}
	}
	file1.close();
}
void manager::write(void){
	ofstream file;
	file.open("1.txt");
	if(!file){
		cerr<<"打开文件错误,可能是该文件不存在"<<endl;
		exit (1);
	}
	else{
		for(int i=0;i<=num;i++){
			file<<rn[i].railname<<' '<<rn[i].tickets<<' ';
			city * current=rn[i].shifa;
			while(current!=NULL){
				if(current->price<10)
					file<<current->name<<' '<<"00"<<current->price<<' '<<current->reachtime<<' '<<current->launchtime<<' ';
				if(current->price>=10&&current->price<100)
					file<<current->name<<' '<<'0'<<current->price<<' '<<current->reachtime<<' '<<current->launchtime<<' ';
				if(current->price>100)
					file<<current->name<<' '<<current->price<<' '<<current->reachtime<<' '<<current->launchtime<<' ';
				current=current->next;
			}
			if(i<num)
				file<<'\n';
		}
	}
	file.close();
}
void manager::adminpw(void){//修改管理员密码
	string str1,str2,passwd;

	bool flag=1;
	while(flag){
		cout<<"请输入新的密码"<<endl;
		cin>>str1;
		cout<<"再次输入新的密码并确认"<<endl;
		cin>>str2;
		if(str1==str2){
			ofstream file;
			file.open("admin.txt");//打开密码文件
			file.close();
			file<<str1;
			cout<<"密码修改成功!"<<endl;
			flag=0;
		}	
		else{
			cout<<"两次密码输入不一致!接着更改吗?Y/N"<<endl;
			char c;
			cin>>c;
			if(c=='y')
				flag=1;
			else
				flag=0;
		}
	}
}
void manager::update(void){//更新车次信息
	int k;
	string temp;
	string str;
	city *newcity=NULL;
	city *sf;
	int e=0;
	cout<<"退出<0>;修改车次信息<1>;增加车次<2>;删除车次<3>:"<<endl;
	cin>>k;
	if(k==1)
	{
		cout<<"请输入要修改的车次:"<<endl;
		cin>>temp;
		for(int i=0;i<=num;i++)
		{
			e=0;
			if(rn[i].railname==temp)
			{
				e=1;
				sf=new city;
				rn[i].shifa=sf;
				cout<<"请输入车次信息(结束输入请在末尾输入stop):"<<endl;
				cin>>rn[i].tickets;
				cin>>str;
				while(str!="stop")
				{
					sf->name=str;
					str=kong;

					cin>>str;
					sf->price=((str[0]-'0')*100+(str[1]-'0')*10+(str[2]-'0'));
					str=kong;

					cin>>str;
					sf->reachtime=str;
					str=kong;

					cin>>str;
					sf->launchtime=str;
					str=kong;
					cin>>str;
					if(str!="stop")
					    sf=sf->next=new city;
				}
				break;
			}

		}
		if(e==0)
			cout<<"您输入的车次有误  请查证后再操作!"<<endl;
		else
		{
			write();
			cout<<"修改成功"<<endl<<endl;
		}
	}

	if(k==2)
	{
		num=num+1;
		str=kong;
		temp=kong;
		newcity=NULL;
		sf=new city;
		rn[num].shifa=sf;
		cout<<"请输入要增加的车次:"<<endl;
		cin>>temp;
		for(int i=0;i<=num;i++)
		{
			e=0;
			if(rn[i].railname==temp)
			{
				e=1;
				break;
			}
		}
		if(e==1)
			cout<<"您要增加的车次已存在 请查证后再操作"<<endl<<endl;
		else
		{
		rn[num].railname=temp;
		cout<<"请输入车次信息(结束请在末尾输入stop):"<<endl;
		cin>>rn[num].tickets;
		cin>>str;
		while(str!="stop")
		{
			sf->name=str;
			str=kong;

			cin>>str;
			sf->price=((str[0]-'0')*100+(str[1]-'0')*10+(str[2]-'0'));
			str=kong;

			cin>>str;
			sf->reachtime=str;
			str=kong;

			cin>>str;
			sf->launchtime=str;
			str=kong;
			cin>>str;
			if(str!="stop")
			    sf=sf->next=new city;
		}
		write();
		cout<<"增加成功"<<endl<<endl;
		}
	}

	if(k==3)
	{
		e=0;
		temp=kong;
		cout<<"请输入要删除的车次:"<<endl;
		cin>>temp;
		for(int i=0;i<=num;i++)
		{
			e=0;
			if(rn[i].railname==temp)
			{
				e=1;
				for(int j=i;j<=num;j++)
				{
					rn[j]=rn[j+1];
				}
				num=num-1;
				break;
			}
		}
		if(e==0)
			cout<<"您输入的车次有误 请查证后再操作"<<endl<<endl;
		else
		{
	    	write();
		    cout<<"删除成功"<<endl<<endl;
		}
	}



}
//车次查询函数
void manager::getrail()
{
	int exist=0;
	cout<<"请输入车次:"<<endl;
	string rail;
	cin>>rail;
	city *tem;

	for(int i=0;i<=num;i++)
	{
		if(rn[i].railname==rail)
		{	cout<<"车次:"<<rn[i].railname<<"  剩余票数:"<<rn[i].tickets<<endl;
			cout<<"到达时间  停靠站  发车时间  票价"<<endl;
			tem=rn[i].shifa;
			while(tem!=NULL)
			{
				cout<<"  "<<tem->reachtime<<"     "<<tem->name<<"      "<<tem->launchtime<<"     "<<tem->price<<endl;
				tem=tem->next;
			}

			exist=1;
			break;
		}
	}

	if(exist==0)
		cout<<"对不起,您要查询的车次不存在,请查证后再查询"<<endl;
}
//站间查询函数
void manager::findway()
{
	int exist;
	exist=0;
	string begin;
	string end;
	cout<<"输入出发地点:"<<endl;
	cin>>begin;
	cout<<"输入目的地:"<<endl;
	cin>>end;

	
	findway(begin,end,exist);
	if(exist==0)
	{
		cout<<"没有直达车次"<<endl;

		findway(begin,end);
	}
}
//直达车次查询
void manager::findway(string begin,string end,int &ex)
{
	city *tem=NULL;
	city *beg=NULL;
	city *en=NULL;
	for(int i=0;i<num+1;i++)
	{
		tem=rn[i].shifa;
		while(tem!=NULL)
		{
			if(tem->name==begin)
			{
				beg=tem;
				break;
			}
			tem=tem->next;
		}
		if(beg!=NULL)
		{
			while(tem!=NULL)
			{
				if(tem->name==end)
				{
					en=tem;
					cout<<rn[i].railname<<"剩余票数"<<rn[i].tickets<<"  "<<beg->reachtime<<"   "<<begin<<"  "<<beg->launchtime<<"------"<<en->reachtime<<"   "<<
						en->name<<"   "<<en->price-beg->price<<endl;
					cout<<endl;
					ex=1;
					break;
				}
				tem=tem->next;
			}
		}
	}
}
//站间查询
void manager::findway(string begin,string end)
{
	int exist=0;
	city *tem=NULL;
	city *beg=NULL;
	for(int i=0;i<num+1;i++)
	{

		tem=rn[i].shifa;
		while(tem!=NULL)
		{
		
			if(tem->name==begin)
			{
				beg=tem->next;
				break;
			}
			tem=tem->next;
		}
		while(beg!=NULL)
		{
			exist=0;
			city *temp=NULL;
	        city *begi=NULL;
	        city *e=NULL;
	        for(int j=0;j<num+1;j++)
			{
				exist=0;
	        	temp=rn[j].shifa;
		        while(temp!=NULL)
				{
		        	if(temp->name==beg->name)
					{
						begi=temp;
			        	break;
					}
		        	temp=temp->next;
				}
	        	if(begi!=NULL)
				{
		        	while(temp!=NULL)
					{
			        	if(temp->name==end)
						{
			        		e=temp;
			        	//	cout<<rn[i].railname<<"   "<<begi->reachtime<<"   "<<beg->name<<"  "<<begi->launchtime<<"  "<<e->reachtime<<"   "<<
			     		//	e->name<<endl;
			        		exist=1;
			        		break;
						}
			        	temp=temp->next;
					}
				
				}
				if(exist==1)
				{	
					if(i!=j){
		    			cout<<rn[i].railname<<"剩余票数"<<rn[i].tickets<<"  "<<tem->reachtime<<"   "<<tem->name<<"   "<<tem->launchtime<<"------"<<beg->reachtime<<"   "<<beg->name<<endl;
		    			cout<<rn[j].railname<<"剩余票数"<<rn[j].tickets<<"  "<<begi->reachtime<<"   "<<begi->name<<"   "<<begi->launchtime<<"------"<<e->reachtime<<"   "<<e->name<<endl;
						cout<<endl;
					}
				}
			}
			beg=beg->next;
		}
	}
}
void manager::reserve(void){
	cout<<"请输入您要订的车次"<<endl;
	string str;
	cin>>str;
	bool flag=0;
	for(int i=0;i<=num;i++){
		if(rn[i].railname==str){
			cout<<"剩余票数:"<<rn[i].tickets<<"请输入您的出发地和目的地"<<endl;
			string str1,str2;
			cin>>str1;
			cin>>str2;
			city* c1;city * c2;
			c1=c2=rn[i].shifa;
			while(c1!=NULL){
				if(c1->name!=str1)
					c1=c1->next;
				else
					break;
			}
			while(c2!=NULL){
				if(c2->name!=str2)
					c2=c2->next;
				else
					break;
			}
			if((c1==NULL)||(c2==NULL)){
				cout<<"您输入的信息有错误,请查证后再输入"<<endl;
			}
			else{
				cout<<"您将要从"<<str1<<"去往"<<str2<<",票价为"<<c2->price-c1->price<<",请输入您要的票数"<<endl;
				int p;
				cin>>p;
				if(p>rn[i].tickets){
					cout<<"对不起,目前剩余票数不能满足您的需求,下面是我们的查询系统为您查出的可以完成您的旅程的车次,请选择购买"<<endl;
					int i;
					findway(str1,str2,i);
					findway(str1,str2);
				}
				else{
					rn[i].tickets=rn[i].tickets-p;
					write();
					cout<<"订票成功!"<<endl;
				}
			}
			flag=1;
			break;
		}
	}
	if(flag==0)
		cout<<"您所要乘坐的车次不存在,请查证后再订票"<<endl;
}
void manager::giveup(){
	cout<<"您要退那个车次的车票?"<<endl;
	string str;
	cin>>str;
	bool flag=0;
	for(int i=0;i<=num;i++){
		if(rn[i].railname==str){
			cout<<"请输入您要退的票数,并输入您车票的出发地和目的地"<<endl;
			int j;
			cin>>j;
			string str1;
			cin>>str1;
			string str2;
			cin>>str2;
			city* c1;city * c2;
			c1=c2=rn[i].shifa;
			while(c1!=NULL){
				if(c1->name!=str1)
					c1=c1->next;
				else
					break;
			}
			while(c2!=NULL){
				if(c2->name!=str2)
					c2=c2->next;
				else
					break;
			}
			if((c1==NULL)||(c2==NULL)){
				cout<<"您输入的信息有错误,请查证后再输入"<<endl;
			}
			else{
				cout<<"您共退了"<<j<<"张车票,车票原价是"<<c2->price-c1->price<<"元,加收30%手续费后退您"<<(c2->price-c1->price)*0.7*j<<"元"<<endl;
				rn[i].tickets=rn[i].tickets+j;
				write();
				flag=1;
			}
		}
	}
	if(flag==0)
		cout<<"您所要退票的车次不存在,请查证后再退票"<<endl;
}
void manager::inteface(void){
	cout<<"****************欢迎使用铁路票务管理系统!*******************"<<endl;
	read();//录入车次信息
	bool out=0;
	while(!out){
		cout<<"请输入您要以什么身份登陆本系统,管理员(1),普通用户(2),要退出本系统请输入0"<<endl;
		int i;cin>>i;
		if(i==0)
			out=1;
		if(i==1){
			ifstream file;
			file.open("admin.txt");
			string passwd;char c;
			if(!file){
			cerr<<"打开文件错误,可能是该文件不存在"<<endl;
			exit (1);
			}
			else{
				while(file.get(c))
					passwd=passwd+c;				
			}//读取管理员密码
			file.close();
			cout<<"请输入管理员密码!"<<endl;
			string str;cin>>str;
			if(str==passwd){
				int adlaunch=1;
				while(adlaunch!=0){
					cout<<"密码正确,您已经作为管理员登陆成功。现在您能:登出(0),修改密码(1),更新车次(2)"<<endl;
					int addo;cin>>addo;
					if(addo==0)
						adlaunch=0;
					if(addo==1)
						adminpw();
					if(addo==2)
						update();
				}
			}
			else
				cout<<"密码不正确,请重新选择用户身份"<<endl;
		}
		if(i==2){
			int userlaunch=1;
			while(userlaunch!=0){
				cout<<"您已经作为用户登录,您现在可以:退出系统(0),车次查询(1),站站查询(2),订票(3),退票(4)"<<endl;
				int userdo;
				cin>>userdo;
				if(userdo==0){
					userlaunch=0;
					out=1;
				}
				if(userdo==1)
					getrail();
				if(userdo==2)
					findway();
				if(userdo==3)
					reserve();
				if(userdo==4)
					giveup();
			}
		}
	}
}



⌨️ 快捷键说明

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