idk why these stuffs get stashed for so long and I didn't ever commit them
This commit is contained in:
77
Experiment/Experiment2/Task2/Ferry.cpp
Normal file
77
Experiment/Experiment2/Task2/Ferry.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include <iostream>
|
||||
#include "LinkQueue.h"
|
||||
using namespace std;
|
||||
|
||||
struct Car
|
||||
{
|
||||
int no; //车号
|
||||
char type; //c:客车,t:货车
|
||||
};
|
||||
|
||||
//求队列长度
|
||||
template<class DT>
|
||||
int QueueLength(LinkQueue<DT> Q)
|
||||
{
|
||||
int n = 0;
|
||||
QNode<DT>* p = Q.front->next;
|
||||
while (p)
|
||||
{
|
||||
p = p->next;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
//车辆入队
|
||||
void Ferry(LinkQueue<Car>& Qc, LinkQueue<Car>& Qt)
|
||||
{
|
||||
int i = 0;
|
||||
Car e;
|
||||
while (QueueLength(Qc) + QueueLength(Qt) > 0) //队列非空
|
||||
{
|
||||
if (QueueLength(Qc) > 0) //客车队列非空
|
||||
{
|
||||
while ((i < 4) && (QueueLength(Qc) > 0)) //客车数小于4且客车队列非空
|
||||
{
|
||||
DeQueue(Qc, e); //客车出队
|
||||
cout << "车号:" << e.no << "\t类型:" << e.type << endl;
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
}
|
||||
if (QueueLength(Qt) > 0) //货车队列非空
|
||||
{
|
||||
DeQueue(Qt, e); //货车出队
|
||||
cout << "车号:" << e.no << "\t类型:" << e.type << endl;
|
||||
}
|
||||
}
|
||||
cout << "所有车辆已过江!" << endl;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
LinkQueue<Car> Qc, Qt;
|
||||
Car e;
|
||||
int i, n;
|
||||
InitQueue(Qc);
|
||||
InitQueue(Qt);
|
||||
cout << "客车数:";
|
||||
cin >> n;
|
||||
for (i = 1; i <= n; i++) //客车入队
|
||||
{
|
||||
e.no = i;
|
||||
e.type = 'c';
|
||||
EnQueue(Qc, e);
|
||||
}
|
||||
cout << "货车数:";
|
||||
cin >> n;
|
||||
for (i = 1; i <= n; i++) //货车入队
|
||||
{
|
||||
e.no = i;
|
||||
e.type = 't';
|
||||
EnQueue(Qt, e);
|
||||
}
|
||||
Ferry(Qc, Qt); //车辆过江
|
||||
DestroyQueue(Qc);
|
||||
DestroyQueue(Qt);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user