C ++队列模拟

时间:2013-10-23 17:14:19

标签: c++ simulation

作为家庭作业的一部分,我应该编写一个模拟杂货店环境中队列的程序。 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)]'

主程序
主程序包括一个名为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;
}

1 个答案:

答案 0 :(得分:2)

Queue line[queuecount];

if ( !line.empty() )

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