Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 队列使用结构(出租车调度问题)_C++_Arrays_Struct_Member Function Pointers - Fatal编程技术网

C++ 队列使用结构(出租车调度问题)

C++ 队列使用结构(出租车调度问题),c++,arrays,struct,member-function-pointers,C++,Arrays,Struct,Member Function Pointers,我想写一个程序,从用户那里读取命令,当输入d出租车时,它会提示输入driver\u id,并将出租车存储在队列中(队列最多可以有n辆出租车),当客户输入命令c时,它会将队列中最早的出租车分配给客户 我试图使用struct member函数来解决它,这样我们的代码看起来不错,但是尽管我已经初始化了n=4,但它只能存储2taxi,并显示第三个条目的队列已满,这不应该发生。请回顾我的方法 程序运行方式如下: PS C:\Users; if ($?) { g++struct_taxi};; if ($?

我想写一个程序,从用户那里读取命令,当输入
d
出租车时,它会提示输入
driver\u id
,并将出租车存储在队列中(队列最多可以有n辆出租车),当客户输入命令
c
时,它会将队列中最早的出租车分配给客户

我试图使用struct member函数来解决它,这样我们的代码看起来不错,但是尽管我已经初始化了
n=4
,但它只能存储
2
taxi,并显示第三个条目的队列已满,这不应该发生。请回顾我的方法

程序运行方式如下:

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    
代码:

#包括
使用名称空间std;
常数int n=4;
结构队列{
int元素[n],nwaiting,front;
void initialize(){
nwaiting=0;
正面=0;
}
布尔插入(int v){
如果(n等待>=n)
返回false;
元素[(前+n等待)%n]=v;
nwaiting++;
返回true;
}
布尔删除(内部和外部){
如果(nwaiting==0)
返回false;
否则{
v=元素[前];
正面=(正面+1)%n;
等待--;
返回true;
}
}
};
int main(){
队列q;
q、 初始化();
while(true){
coutc;
如果(c=='d'){
coutdriverid;
如果(!q.insert(driverid)){

cout问题是,当您检查条件
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;}
        }
    }
}
显然,一种更简单的方法是使用一种用于类似情况的数据结构,由于它具有与
队列
结构相同的功能,因此代码会短得多:

#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";
            }
        }
    }
}

此外,不建议对变量名使用诸如
front
remove
等关键字。请签出,而不要使用“initialize”方法,您应该实现以下内容:。查看STL库可能也是一个好主意,因为其中的类可以实现您试图为您实现的大部分内容,例如or。@luiz谢谢您,先生,今天刚刚学习的构造函数,以后将实现它,请阅读所有推荐的概念我刚刚修改了我的答案,使用了
std::queue
。如果你需要更短更干净的代码,也许可以用它来代替声明你自己的结构。感谢兄弟添加了另一段代码,我知道一些数据结构,但从未使用过堆栈,quequ…我对数组非常有限,必须从你的应用程序中学习队列应用程序代码
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
#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 :