2015-12-04 18 views
-1

我有一個應該模擬'm'服務器工作的編程任務。從0到某個't'的每個時間單位,任意數量的任務進入(從0到某些'k'),從1到某些'd'時間完成。服務器自行處理任務,有一個通用的隊列。我或多或少完成了這些事情,但是我的問題是解決結構的元素。尋址類型列表中的元素struct

有問題的線

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

「整個代碼」不是整個代碼,因爲它不包含類「ServerSimulation」的聲明。請發佈[MCVE](http://stackoverflow.com/help/mcve)。 – MikeCAT

+0

已添加,我的不好... – Valentin

+0

'server_array [0] = 0;'server_array = new int [0];''之後''?它看起來很危險,因爲越界訪問。 – MikeCAT

回答

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中有多少元素。

+0

哇,非常感謝。解決了一切。爲什麼重要()雖然?該計劃的工作原理,但我試圖理解這一點。 – Valentin

+0

'queue.front'是一個成員函數,它返回對第一個元素的引用,並且只寫'queue.front'並不意味着調用該函數。使用'()'運算符來調用函數。 – MikeCAT

+0

再次感謝:) – Valentin