21xrx.com
2024-09-20 00:55:29 Friday
登录
文章检索 我的文章 写文章
C++银行排队服务模拟系统代码
2023-07-07 16:08:37 深夜i     --     --
C++ 银行排队 服务模拟系统 代码实现 模拟器

银行排队服务模拟系统在现实生活中非常有用。使用C++编写的模拟系统,可以轻松模拟整个过程,从顾客进入银行,到他们离开的时间。本文将介绍基于C++编写的银行服务模拟系统的代码。

首先,我们需要定义一些结构体和变量。以下是用于存储信息的结构体:

struct Node

  int id;

  int arrival_time;

  int service_time;

  int start_time;

  int finish_time;

;

其中,id是顾客的编号,arrival_time是顾客到达的时间,service_time是服务所需的时间,start_time是开始服务的时间,finish_time是完成服务的时间。

我们还需要定义一些全局变量:

int customer_num;      //顾客数量

Node *customers;      //存储顾客信息的数组

int teller_num;       //柜员数量

bool *teller_status;    //柜员工作状态

int *queue;         //顾客队列

int queue_front = -1;    //队列头

int queue_rear = -1;    //队列尾

int current_time = 0;    //当前时间

int total_wait_time = 0;  //总等待时间

int total_service_time = 0; //总服务时间

customer_num表示银行接受的顾客数量,customers数组存储顾客的信息,teller_num表示柜员数量,teller_status数组表示柜员的工作状态(空闲或忙碌),queue数组存储顾客队列,queue_front和queue_rear分别表示队列的头和尾,current_time表示当前时间,total_wait_time表示总等待时间,total_service_time表示银行的总服务时间。

接下来,我们将编写一个函数来生成随机的顾客信息:

void generate_customers()

{

  //初始化随机数生成器

  srand(time(NULL));

  //生成每个顾客到达银行的时间和服务所需的时间

  for (int i = 0; i < customer_num; i++)

  {

    customers[i].id = i;

    customers[i].arrival_time = rand() % 601;

    customers[i].service_time = rand() % 61 + 30;

    customers[i].start_time = -1;

    customers[i].finish_time = -1;

    total_service_time += customers[i].service_time;

  }

}

该函数使用rand()生成随机数。随机生成每个顾客到达银行的时间和服务所需的时间,并将其存储在相应的结构体变量中。

接下来,我们将编写一个函数来检查当前所有柜员是否正在工作:

bool all_tellers_busy()

{

  //遍历所有柜员,判断它们是否忙碌

  for (int i = 0; i < teller_num; i++)

  {

    if (!teller_status[i])

      return false;

  }

  return true;

}

该函数遍历所有柜员,如果有柜员空闲,则返回false;否则返回true。

接下来,我们将编写一个函数来将顾客插入队列:

void enqueue(int customer_id)

{

  //如果队列为空,则头和尾都是0

  if (queue_front == -1 && queue_rear == -1)

  {

    queue_front++;

    queue_rear++;

    queue[queue_rear] = customer_id;

  }

  //如果队列不为空,则将队列尾部向后移动一位,插入新顾客

  else

  {

    queue_rear++;

    queue[queue_rear] = customer_id;

  }

}

该函数会将顾客的id插入到队列的末尾。

接下来,我们将编写另一个函数来从队列中删除顾客:

int dequeue()

{

  int customer_id = queue[queue_front];

  //如果只有一个顾客,队列变为空

  if (queue_front == queue_rear)

    queue_front = -1;

    queue_rear = -1;

  //如果队列不只一个顾客,将队列头后移一位

  else

  {

    queue_front++;

  }

  return customer_id;

}

该函数会从队列的头部删除顾客,并返回其id。

接下来,我们将编写一个函数来寻找空闲的柜员:

int find_free_teller()

{

  //遍历所有柜员,返回第一个空闲的柜员编号

  for (int i = 0; i < teller_num; i++)

  {

    if (!teller_status[i])

      return i;

  }

  return -1;

}

该函数会遍历所有柜员,返回第一个空闲的柜员编号;如果没有空闲的柜员,则返回-1。

接下来,我们将编写一个函数来处理顾客:

void serve_customers()

{

  //遍历所有顾客

  for (int i = 0; i < customer_num; i++)

  {

    current_time = customers[i].arrival_time;

    //检查队列中是否还有顾客

    if (queue_front != -1)

    {

      int free_teller_index = find_free_teller();

      //如果有空闲的柜员,从队列中获取第一个顾客

      if (free_teller_index != -1)

      {

        int customer_id = dequeue();

        //更新顾客的开始服务时间和结束服务时间,并更新柜员的状态

        customers[customer_id].start_time = current_time;

        customers[customer_id].finish_time = current_time + customers[customer_id].service_time;

        teller_status[free_teller_index] = true;

        //更新总等待时间

        total_wait_time += current_time - customers[customer_id].arrival_time;

      }

    }

    //遍历所有柜员

    for (int j = 0; j < teller_num; j++)

    {

      //如果柜员正在工作,检查服务是否完成

      if (teller_status[j])

      {

        //如果服务完成,更新柜员状态,并将顾客编号加入到队列中

        if (current_time == customers[j].finish_time)

        {

          teller_status[j] = false;

          enqueue(j);

        }

      }

    }

    current_time++;

  }

}

该函数会遍历所有顾客,根据顾客到达时间、队列状态和柜员状态来处理顾客,并更新统计信息。

最后,我们将编写主函数:

int main()

{

  //获取顾客数量和柜员数量

  cout << "Please enter the number of customers: ";

  cin >> customer_num;

  cout << "Please enter the number of tellers: ";

  cin >> teller_num;

  //分配内存

  customers = new Node[customer_num];

  teller_status = new bool[teller_num];

  queue = new int[customer_num];

  //初始化柜员状态为false表示空闲

  for (int i = 0; i < teller_num; i++)

  {

    teller_status[i] = false;

  }

  //生成随机顾客信息

  generate_customers();

  //处理顾客

  serve_customers();

  //输出统计信息

  cout << "Average waiting time: " << total_wait_time / customer_num << endl;

  cout << "Average service time: " << total_service_time / customer_num << endl;

  //释放内存

  delete[] customers;

  delete[] teller_status;

  delete[] queue;

  return 0;

}

该函数会获取顾客数量和柜员数量,分配内存,调用其他函数来处理顾客,并输出统计信息。最后,释放内存。

综上所述,基于C++编写的银行服务模拟系统可以轻松模拟整个过程。通过以上的代码示例,您可以学习如何使用C++编程语言,实现银行服务模拟系统的代码。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复