Example #1
0
int main(void)
{
    Queue line;
    Item temp;
    char ch;

    InitializeQueue(&line);
    puts("Testing the Queue interface. Type a to add a value,");
    puts("type d to delete a value, and type q to quit.");
    while ((ch = getchar()) != 'q')
    {
        if (ch != 'a' && ch != 'd')   /* ignore other input */
            continue; 
        if ( ch == 'a')
        {
            printf("Integer to add: ");
            scanf("%d", &temp);
            if (!QueueIsFull(&line))
            {
                printf("Putting %d into queue\n", temp);
                EnQueue(temp,&line);
            }
           else
               puts("Queue is full!");
        }
        else
        {
            if (QueueIsEmpty(&line))
                puts("Nothing to delete!");
            else
            {
                 DeQueue(&temp,&line);
                 printf("Removing %d from queue\n", temp);
            }
        }
        printf("%d items in queue\n", QueueItemCount(&line));
        puts("Type a to add, d to delete, q to quit:");
    }
    EmptyTheQueue(&line);
    puts("Bye!");

    return 0;
}
Example #2
0
int main(void)
{
    Queue line;
    Item temp;
    char ch;
    
    InitializeQueue(&line);
    puts("testing the queue interface. type a to  a value");
    puts("type d to delete a value, and type q tp quit.");
    while ((ch = getchar()) != 'q')
    {
        while (ch != 'a' && ch != 'd')
            continue;
        if (ch == 'a')
        {
            printf("integer to add: ");
            scanf("%d",&temp);
            if (!QueueIsFull(&line)) //添加前确认一下队列
            {
                printf("putting %d into queue\n", temp); //重复输入的内容,确认一遍
                EnQueue(temp, &line);
            }
        }
        else
        {
            if (QueueIsEmpty(&line))
                puts("nothing to delete!");
            else
            {
                DeQueue(&temp, &line);
                printf("removing %d from queue\n",temp);
            }
        }
        printf("%d item in queue\n",QueueItemCount(&line));
        puts("type a to add, d to delete, q to quit: ");
    }
    EmptyTheQueue(&line);
    puts("bye!");
    
    return 0;
}
Example #3
0
File: mall.c Project: ludwig123/ADT
int main(void)
{
    Queue line;
    Item temp;   //关于顾客的数据
    int hours;   //模拟的小时数
    int perhours;  //每小时的平均顾客数
    long cycle, cyclelimit; //循环计数器,计数器的上界
    long turnaways = 0;  //因队列已满而被拒绝的顾客数量
    long customers = 0;  //被加入队列的顾客数
    long served = 0;
    long sum_line = 0;  //累计的队列长度
    long wait_time = 0;  //从当前到Sigmund空闲所需的时间
    double min_per_cust;
    long line_wait = 0;
    
    InitializeQueue(&line);
    srand(time(0));
    puts("case study: sigmund lander's advice booth");
    puts("enter the number of simulation hours: ");
    scanf("%d",&hours);
    cyclelimit = MIN_PER_HR * hours;
    puts("enter the average number of customers per hour: ");
    scanf("%d",&perhours);
    min_per_cust = MIN_PER_HR / perhours;
    
    for (cycle = 0; cycle < cyclelimit; cycle++)
    {
        if (newcustomer(min_per_cust))
        {
         if (QueueIsFull(&line))  //如果队列是满的
            turnaways++;
            else  //如果队列不是满的
            {
                customers++;
                temp = customertime(cycle);
                EnQueue(temp, &line);
            }
        }
        
        if (wait_time <= 0 && !QueueIsEmpty(&line)) //如果不需要等待,且队列不是空的;意味着只有一个人!
        {
            DeQueue(&temp, &line);
            wait_time = temp.processtime;
            line_wait += cycle - temp.arrive;
            served++;
        }
        if (wait_time > 0)
            wait_time--;
        sum_line += QueueItemCount(&line);
    }
    
    if (customers > 0)
    {
        printf("customer acccepted: %ld\n",customers);
        printf("customer served:%ld\n",served);
        printf(   "turnaways: %ld\n",turnaways);
        printf("average queue size: %.2f\n",(double)sum_line / cyclelimit);
        printf(" average wait time: %.2f minutes\n",(double)line_wait / served);
    }
    else
        puts("no customer!");
    EmptyTheQueue(&line);
    puts("BYE!");
    
    return 0;
}