【发布时间】:2020-04-19 02:34:34
【问题描述】:
我有这个任务,我应该制作一个队列程序,计算每个客户服务最终服务了多少客户(这个程序有 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;
这个广告的模块是:
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->serving 将是真的,下一个客户将在“b”(第二个客户服务),因为“a”还没有available(假设第一个和第二个客户的到达时间相同)并且 b->serving 也将是 true。然后第三个客户来了,将由一个时间最短的客户服务来服务(通过查看 a->time 或 b->time)
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;
}
【问题讨论】:
-
尝试在您的
printf中添加\n -
谢谢你的回答,但还是不行
-
char ID[3];可能太小了。试试char ID[300]; -
感谢您的回答,但我认为这不是问题,因为我只需要像 001、045 这样的小 ID ..
-
001 不适合
char[3],因为您没有空间用于空终止符。三个字符的字符串需要char[4]。