寻址类型结构列表的元素

时间:2015-12-04 15:23:37

标签: c++

我有一个编程任务,应该模拟' m'服务器。从0到某个时间的每个单位时间,随机数量的任务进入(从0到某些' k')从1到某些' d&#39 ;时间来完成。服务器自己处理任务,有一般排队。我或多或少完成了这件事,但我的问题是解决结构的元素。

有问题的行

server_array[j] = queue.front.time_req;//We assign the first task in the queue to the server, making it delayed by time_required.
total_wait += (t - queue.front.arrival_time);   //After task assignement we check how long the task had to wait to be assigned.

这是整个代码

#include "stdafx.h"
#include "ServerSimulation.h"
#include <random>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <list>

using namespace std;
ServerSimulation::ServerSimulation()
{
    int m = 0;
    int t = 0;
    int k = 0;
    int d = 0;
    int avgwait = 0;
    int avglenght = 0;
    int finlenght = 0;
    server_array = new int[0];
    server_array[0] = 0;
    int test = 0;
    int total_length = 0;
    int total_tasks = 0;
    int total_wait = 0;
}


struct ServerSimulation::task
{
    int arrival_time;
    int time_req;
};

int ServerSimulation::random_number(int lb, int ub)
{
        int r;
        r = rand();
        r = r % (ub - lb + 1);
        r = r + lb;
        return r;
}

void ServerSimulation::simulate(int m, int t, int k, int d)
{
    server_array = new int[m]; //Array of servers is created, the value of each server is going to indicate how much more time the server is busy.

    for (int i = 0; i < t; i++) //General loop of time going by.
    {
        int new_tasks = random_number(0, k);
        total_tasks += new_tasks;
        for (int l = 0; l < new_tasks; l++)
        {
            task addition_to_queue; //New task to be added is defined.
            addition_to_queue.arrival_time = t;
            addition_to_queue.time_req = random_number(1, d);
            queue.push_back(addition_to_queue); //We add the task at the end of the queue.

            for (int j = 0; j < m; j++) //The loop of assigning tasks to servers (otherwise adding to the queue).
            {
                if (server_array[j] == 0)
                {
                    server_array[j] = queue.front.time_req;//We assign the first task in the queue to the server, making it delayed by time_required.
                    total_wait += (t - queue.front.arrival_time);   //After task assignement we check how long the task had to wait to be assigned.
                    queue.pop_front; //The first element is deleted.
                    j = m; //We use this to leave the for loop ===> task was assigned.
                }
                else
                {
                    //try next server, so the for loop continues
                }


            }

        }
        total_lenght += queue.size;//Total lenght will be calculated (it is a lenght after we tried to assign the tasks if possible.
        for (int t = 0; t < m; t++) //Time passes...
        {
            if (server_array[t] == 0)
            {
            }
            else
            {
                server_array[t]--;                      //One time unit passes, so we subtract 1 from all servers (unless they are free).
            }

        }
    }           
    avglenght = total_lenght / t;
    finlenght = queue.size;
    avgwait = total_wait / total_tasks;
    cout << "Simulation complete!" << endl;
    Sleep(2000);
    cout << "The results are as follows: " << endl;
    Sleep(1000);
    cout << "The average wait time: ";
    Sleep(1000);
    cout << avgwait << endl;
    Sleep(1000);
    cout << "The average lenght of the queue ";
    Sleep(1000);
    cout << avglenght << endl;
    Sleep(1000);
    cout << "Number of tasks that were not finished: ";
    Sleep(1000);
    cout << finlenght << endl;

}
    ServerSimulation::~ServerSimulation()
 {

}

请求添加 - 对不起。

#pragma once
#ifndef SERVERSIMULATION_H
#define SERVERSIMULATION_H
#include <list>

using namespace std;
class ServerSimulation
{
public:
    ServerSimulation();
    ~ServerSimulation();
    void simulate(int m, int t, int k, int d);
    int random_number(int lb, int up);
    struct task;

private:
    int m;
    int t;
    int k;
    int d;
    int avgwait;
    int avglenght;
    int finlenght;
    int* server_array;
    int test;
    int total_lenght;
    int total_tasks;
    int total_wait;
    std::list<task> queue;
};
#endif

1 个答案:

答案 0 :(得分:0)

致电 queue.front,而不是仅仅访问它以获取价值。

server_array[j] = queue.front().time_req;//We assign the first task in the queue to the server, making it delayed by time_required.
total_wait += (t - queue.front().arrival_time);   //After task assignement we check how long the task had to wait to be assigned.

此外,queue.pop_front;不会删除第一个元素。使用queue.pop_front();删除它。

还有一件事:使用queue.size()代替queue.size来获取queue中有多少元素。