I am simulating a fast food restaurant over three hours. The three hours are divided into 18 intervals of 600 seconds each. Each interval outputs statistics about what happened on those 600 seconds.
我在三个小时内模拟一家快餐店。三小时分为18个间隔,每个间隔600秒。每个间隔输出有关600秒内发生的事件的统计数据。
My original structure was like this:
我原来的结构是这样的:
int i;
for (i=0; i<18; i++)
interval(customerArrive);
int interval(double customerArrive)
...
...
int t;
for (t=0;t<600;t++)
This method of two (for loops) doesn't work because it doesn't allow time to be a continuous function. If an event happens (as in, a customer arrives) in the first interval at t=599, then this event will not exist at t=601 because everything is erased as the second interval begins.
这种方法(for循环)不起作用,因为它不允许时间成为连续函数。如果在t = 599的第一个间隔中发生事件(如,客户到达),那么在t = 601时该事件将不存在,因为在第二个间隔开始时所有事件都被擦除。
The way I want to approach this is to make a while loop to allow time to be a continuous function, I just don't know how to 'convert' my code to this.
我想要解决这个问题的方法是创建一个while循环以允许时间成为一个连续函数,我只是不知道如何将我的代码“转换”为此。
Would it be something like this?
它会是这样的吗?
while (t<10800)
{
...
}
I'm not sure what the condition needs to be for this while loop to exit.
我不确定这个while循环退出时需要什么条件。
If necessary, here is my full code: http://pastebin.com/3ec0Ks9u
如有必要,这是我的完整代码:http://pastebin.com/3ec0Ks9u
2 个解决方案
#1
0
"May be",i understood your problem.If you want something to be monitored (continuous function in your words),u need to take help of threads and timers,which is out of your scope for now.If you want to use something basic,the question is not well understood,please re-construct it.
“可能是”,我理解你的问题。如果你想监视某些东西(用你的话来说是连续的功能),你需要得到线程和计时器的帮助,这在你的范围之外是不可能的。如果你想用某些东西基本的,问题还不太清楚,请重新构建。
#2
0
[…] everything is erased as the second interval begins.
[...]随着第二个间隔的开始,一切都被删除了。
That's not due to the two for
loops, but instead due to the fact that the outer for
loop calls a function, and you reinitialize stuff in that function. So move that outer for
loop into the function interval
(probably changing the method name along the way, as it no longer fits). In that method you can write
这不是由于两个for循环,而是由于外部for循环调用一个函数,并重新初始化该函数中的东西。因此将外部for循环移动到函数区间(可能会改变方法名称,因为它不再适合)。在那种方法中你可以写
int i, t;
… // initialize stuff
for (i=0; i<18; i++) {
for (t=0;t<600;t++) {
… // do stuff
}
}
Those loops won't erase anything, and you won't have to compute the values of i
and t
from some single counter.
这些循环不会删除任何内容,您不必从某个单个计数器计算i和t的值。
allow time to be a continuous function
让时间成为一个连续的功能
Obviously, time will still be discretized in those steps. As it would in your while
loop. If you dont want discrete time steps, you'll have to radically change your code, e.g. by computing the times when customers arrive up front, and then stepping from event to event instead of in fixed increments.
显然,时间仍将在这些步骤中离散化。就像在你的while循环中一样。如果您不想要离散的时间步长,则必须从根本上改变您的代码,例如:通过计算客户预先到达的时间,然后从事件转到事件而不是固定增量。
#1
0
"May be",i understood your problem.If you want something to be monitored (continuous function in your words),u need to take help of threads and timers,which is out of your scope for now.If you want to use something basic,the question is not well understood,please re-construct it.
“可能是”,我理解你的问题。如果你想监视某些东西(用你的话来说是连续的功能),你需要得到线程和计时器的帮助,这在你的范围之外是不可能的。如果你想用某些东西基本的,问题还不太清楚,请重新构建。
#2
0
[…] everything is erased as the second interval begins.
[...]随着第二个间隔的开始,一切都被删除了。
That's not due to the two for
loops, but instead due to the fact that the outer for
loop calls a function, and you reinitialize stuff in that function. So move that outer for
loop into the function interval
(probably changing the method name along the way, as it no longer fits). In that method you can write
这不是由于两个for循环,而是由于外部for循环调用一个函数,并重新初始化该函数中的东西。因此将外部for循环移动到函数区间(可能会改变方法名称,因为它不再适合)。在那种方法中你可以写
int i, t;
… // initialize stuff
for (i=0; i<18; i++) {
for (t=0;t<600;t++) {
… // do stuff
}
}
Those loops won't erase anything, and you won't have to compute the values of i
and t
from some single counter.
这些循环不会删除任何内容,您不必从某个单个计数器计算i和t的值。
allow time to be a continuous function
让时间成为一个连续的功能
Obviously, time will still be discretized in those steps. As it would in your while
loop. If you dont want discrete time steps, you'll have to radically change your code, e.g. by computing the times when customers arrive up front, and then stepping from event to event instead of in fixed increments.
显然,时间仍将在这些步骤中离散化。就像在你的while循环中一样。如果您不想要离散的时间步长,则必须从根本上改变您的代码,例如:通过计算客户预先到达的时间,然后从事件转到事件而不是固定增量。