[英]C++ Queue Simulation
作為家庭作業的一部分,我應該編寫一個模擬雜貨店環境中隊列的程序。 完整的作業在鏈接頁面上進行了解釋。
我只有一個隊列時,程序正在運行,我正在嘗試根據分配說明修改它以處理多個隊列。 但是,編譯時我遇到了一些錯誤。
我知道這個問題與排隊的客戶有關; 我只是不確定如何修改程序,因此它適用於多個隊列。
非常感謝任何幫助!
錯誤消息:
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)]'
主要計划
主程序包括一個名為Queue的類。 我知道代碼是正確的,因為它在不同的測試程序中完美地工作。
#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()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.