gpt4 book ai didi

C++ 队列模拟

转载 作者:行者123 更新时间:2023-11-28 03:06:31 24 4
gpt4 key购买 nike

作为家庭作业的一部分,我应该编写一个程序来模拟杂货店环境中的队列。 full assignment在链接页面上有解释。

我让程序在只有一个队列时工作,并且我正在根据分配描述尝试修改它以处理多个队列。但是,我在编译时遇到了一些错误。

我知道这个问题与排队的客户出队有关;我只是不确定如何修改程序以使其适用于多个队列。

非常感谢任何帮助!

错误信息:

qsim.cpp: In function 'int main()':
qsim.cpp:64: error: request for member 'empty' in 'line', which is of non-class type 'Queue [(((long unsigned int)(((long int)queuecount) - 1)) + 1u)]'
qsim.cpp:66: error: request for member 'dequeue' in 'line', which is of non-class type 'Queue [(((long unsigned int)(((long int)queuecount) - 1)) + 1u)]'

主程序
主程序包含一个名为队列的类。我知道代码对此是正确的,因为它在不同的测试程序中完美运行。

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "queue.h"
using namespace std;

int shortest_queue(Queue q[], int queuecount)
{
int shortest = 0;
for (int i = 1; i < queuecount; ++i)
{
if(q[i].size() < q[shortest].size())
shortest = i;
}
return shortest;
}

int queue_total(Queue q[], int queuecount)
{
int custcount = 0;
for (int i = 0; i < queuecount; ++i)
custcount += q[i].size();
return custcount;
}

int main()
{
int trans_time = 0;
int count = 0;
int entry_time;
int wait_sum = 0;
int wait_time = 0;
int seed;
int ARV_PROB;
int MAX_TRANS_TIME;
int DURATION;
int queuecount;
int shortline;
int temp;

cout << "Enter these parameters of the simulation:" << endl;
cout << " The number of queue/server pairs: ";
cin >> queuecount;
Queue line[queuecount];
cout << " The probability that a customer arrives in one tick (%): ";
cin >> ARV_PROB;
cout << " The maximum duration of a transaction in ticks: ";
cin >> MAX_TRANS_TIME;
cout << " The duration of the simulation in ticks: ";
cin >> DURATION;
cout << "Enter a random number seed: ";
cin >> seed;
srand(seed);

for (int time = 0; time < DURATION; ++time)
{
if ( rand() % 100 < ARV_PROB )
{
shortline = shortest_queue(line, queuecount);
line[shortline].enqueue(time);
}
if ( trans_time == 0 )
{
if ( !line.empty() )
{
entry_time = line.dequeue();
temp = (time - entry_time);
if(temp > wait_time)
wait_time = temp;
wait_sum += (time - entry_time);
++count;
trans_time = (rand() % MAX_TRANS_TIME) + 1;
}
}
else
{
--trans_time;
}
cout << setw(4) << time << setw(4) << trans_time << " " << line << endl;
}

cout << count << " customers waited an average of ";
cout << wait_sum / count << " ticks." << endl;
cout << "The longest time a customer waited was " << wait_time << " ticks." << endl;
cout << queue_total(line, queuecount) << " customers remain in the lines." << endl;

return 0;
}

最佳答案

Queue line[queuecount];

if ( !line.empty() )

line 不是 Queue。它是一个 Queues 数组,因此您必须对要检查的特定数组元素调用 empty()

关于C++ 队列模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19548143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com