首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于C语言的多客户服务银行队列仿真

基于C语言的多客户服务银行队列仿真
EN

Stack Overflow用户
提问于 2020-04-19 02:34:34
回答 1查看 794关注 0票数 0

我有这样的任务,我应该建立一个队列程序,计算每个客户服务最终为多少客户服务(这个程序有2个客户服务)。我尝试使用这段代码,但是当我试图打印cs->count时,它并没有给屏幕提供任何信息。

对于队列,我使用了一般链接列表队列,并且我很确定它没有任何问题。

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

代码语言:javascript
复制
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的模块是:

代码语言:javascript
复制
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;
}

编辑:现在我想问题就从这里开始,我真的不知道为什么它不像应该的那样运行。

代码语言:javascript
复制
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->时间)。

代码语言:javascript
复制
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;
     }
   }    
}

最后,我的主要司机包括:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-19 22:53:03

你们的主要问题是:

如果结构复杂且没有同时工作2名客户,则allocated

  • servedByCS
  • 的员工不是

,否则就缺少

我增加了队列,并按到达时间的顺序插入了nasabah。

我移除服务字段,因为它是无用的,我已经将一些字段从值更改为指针。

丢失,检查scanf中的值,检查malloc,释放malloc内存

代码语言:javascript
复制
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;
}

测试用

代码语言:javascript
复制
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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61298845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档