0

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

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

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

非常感谢任何帮助!

错误信息:

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;
}
4

1 回答 1

2
Queue line[queuecount];

if ( !line.empty() )

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

于 2013-10-23T17:19:23.680 回答