【问题标题】:Bank Queue Simulation with multiple customer services in c在 c 中具有多个客户服务的银行队列模拟
【发布时间】: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]

标签: c queue


【解决方案1】:

您的主要问题是:

  • 员工未分配
  • 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("%d\n", 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

【讨论】:

  • 您好,也非常感谢您更正我的代码。我真的很抱歉,因为我现在才找到你的答案。再次感谢您对我的帮助!
  • 如果答案很好,您可以将其标记为已解决,如果没有,请毫不犹豫地指出问题,我们很乐意提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多