C++ 寻址类型结构列表的元素
我有一个编程任务,应该是模拟“m”服务器的工作。从0到某个“t”的每一个时间单位,都会出现随机数目的任务(从0到某个“k”),需要从1到某个“d”的时间才能完成。服务器自行处理任务,一般存在排队现象。我差不多完成了这件事,但我的问题是如何处理结构的元素 有问题的台词C++ 寻址类型结构列表的元素,c++,C++,我有一个编程任务,应该是模拟“m”服务器的工作。从0到某个“t”的每一个时间单位,都会出现随机数目的任务(从0到某个“k”),需要从1到某个“d”的时间才能完成。服务器自行处理任务,一般存在排队现象。我差不多完成了这件事,但我的问题是如何处理结构的元素 有问题的台词 server_array[j] = queue.front.time_req;//We assign the first task in the queue to the server, making it delayed by t
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()
{
}
#包括“stdafx.h”
#包括“ServerSimulation.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
ServerSimulation::ServerSimulation()
{
int m=0;
int t=0;
int k=0;
int d=0;
int avgwait=0;
int-avglenght=0;
int finlenght=0;
服务器_数组=新整数[0];
服务器_数组[0]=0;
int检验=0;
int总长度=0;
int total_tasks=0;
int total_wait=0;
}
结构服务器模拟::任务
{
国际到达时间;
整数时间要求;
};
int-ServerSimulation::随机数(int-lb,int-ub)
{
INTR;
r=rand();
r=r%(ub-lb+1);
r=r+lb;
返回r;
}
void ServerSimulation::simulate(int m、int t、int k、int d)
{
server_array=new int[m];//创建服务器数组后,每个服务器的值将指示服务器的繁忙时间。
for(inti=0;i任务。
}
其他的
{
//请尝试下一个服务器,以便for循环继续
}
}
}
total_lenght+=queue.size;//将计算总长度(如果可能,这是我们尝试分配任务后的长度)。
对于(int t=0;t//一个时间单位经过,所以我们从所有服务器中减去1(除非它们是空闲的)。
}
}
}
avglenght=总长度/t;
finlenght=queue.size;
avgwait=总等待/总任务;
cout调用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
来获取队列中有多少元素
“整个代码”不是整个代码,因为它不包含类ServerSimulation
的声明。请发布。server\u数组[0]=0;
在server\u array=new int[0];
之后?越界访问看起来很危险。希望我知道你的意思:D.程序确实运行,但结果完全不正确,可能是这样吗?:DWoah,非常感谢。这解决了所有问题。为什么放()虽然?该程序可以运行,但我试图理解这一点。queue.front
是一个成员函数,返回对第一个元素的引用,只需编写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.