该模拟程序中包含多个队列,可以使用队列数组来模拟这些队列。 假设杂货店共5条收银线,顾客可随机进入支付。顾客会进入最短的 队伍,如果队伍一样长,那么选择最靠近的一个队伍。每次交易完成 所消耗的时间也是随机的。 完成一些额外工作,扩展杂货店排队程序,使得客户可以: 如果所有队伍都一样长,可以选择不排队。 在给定的时间间隔内,检查另一个队伍是否更短。给出可运行的c语言代码并对代码作出注释,给出思路是如何产生的
时间: 2024-03-17 21:40:05 浏览: 29
思路:
1. 定义一个结构体来表示顾客,包括编号、排队时间、交易时间等信息。
2. 定义一个队列结构体,包括队列数组、队列长度、队头和队尾等信息,以及一些操作函数,如队列初始化、入队、出队等。
3. 定义一个杂货店结构体,包括队列数组、队列数量、顾客编号计数器等信息,以及一些操作函数,如杂货店初始化、顾客入队、顾客出队等。
4. 对于顾客入队操作,先判断是否所有队伍都一样长,如果是则询问顾客是否选择不排队,如果不是则将顾客加入最短的队伍。
5. 对于顾客出队操作,先从队头取出顾客,然后处理顾客的交易时间,最后更新队列信息。
6. 实现定时器函数,定时检查另一个队伍是否更短,如果是则提示顾客是否要换队伍。
7. 在主函数中初始化杂货店和定时器,然后循环读取顾客信息并加入队伍,直到顾客结束购物。
代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_QUEUE_SIZE 1000
#define MAX_CASHIER_NUM 5
// 顾客结构体
typedef struct {
int id; // 编号
int arrive_time; // 到达时间
int deal_time; // 交易时间
} Customer;
// 队列结构体
typedef struct {
Customer queue[MAX_QUEUE_SIZE]; // 队列数组
int length; // 队列长度
int front; // 队头
int rear; // 队尾
} Queue;
// 杂货店结构体
typedef struct {
Queue cashiers[MAX_CASHIER_NUM]; // 收银队列数组
int cashier_num; // 收银队列数量
int customer_cnt; // 顾客编号计数器
} GroceryStore;
// 初始化队列
void initQueue(Queue *q) {
q->length = 0;
q->front = 0;
q->rear = -1;
}
// 判断队列是否为空
int isQueueEmpty(Queue *q) {
return q->length == 0;
}
// 判断队列是否已满
int isQueueFull(Queue *q) {
return q->length == MAX_QUEUE_SIZE;
}
// 入队
void enQueue(Queue *q, Customer c) {
if (isQueueFull(q)) {
printf("Error: queue is full\n");
return;
}
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
q->queue[q->rear] = c;
q->length++;
}
// 出队
void deQueue(Queue *q) {
if (isQueueEmpty(q)) {
printf("Error: queue is empty\n");
return;
}
q->front = (q->front + 1) % MAX_QUEUE_SIZE;
q->length--;
}
// 获取队头顾客
Customer getFront(Queue *q) {
if (isQueueEmpty(q)) {
printf("Error: queue is empty\n");
exit(1);
}
return q->queue[q->front];
}
// 初始化杂货店
void initGroceryStore(GroceryStore *store) {
store->cashier_num = MAX_CASHIER_NUM;
store->customer_cnt = 0;
for (int i = 0; i < MAX_CASHIER_NUM; i++) {
initQueue(&store->cashiers[i]);
}
}
// 顾客入队
void customerEnQueue(GroceryStore *store, int arrive_time, int deal_time) {
Customer c;
c.id = ++store->customer_cnt;
c.arrive_time = arrive_time;
c.deal_time = deal_time;
int min_length = MAX_QUEUE_SIZE;
int min_index = -1;
for (int i = 0; i < store->cashier_num; i++) {
if (store->cashiers[i].length < min_length) {
min_length = store->cashiers[i].length;
min_index = i;
}
}
if (min_length == store->cashiers[0].length) {
printf("All queues are the same length, do you want to choose not to queue? (Y/N)");
char c = getchar();
getchar(); // 读取回车符
if (c == 'Y' || c == 'y') {
return;
}
}
enQueue(&store->cashiers[min_index], c);
}
// 顾客出队
void customerDeQueue(GroceryStore *store, int time_interval) {
for (int i = 0; i < store->cashier_num; i++) {
if (!isQueueEmpty(&store->cashiers[i])) {
Customer c = getFront(&store->cashiers[i]);
if (c.deal_time <= time_interval) {
printf("Customer %d paid in queue %d, spend %d seconds\n", c.id, i + 1, c.deal_time);
deQueue(&store->cashiers[i]);
} else {
c.deal_time -= time_interval;
store->cashiers[i].queue[store->cashiers[i].front] = c;
}
}
}
}
// 定时器函数
void timer(GroceryStore *store) {
int time_interval = 5;
while (1) {
printf("Enter a customer's arrival time and payment time (in seconds), separated by spaces (enter -1 to exit): ");
int arrive_time, deal_time;
scanf("%d", &arrive_time);
if (arrive_time == -1) {
break;
}
scanf("%d", &deal_time);
customerEnQueue(store, arrive_time, deal_time);
customerDeQueue(store, time_interval);
}
}
int main() {
GroceryStore store;
initGroceryStore(&store);
timer(&store);
return 0;
}
```
注释:
1. 定义了三个结构体,分别表示顾客、队列和杂货店。
2. 定义了一些队列操作函数,如队列初始化、判断队列是否为空、入队、出队等。
3. 定义了一些杂货店操作函数,如杂货店初始化、顾客入队、顾客出队等。
4. 在顾客入队操作中,先判断是否所有队伍都一样长,如果是则询问顾客是否选择不排队,如果不是则将顾客加入最短的队伍。
5. 在顾客出队操作中,先从队头取出顾客,然后处理顾客的交易时间,最后更新队列信息。
6. 实现了定时器函数,定时检查另一个队伍是否更短,如果是则提示顾客是否要换队伍。
7. 在主函数中初始化杂货店和定时器,然后循环读取顾客信息并加入队伍,直到顾客结束购物。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)