c++模拟银行排队系统

银行排队系统的实现,利用了一个STL库中的向量,使得问题变得简单,因老师不要求实现vip用户,因此没有该功能,但实现也并不困难,利用向量可以在任意位置插入的性质,若有vip用户到达,可以直接插队到前一个vip用户的后方。

此代码写得较为复杂,在写代码前并未画流程图,因此花了许多时间,下次应多注意该问题。

#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <vector>
#define end_time 100 //该银行的工作总时间
using namespace std;
int last_time = 0;//记录最后一个人的到达时间
int index = 0;
class Customer
{
public:
    Customer(int cost,int arrive):cost_time(cost),arrive_time(arrive){}
    ~Customer(){}
    int get_cost_time() {return cost_time;}
    int get_arrive_time() {return arrive_time;}
    int get_indexcounter() {return index_counter;}
private:
    int cost_time;
    int arrive_time;
    int index_counter;
};
struct Counter
{
	bool isfull;
	int arrive_time;
	int cost_time;
}counter[4] = { {false,0,0},{ false,0,0 },{ false,0,0 },{ false,0,0 } };
void bank_service(vector<Customer>& vector_wait,struct Counter* p);
int search_last(struct Counter* p);
int check_workcounter(struct Counter* p);

int main()
{
	vector<Customer> wait_vector;
	bank_service(wait_vector,counter);
	return 0;
}
int check_workcounter(struct Counter* p)
{
	int count = 0;
	for (int i = 0;i < 4;i++)
	{
		if (p[i].isfull == true)
		{
			count++;
		}
	}
	return count;
}
int search_last(struct Counter* p)//记录柜台中最早结束顾客的具体时间
{
	int last_overtime = 999999999;
	index = 0;
	for (int i = 0;i < 4;i++)
	{
		if (counter[i].arrive_time + counter[i].cost_time <= last_overtime&&counter[i].isfull == true)
		{
			last_overtime = counter[i].arrive_time + counter[i].cost_time;
			index = i;
		}
	}
	return last_overtime;
}
void bank_service(vector<Customer>& vector_wait, struct Counter* p)
{
	static int num = 0;//记录总人数
	static int sum_time = 0;//记录工作总时间
	int last_overtime; //柜台中顾客最先结束的时间
	bool is_full = false;//柜台是否都为满
	int count = 0;
	srand((unsigned)time(NULL));
	while (1)
	{
		num++;
		if (vector_wait.size() == 0)//如果队列中没有顾客排队
		{
			Customer temp_customer(rand() % 10, last_time + rand() % 4);
			if (temp_customer.get_arrive_time() + temp_customer.get_cost_time() > end_time)//超过10000分钟直接跳出
			{
				while (check_workcounter(counter) != 0)
				{
					last_overtime = search_last(counter);
					cout << "时间为" << search_last(counter) << "时,一位顾客离开了" << index << "号柜台" << endl;
					counter[index].isfull = false;
				}
				cout << "今天接客" << num - 1 << "个人" << endl;
				cout << "平均服务时间为" << sum_time / (num - 1) << endl;
				cout << "下班了!!!!" << endl;
				return;
			}
			sum_time += temp_customer.get_cost_time();
			count = check_workcounter(counter);
			while (temp_customer.get_arrive_time() >= search_last(counter)&&count != 0)
			{
				cout << "时间为" << search_last(counter) << "时,一位顾客离开了" << index << "号柜台" << endl;
				counter[index].isfull = false;
				is_full = false;
				count--;
			}
			if (is_full == false)//如果柜台有空位
			{
				for (int i = 0;i < 4;i++)//让新顾客直接进入一个空柜台
				{
					if (counter[i].isfull == false)
					{
						counter[i].isfull = true;
						counter[i].arrive_time = temp_customer.get_arrive_time();
						counter[i].cost_time = temp_customer.get_cost_time();
						last_time = counter[i].arrive_time;
						cout << "时间为" << temp_customer.get_arrive_time() << "时,有一位顾客到达了,并直接进入了" << i << "号柜台" << endl;
						break;
					}
				}
				int count = 0;     //检查柜台是否都为满
				count = check_workcounter(counter);
				if (count == 4)
				{
					is_full = true;
				}
			}
			else
			{
				last_overtime = search_last(counter);
				if (temp_customer.get_arrive_time() >= last_overtime)
				{
					cout << "时间为" << last_overtime << "时,有一位顾客离开了" << index << "号柜台" << endl;
					counter[index].arrive_time = temp_customer.get_arrive_time();
					counter[index].cost_time = temp_customer.get_cost_time();
					last_time = counter[index].arrive_time;
					cout << "时间为" << temp_customer.get_arrive_time() << "时,有一位顾客直接进入了" << index << "号柜台" << endl;

				}
				else
				{
					vector_wait.push_back(temp_customer);
					last_time = counter[index].arrive_time;
					cout << "时间为" << temp_customer.get_arrive_time() << "时,有一位顾客进入了队列" << endl;
				}
			}
		}
		else//如果队列中有顾客排队
		{
			Customer temp_customer(rand() % 27, last_time + rand() % 4);
			sum_time += temp_customer.get_cost_time();
			last_overtime = search_last(counter);
			while(temp_customer.get_arrive_time() >= last_overtime&& vector_wait.size() != 0)
			{
				last_overtime = search_last(counter);
				cout << "时间为" << counter[index].arrive_time + counter[index].cost_time << "时,有一位顾客离开了" << index << "号柜台" << endl;
				cout << "时间为" << counter[index].arrive_time + counter[index].cost_time << "时,有一位顾客从队列中进入了" << index << "号柜台" << endl;
				counter[index].arrive_time = vector_wait[0].get_arrive_time();
				counter[index].cost_time = vector_wait[0].get_cost_time();
				vector_wait.erase(vector_wait.begin());
				last_overtime = search_last(counter);
			}
			if(temp_customer.get_arrive_time() + temp_customer.get_cost_time() <= end_time && temp_customer.get_arrive_time() < last_overtime)
			{
				vector_wait.push_back(temp_customer);
				last_time = temp_customer.get_arrive_time();
				cout << "时间为" << temp_customer.get_arrive_time() << "时,有一位顾客进入了队列" << endl;
			}
		}
	}
}



阅读更多
个人分类: 上课的小题目
上一篇c++栈的应用 中缀表达式求值
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

c++模拟银行排队系统

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭