排队论的C实现

大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang
以下鄙人实现了排队论思想,语言是C语言
 
#include<stdio.h>
#include<float.h>
 
int main()
{
 
/************************************what we know in the system**************************************/
    float arrivals[] = {0.4, 1.6, 2.1, 3.8, 4.0, 5.6, 5.8, 7.2};//the time of each customer's arrival
    float departures[] = {2.4, 3.1, 3.3, 4.9, 8.6};//the time of each customer's departure
    int acount = 0;//the index of arrivals array
    int dcount = 0;//the index of departures array
/***************the clock and clock event in the simulating system******************/
    float clock = 0;//simulation clock
    float clock_event[2] = {arrivals[acount],departures[dcount]};//clock[0] means arrival time,clock[1] means departure time
    int clock_event_times[2] = {0, 0}; //clock_event_times[0] means the count of arrival event happened;
                                        //clock_event_times[1] means the count of departure event happened
/*****************system state ****************************/
    int serve_status = 0;//false means ideal and 1 means busy
    int no_in_queue = 0;//the number in queue
    float time_of_arrival[4] = {0};//the time of arrival
    float time_of_last_event = 0;// the time of last event
 
/*****************statistical counters***************/
    int no_delayed = 0;//the number of delayed
    float total_delay = 0;//the total delay time
    float area_under_Q = 0;//the area under queue(t)
    float area_under_B = 0;//the area under busy(t)
    
/********the stop time***/
    char temp;
    int end_event;
    printf("Please input the stop event of this simulating system(a means arrival and d means departure): ");
    scanf("%c", &temp);
    if (temp == 'd')
        end_event = 1;
    else
        end_event = 0;
    int ordinal;
    printf("Please input the customer's ordinal of the stop event you have input:");
    scanf("%d", &ordinal);
    
/***********start********************/
    while(clock_event_times[end_event] < ordinal)
    {
 
        if(clock_event[0]<clock_event[1] )//arrival event
        {
            clock = clock_event[0];
            acount += 1;
            clock_event[0] = arrivals[acount];
            clock_event_times[0] += 1;
            if(serve_status == 0)//no body
            {
                serve_status = 1;
            }
            else
            {
                time_of_arrival[no_in_queue] = clock;
                area_under_B += serve_status * (clock - time_of_last_event);
                area_under_Q += no_in_queue * (clock - time_of_last_event);
                no_in_queue += 1;
            }
 
        }
        else//department event
        {
            clock = clock_event[1];
            dcount += 1;
            clock_event[1] = departures[dcount];
            clock_event_times[1] += 1;
            area_under_B += clock - time_of_last_event;
            if(no_in_queue == 0)
            {
                serve_status = 0;
            }
            else
            {
                clock_event[1] = departures[dcount];
                total_delay += clock - time_of_arrival[0];
                area_under_Q += no_in_queue * (clock - time_of_last_event);
                no_in_queue -= 1;
                time_of_arrival[0] = time_of_arrival[1];
                time_of_arrival[1] = time_of_arrival[2];
                time_of_arrival[2] = time_of_arrival[3];
 
            }
 
        }
        time_of_last_event = clock;
    }
    printf("Number delayed: %d\n", clock_event_times[1]);
    printf("Total delay: %2f\n", total_delay);
    printf("Area under Q(t): %2f\n", area_under_Q);
    printf("Area under B(t): %2f\n", area_under_B);
 
    return 0;
}
上一篇:XCODE 添加不同IOS版本的模拟器


下一篇:archlinux 加载loop模块,且设定loop设备个数