c中具有多个客户服务的银行队列模拟



我有一个任务,我应该制作一个队列程序,计算每个客户服务最终为多少客户提供服务(这个程序有2个客户服务(。我尝试使用此代码,但当我尝试打印cs->count时,它对屏幕没有任何影响。

对于队列,我使用了通用链表队列,我确信它没有任何问题

我拥有的第一个代码是这个adt结构

typedef struct{
int ID;
int time;
int count;
bool serving;
}employee;
typedef struct{
char ID[3];
int code;
int arrivalTime;
int timeNeeded;
employee servedBy;
}nasabah;

这个adt的模块是:

nasabah inputNasabah(nasabah *n) // to initialize customer's data
{
printf("Customer's ID : ");
scanf("%s", (*n).ID);
printf("Customer's need (type '1' for teller or '2' for Customer Service) : ");
scanf("%d", &((*n).code));
printf("Enter your arrvial time : ");
scanf("%d", &((*n).arrivalTime));
printf("Transaction time needed : ");
scanf("%d", &((*n).timeNeeded));
printf("n");
return *n;
}

编辑:现在我认为问题从这里开始,我真的不知道为什么它没有像预期的那样运行。

void csInit(employee *a, employee *b) //to initialize cs's data
{
a->ID = 1;
a->time = 0;
a->count = 0;
a->serving = false;
b->ID = 2;
b->time = 0;
b->count = 0;
b->serving = false;
}

还有这个(我认为是这个问题(。此模块用于分配为客户提供的客户服务以及客户服务服务的客户数量。我的逻辑是,第一个客户将去"a"(第一个客户服务(,a->服务将成立,下一个客户将在"b"(第二个客户服务,因为"a"还不可用(假设第一个和第二个顾客到达时间相同(,b->服务也将成立。然后第三个客户来了,将由一个时间最小的客户服务(通过查看a->时间或b->时间(

void servedByCS(nasabah *n, employee *a, employee *b) 
{
csInit(a, b);
if(a->serving == false)
{
n->servedBy = *a;
a->time = a->time + n->timeNeeded;
a->count = a->count + 1;
a->serving = true;
}
else if(a->serving == true)
{
if(a->time > b->time)
{
n->servedBy = *b;
b->time = b->time + n->timeNeeded;
b->count = b->count + 1;
b->serving = true;
}
if(a->time < b->time)
{
a->serving = false;
n->servedBy = *a;
a->time = a->time + n->timeNeeded;
a->count = a->count + 1;
a->serving = true;
}
}
if (b->serving == true)
{
if (b->time < a->time)
{
b->serving = false;
}
}    
}

最后我的主要驱动程序包含:

int main()
{
nasabah *n;
antre cs; //queue for customer 
int i;
int amount = 0;
node a;
employee *cs1, *cs2; //since there are two customer service
printf("Input the queue lenghth : ");
scanf("%d", &amount);
n = (nasabah *) malloc(amount * sizeof(nasabah));
cs = CreateQueue(); //creating queue 
for(i = 0; i < amount; i++)
{
*n = inputNasabah(n + i);
enQueue(cs, *n); //inputting customer's data into the queue
}
while(!isEmpty(cs))
{
a = deQueue(cs); //dequeue from the first customer so it can be proceed to see which customer service will be served.
servedByCS(&a.info, cs1, cs2); //i think i got this one wrong so the program didn't work
}
printf("%d", cs1->count); //i try to print how many customers are served by the cs1 (first customer service), but it didn't work.
return 0;
}

您的主要问题是:

  • 未分配员工
  • servedByCS,如果结构复杂,并且不能同时工作2个客户,则缺少其他客户

我已经添加了队列,nasabah是按到达时间顺序插入的。

我去掉发球区,因为它没用我已经将一些字段从值更改为指针

缺少,检查scanf中的值,检查malloc,释放malloc内存

typedef struct{
int ID;
int time;
int count;
}employee;
typedef struct nasabah nasabah;
struct nasabah{
char ID[64];
int code;
int arrivalTime;
int timeNeeded;
employee *servedBy;
nasabah *next;
};
static nasabah* inputNasabah(nasabah *n) // to initialize customer's data
{
printf("Customer's ID : ");
scanf("%s", n->ID);
printf("Customer's need (type '1' for teller or '2' for Customer Service) : ");
scanf("%d", &n->code);
printf("Enter your arrival time : ");
scanf("%d", &n->arrivalTime);
printf("Transaction time needed : ");
scanf("%d", &n->timeNeeded);
printf("n");
return n;
}
static void employeeInit(employee *e, int id)
{
e->ID = id;
e->time = 0;
e->count = 0;
}

static void serve(nasabah *n, employee *e) {
n->servedBy = e;
e->count++;
if (n->arrivalTime > e->time) {
/* if customer arrived after the employee has finished */
e->time = n->arrivalTime + n->timeNeeded;
} else {
e->time += n->timeNeeded;
}
}
static void servedByCS(nasabah *n, employee *a, employee *b)
{
serve(n, a->time <= b->time ? a : b);
}
static void addNasabah(nasabah **root, nasabah *n) {
nasabah *current = *root;
if (!current || n->arrivalTime < current->arrivalTime) {
n->next = current;
*root = n;
return;
}
while (current->next && current->next->arrivalTime < n->arrivalTime) {
current = current->next;
}
n->next = current->next;
current->next = n;
}
int main()
{
int i;
int amount = 0;
nasabah *nasabahs = NULL;
employee *cs1 = malloc(sizeof(employee));
employee *cs2 = malloc(sizeof(employee));
employeeInit(cs1, 1);
employeeInit(cs2, 2);
printf("Input the queue length : ");
scanf("%d", &amount);
for(i = 0; i < amount; i++)
{
nasabah *n = malloc(sizeof(nasabah));
inputNasabah(n);
addNasabah(&nasabahs, n);
}
for (nasabah *n = nasabahs; n; n=n->next) {
servedByCS(n, cs1, cs2);
}
printf("%dn", cs1->count);
return 0;
}

测试

Input the queue length : 3
Customer's ID : 1
Customer's need (type '1' for teller or '2' for Customer Service) : 1
Enter your arrival time : 0
Transaction time needed : 10
Customer's ID : 2
Customer's need (type '1' for teller or '2' for Customer Service) : 1
Enter your arrival time : 1
Transaction time needed : 3
Customer's ID : 3
Customer's need (type '1' for teller or '2' for Customer Service) : 1
Enter your arrival time : 6
Transaction time needed : 5
1

Input the queue length : 5
Customer's ID : 1
Customer's need (type '1' for teller or '2' for Customer Service) : 1
Enter your arrival time : 0
Transaction time needed : 10
Customer's ID : 2
Customer's need (type '1' for teller or '2' for Customer Service) : 1
Enter your arrival time : 7
Transaction time needed : 2
Customer's ID : 3
Customer's need (type '1' for teller or '2' for Customer Service) : 1
Enter your arrival time : 2
Transaction time needed : 6
Customer's ID : 4
Customer's need (type '1' for teller or '2' for Customer Service) : 1
Enter your arrival time : 8
Transaction time needed : 5
Customer's ID : 5
Customer's need (type '1' for teller or '2' for Customer Service) : 1
Enter your arrival time : 9
Transaction time needed : 2
2

最新更新