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

📄 轮渡.cpp

📁 数据结构课程设计(C++)版本
💻 CPP
字号:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>					//此头文件包含有上time函数和ctime函数的

typedef int ElemType;
struct LNode{
	ElemType data;					//值域
	LNode* next;					//链接指针域
};

struct LinkQueue
{
	LNode* front;					//队首指针
	LNode* rear;					//队尾指针
};

#include"链接队列.cpp"

									//输出本次轮渡所载汽车编号
void Print(int a[],int n)
{
	long t;
	t=time(0);//当前机器系统时间被保存到t中,单位为秒
	cout<<endl;
	cout<<"轮渡开始起航->"<<endl;
	cout<<"本次过江的时间:"<<ctime(&t)<<endl;
	//ctime(&t)函数的值为根据参数t转换得到的日期和时间的字符串
	cout<<"本次轮渡所载汽车:";
	for(int i=0;i<n;i++)cout<<a[i]<<' ';
	cout<<endl;
}
//输出汽车排队等待的情况
void OutputQueue(const LinkQueue& q1,const LinkQueue& q2)
{
	cout<<"客车等候的情况:";
	LNode* p=q1.front;
	if(p==NULL)cout<<"暂时无客车等候."<<endl;
	while(p!=NULL)
	{
		cout<<p->data<<' ';
		p=p->next;
	}
	cout<<endl;
	cout<<"货车排队的情况:";
	p=q2.front;
	if(p==NULL)cout<<"暂时无货车等候."<<endl;
	while(p!=NULL)
	{
		cout<<p->data<<' ';
		p=p->next;
	}
	cout<<endl;
}

void main()
{
	//q1和q2队列用来分别存储待渡江的客车和货车
	LinkQueue q1,q2;
	//对q1和q2进行初始化
	InitQueue(q1);
	InitQueue(q2);
	//用flag保存用户选择,用mark登记轮渡到渡口
	int flag,mark=0;
	//用数组a记录轮渡上的每个汽车号,用n记录汽车的个数
	int a[10],n=0;
	// 用t1和t2登记时间
	long t1,t2;
	//程序处理结果
	do
	{
		//显示功能表并接受用户选择
		L1:cout<<"功能表:"<<endl;
		cout<<"1---车到渡口进行登记"<<endl;
		cout<<"2---轮渡到渡口进行登记"<<endl;
		cout<<"3---汽车上轮渡"<<endl;
		cout<<"4---命令轮渡起航"<<endl;
		cout<<"5---输出当前汽车排队情况"<<endl;
		cout<<"6---结束程序运行"<<endl<<endl;
		cout<<"请输入你的选择(1-6):";
		do
		{
			cin>>flag;
			if(flag<1||flag>6)cout<<"输入功能号错,重输:";
		}while(flag<1||flag>6);
		int x,i;
		//根据不同选择进行相应处理
		switch(flag){
			case 1:
				cout<<"输入车辆号,假定小于100为客车,否则为货车,"<<endl;
				cout<<"可以输入多辆车,用空格分开,直到输入-1为止。"<<endl;
				while(1){
					cin>>x;
					if(x==-1)break;
					if(x<100)EnQueue(q1,x);//客车进q1队
					else EnQueue(q2,x);//货车进q2队
				}
				break;				//结束switch语句
			case 2:
				if(mark==1){
					cout<<"渡轮已在渡口等待,不要重复登记!"<<endl;
					break;			//结束switch语句	
				}
				mark=1;			//渡轮到口岸登记
				cout<<"渡轮已到渡口,可以上船!"<<endl;
				n=0;		//装载车辆数初始为0;
				t1=time(0);			//登记渡轮到渡口时间,单位为秒
				break;				//结束switch语句
			case 3:
				if(EmptyQueue(q1) && EmptyQueue(q2)){
					cout<<"暂无汽车过江!"<<endl;
					if(mark==1 && n!=0){
						t2=time(0)-t1;				//计算到目前为止渡轮等待时间的秒数
						cout<<"轮渡未满,有车"<<n<<"辆,已等待"<<t2/60<<"分";
						cout<<t2%60<<"秒,等候其他汽车上渡轮!"<<endl;
					}
					break;		//结束switch语句
				}
				if(mark!=1){
					cout<<"渡轮未到,请汽车稍后上渡轮!"<<endl;
					break;		//结束switch语句
				}
				do{
					i=0;
					//首先上4辆客车
					while(!EmptyQueue(q1) && n<10 && i<4){
						a[n++]=OutQueue(q1);
						i++;
					}
					//满10辆开船,打印车辆号,重新对mark和n清0,转功能号表
					if(n==10){Print(a,n);mark=0;n=0;goto L1;}
					//进4辆客车则接着进一辆货车,不满4辆则由货车补
					if(i==4){
						if(!EmptyQueue(q2)) a[n++]=OutQueue(q2);
					}
					else{
						while(!EmptyQueue(q2) && n<10 && i<5){
							a[n++]=OutQueue(q2);
							i++;
						}
					}
					//满10辆则开船
					if(n==10){Print(a,n);mark=0;n=0;goto L1;}
				}while(!EmptyQueue(q1) || !EmptyQueue(q2));
						//只要客车或货车队列不全为空,则继续执行do循环
				t2=time(0)-t1;		//登记渡轮已经等待时间的秒数
				cout<<"渡轮上有车"<<n<<"辆,已等待"<<t2/60<<"分"<<t2%60;
				cout<<"秒,等候其他汽车上渡轮!"<<endl;
				break;		//结束switch语句
			case 4:
				if(n==0 || mark==0)
					cout<<"轮渡上无车过江或根本无渡轮!不需要起航!"<<endl;
				else{
					Print(a,n);mark=0;n=0;
				}
				break;
			case 5:
				OutputQueue(q1,q2);
				break;		//结束switch语句
			case 6:
				if(!EmptyQueue(q1) || !EmptyQueue(q2)){
					cout<<"还有汽车未渡江,暂不能结束!"<<endl;
					break;		//结束switch语句
				}
				if(n!=0){
					cout<<"渡轮上有车,不能结束,需命令开渡轮!"<<endl;
					break;		//结束switch语句
				}
				cout<<"程序运行结束!"<<endl;
				return;		//执行结束返回
		}					//switch语句终端位置
	}while(1);				//外层do循环终端位置
	ClearQueue(q1);
	ClearQueue(q2);
}						//主函数结束位置

⌨️ 快捷键说明

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