Queue using struct(出租车调度问题)
Queue using struct (Taxi dispatch problem)
我想写程序读取用户的命令,当输入d
时输入taxi,提示输入driver_id
并把taxi存入队列(队列最多可以有n个)出租车),当客户输入命令 c
时,它会将队列中最早的出租车分配给客户。
我正在尝试使用 struct 成员函数来解决它,以便我们的代码看起来不错,但是虽然我已经初始化了 n=4
,但它只能存储 2
出租车,并向我展示第三个条目的队列已满,这是不应该发生的。请查看我的方法。
这样编程 运行:
PS C:\Users; if ($?) { g++struct_taxi};; if ($?) { .\struct_taxi}
enter command:d
enter driverid:122
enter command:d
enter driverid:124
enter command:d
enter driverid:126
Q is full
代码:
#include<iostream>
using namespace std;
const int n=4;
struct Queue{
int elements[n],nwaiting,front;
void initialize(){
nwaiting=0;
front=0;
}
bool insert(int v){
if(nwaiting>=n)
return false;
elements[(front+nwaiting)%n]=v;
nwaiting++;
return true;
}
bool remove(int &v){
if(nwaiting==0)
return false;
else{
v=elements[front];
front=(front+1)%n;
nwaiting--;
return true;
}
}
};
int main(){
Queue q;
q.initialize();
while(true){
cout<<"enter command:";
char c;cin>>c;
if(c=='d'){
cout<<"enter driverid:";
int driverid;cin>>driverid;
if(!q.insert(driverid)){
cout<<"Q is full\n";}
else{
q.insert(driverid);
}
}
else if(c=='c'){
int driverid;
if(!q.remove(driverid)){
cout<<"No taxi available.\n";
}
else
//q.remove(driverid);
cout<<"assigning:"<<" "<<driverid<<endl;
}
}
}
问题是当您检查条件 if(!q.insert(driverid))
时,您已经将该驱动程序插入到系统中。然后 else
语句再次使用 q.insert(driverid);
插入它
所以解决方案是简单地删除 else
语句。
#include<iostream>
using namespace std;
const int n=4;
struct Queue
{
int elements[n],nwaiting,front;
void initialize()
{
nwaiting=0;
front=0;
}
bool insert(int v)
{
if(nwaiting>=n) {return false;}
elements[(front+nwaiting)%n]=v;
nwaiting++;
return true;
}
bool remove(int &v)
{
if(nwaiting==0)
return false;
else
{
v=elements[front];
front=(front+1)%n;
nwaiting--;
return true;
}
}
};
int main()
{
Queue q;
q.initialize();
while(true)
{
cout<<"enter command:";
char c;
cin>>c;
if(c=='d')
{
cout<<"enter driverid:";
int driverid;
cin>>driverid;
if(!q.insert(driverid))
{
cout<<"Q is full\n";
}
}
else if(c=='c')
{
int driverid;
if(!q.remove(driverid))
{
cout<<"No taxi available.\n";
}
else {cout<<"assigning:"<<" "<<driverid<<endl;}
}
}
}
结果:
enter command:d
enter driverid:121
enter command:d
enter driverid:122
enter command:d
enter driverid:123
enter command:d
enter driverid:124
enter command:d
enter driverid:125
Q is full
一种更简单的方法显然是使用 std::queue
,一种用于这种情况的数据结构,并且由于它具有与 Queue
结构相同的功能,因此代码会很多较短:
#include <iostream>
#include <queue>
using namespace std;
const int maxn=2;
int main()
{ queue<int> q;
while(true)
{
cout << "Enter command : "; char c; cin >> c;
if (c == 'd') //if inserting new driver
{
cout << "Enter driver's ID : "; int id; cin >> id; //input id
if (q.size() == maxn) {cout << "Queue is full\n";} //if size of queue is equal to maxn, no insert
else {q.push(id);} //else insert
}
else if (c == 'c')
{
if (q.empty()) {cout << "No driver available\n";} //if no driver, no assigning
else
{
int curDriver = q.front(); //take drive in front of queue
q.pop(); //take the driver id out of queue
cout << "Assigned driver : " << curDriver << "\n";
}
}
}
}
结果:
Enter command : d
Enter driver's ID : 123
Enter command : d
Enter driver's ID : 124
Enter command : d
Enter driver's ID : 125
Queue is full
Enter command : c
Assigned driver : 123
Enter command : c
Assigned driver : 124
Enter command : c
No driver available
Enter command :
此外,不建议使用 front
、remove
等关键字作为变量名。并查看 Why is "using namespace std;" considered bad practice?
我想写程序读取用户的命令,当输入d
时输入taxi,提示输入driver_id
并把taxi存入队列(队列最多可以有n个)出租车),当客户输入命令 c
时,它会将队列中最早的出租车分配给客户。
我正在尝试使用 struct 成员函数来解决它,以便我们的代码看起来不错,但是虽然我已经初始化了 n=4
,但它只能存储 2
出租车,并向我展示第三个条目的队列已满,这是不应该发生的。请查看我的方法。
这样编程 运行:
PS C:\Users; if ($?) { g++struct_taxi};; if ($?) { .\struct_taxi}
enter command:d
enter driverid:122
enter command:d
enter driverid:124
enter command:d
enter driverid:126
Q is full
代码:
#include<iostream>
using namespace std;
const int n=4;
struct Queue{
int elements[n],nwaiting,front;
void initialize(){
nwaiting=0;
front=0;
}
bool insert(int v){
if(nwaiting>=n)
return false;
elements[(front+nwaiting)%n]=v;
nwaiting++;
return true;
}
bool remove(int &v){
if(nwaiting==0)
return false;
else{
v=elements[front];
front=(front+1)%n;
nwaiting--;
return true;
}
}
};
int main(){
Queue q;
q.initialize();
while(true){
cout<<"enter command:";
char c;cin>>c;
if(c=='d'){
cout<<"enter driverid:";
int driverid;cin>>driverid;
if(!q.insert(driverid)){
cout<<"Q is full\n";}
else{
q.insert(driverid);
}
}
else if(c=='c'){
int driverid;
if(!q.remove(driverid)){
cout<<"No taxi available.\n";
}
else
//q.remove(driverid);
cout<<"assigning:"<<" "<<driverid<<endl;
}
}
}
问题是当您检查条件 if(!q.insert(driverid))
时,您已经将该驱动程序插入到系统中。然后 else
语句再次使用 q.insert(driverid);
所以解决方案是简单地删除 else
语句。
#include<iostream>
using namespace std;
const int n=4;
struct Queue
{
int elements[n],nwaiting,front;
void initialize()
{
nwaiting=0;
front=0;
}
bool insert(int v)
{
if(nwaiting>=n) {return false;}
elements[(front+nwaiting)%n]=v;
nwaiting++;
return true;
}
bool remove(int &v)
{
if(nwaiting==0)
return false;
else
{
v=elements[front];
front=(front+1)%n;
nwaiting--;
return true;
}
}
};
int main()
{
Queue q;
q.initialize();
while(true)
{
cout<<"enter command:";
char c;
cin>>c;
if(c=='d')
{
cout<<"enter driverid:";
int driverid;
cin>>driverid;
if(!q.insert(driverid))
{
cout<<"Q is full\n";
}
}
else if(c=='c')
{
int driverid;
if(!q.remove(driverid))
{
cout<<"No taxi available.\n";
}
else {cout<<"assigning:"<<" "<<driverid<<endl;}
}
}
}
结果:
enter command:d
enter driverid:121
enter command:d
enter driverid:122
enter command:d
enter driverid:123
enter command:d
enter driverid:124
enter command:d
enter driverid:125
Q is full
一种更简单的方法显然是使用 std::queue
,一种用于这种情况的数据结构,并且由于它具有与 Queue
结构相同的功能,因此代码会很多较短:
#include <iostream>
#include <queue>
using namespace std;
const int maxn=2;
int main()
{ queue<int> q;
while(true)
{
cout << "Enter command : "; char c; cin >> c;
if (c == 'd') //if inserting new driver
{
cout << "Enter driver's ID : "; int id; cin >> id; //input id
if (q.size() == maxn) {cout << "Queue is full\n";} //if size of queue is equal to maxn, no insert
else {q.push(id);} //else insert
}
else if (c == 'c')
{
if (q.empty()) {cout << "No driver available\n";} //if no driver, no assigning
else
{
int curDriver = q.front(); //take drive in front of queue
q.pop(); //take the driver id out of queue
cout << "Assigned driver : " << curDriver << "\n";
}
}
}
}
结果:
Enter command : d
Enter driver's ID : 123
Enter command : d
Enter driver's ID : 124
Enter command : d
Enter driver's ID : 125
Queue is full
Enter command : c
Assigned driver : 123
Enter command : c
Assigned driver : 124
Enter command : c
No driver available
Enter command :
此外,不建议使用 front
、remove
等关键字作为变量名。并查看 Why is "using namespace std;" considered bad practice?