多窗口排队系统

本文介绍了一个多窗口排队系统的实现,包括链队结构的定义、入队、出队等操作,并展示了控制台界面的交互功能,如取号、查看窗口状态、办理结束等。系统通过链队维护每个窗口的等待队列,记录处理时间,可统计窗口效率。
摘要由CSDN通过智能技术生成

多窗口排队系统

1、头文件1:WindowQueue.

/**
*定义链队结点结构
*/
struct Lnode
{
	int data;
	time_t nowTime;
	Lnode *next;
};

/**
*定义链队头结点结构
*/
struct Hnode
{
	Lnode *front;
	Lnode *rear;
};

/**
*定义排队链队结构
*/
struct Lqueue                               
{
	Hnode *head;						 //定义头结点
	int Len();                           //记录人数
	void Init();						 //初始化链队
	bool IsEmpty();						 //判空
	void EnterQueue(int x);				 //如队
	bool OutQueue(int &e,time_t &t);	 //出队
	int GetHeadElem(int &e);			 //获取头元素
	int SumTime();                       //统计各个窗口处理总时间
};

int Lqueue::GetHeadElem(int &e)
{
	Lnode *p;
	if (IsEmpty()) return 0;
	p=head->front;
	e=p->data;
	return e;
}

int Lqueue::SumTime()        
{
	Lnode *p;
	int stime = 0;
	p=head->front;
	while (p)
	{
		stime += p->data;
		p=p->next;
	}
	return stime;
}

int Lqueue::Len()
{
	Lnode *p;
	int leng=0;
	p=head->front;
	while (p)
	{
		leng++;
		p=p->next;
	}
	return leng;
}

bool Lqueue::OutQueue(int &e,time_t &t)
{
	Lnode *p;
	if (IsEmpty()) false;
	p=(Lnode *)malloc(sizeof(Lnode));
	p=head->front;
	if (p->next)
	{
		e=p->data;
		t = (int)difftime(time(NULL),p->nowTime);        //获取出队时的时间
		head->front=p->next;
		free(p);
	}
	else
	{
		e=p->data;
		t = (int)difftime(time(NULL),p->nowTime);         //获取出队时的时间
		free(p);
		head->front=NULL;
		head->rear=NULL;
	}
	return true;
}

void Lqueue::EnterQueue(int x)
{
	Lnode *p;
	p=(Lnode *)malloc(sizeof(Lnode));
	p->nowTime = time(NULL);                 //获取入队时的时间
	p->data=x;                               
	p->next=NULL;
	if (IsEmpty())
	{
		head->front=p;
		head->rear=p;
	}
	else
	{
		head->rear->next=p;
		head->rear=p;
	}
}

bool Lqueue::IsEmpty()
{
	if (head->front==NULL) return true;
	return false;
}

void Lqueue::Init()
{
	head=(Hnode *)malloc(sizeof(Hnode));
	head->front=NULL;
	head->rear=NULL;
}

2、头文件2:Function.h

/**
*控制字符颜色
*/
int color(int c)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
	return 0;
}

/**
*控制字符的显示位置
*/
void gotoxy(int x,int y)
{
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}

/**
*窗口菜单,显示各个窗口信息
*/
void menu1(Lqueue L[])
{
	gotoxy(20,5);
	color(1);
	printf("--------------------------------------------");
	for (int i = 6; i <= 14; i++)
	{
		gotoxy(32,i);
		printf("|");
		gotoxy(50,i);
		printf("|");
	}
	gotoxy(20,15);
	printf("--------------------------------------------");
	gotoxy(20,6);
	color(7);
	printf("(0号窗口)\t      (1号窗口)\t       (2号窗口)");
	gotoxy(20,8);
	color(1);
	printf("--------------------------------------------");
	color(5);
	gotoxy(20,9);
	printf("%d人在等待\t     %d人在等待\t      %d人在等待",L[0].Len(),L[1].Len(),L[2].Len());
	color(1);
}

/**
**选择菜单
*/
void menu2() 
{
	gotoxy(20,16);
	printf("1、取号办理业务    2、查看当前窗口业务办理情况");
	gotoxy(20,18);
	printf("3、查看排队情况    4、结束办理");
	gotoxy(20,20);
	printf("5、退出            6、查看窗口办理效率");
}


/**
*取业务号,排队函数
*/
void quhao(Lqueue L[])
{
	int i,length;
	while (1)
	{
		gotoxy(20,16);
		color(2);
		printf("请选择窗口(0,1,2)[ ]\b\b");
		color(6);
		scanf("%d",&i);
		color(1);
		if ( i == 0 || i == 1 || i == 2)
		{
			length = (L[i].Len()) + 1;
			system("cls");
			L[i].EnterQueue(length);
			gotoxy(20,16);
			printf("办理业务中,您的业务号是%d,您排在第%d个\n",L[i].Len(),L[i].Len());  //输处各个窗口的等待人数,实质就是队列元素个数
			break;
		} else {
			system("cls");
			gotoxy(20,16);
			color(12);
			printf("输入有误!请重新输入\n");
			color(1);
			gotoxy(20,17);
			system("pause");
			system("cls");
			menu1(L);
		}
	}
}

/**
*查看各窗口业务办理详情函数
*/
void chakan(Lqueue L[])
{
	int i,e;
	gotoxy(20,5);
	color(1);
	printf("--------------------------------------------");
	for (i = 6; i <= 14; i++)
	{
		gotoxy(32,i);
		printf("|");
		gotoxy(50,i);
		printf("|");
	}
	gotoxy(20,15);
	printf("--------------------------------------------");
	gotoxy(20,6);
	color(7);
	printf("(0号窗口)      (1号窗口)       (2号窗口)");
	color(1);
	gotoxy(20,8);
	printf("--------------------------------------------");
	gotoxy(20,9);  
	color(5);
	printf("%d号顾客在办理  %d号顾客在办理     %d号顾客在办理",L[0].GetHeadElem(e),L[1].GetHeadElem(e),L[2].GetHeadElem(e)); //查看当前窗口正在办理业务的用户,实质上就是取出队首元素
	gotoxy(20,16);
	color(4);
	printf("【注:0号即为该窗口处于空闲状态】");
	color(1);
}

/**
*完成业务办理,结束业务处理函数
*/
void endWork(Lqueue L[],Lqueue Lt[])
{
	int e,e2,i;								   //e问获取当前窗口办理顾客的业务号,e2为办理结束的顾客的业务号
	time_t t;								   //t 为日历时间类型变量用于接收用户从办理开始到办理结束所用时间
	while (1)                       
	{
		gotoxy(20,16);
		color(2);
		printf("请选择窗口(0,1,2)[ ]\b\b");
		color(6);
		scanf("%d",&i);						   //需要结束业务办理的用户选择自己所在窗口
		color(1);
		if ( i == 0 || i == 1 || i == 2)       //只设置了3个窗口,所以有3个条件
		{
			system("cls");                     //清屏
			gotoxy(20,16);
			if (L[i].IsEmpty())                //判断该窗口是否有用户办理,如果没有则返回主界面
			{

			} else {
				printf("您的业务号是%d:\n",L[i].GetHeadElem(e));    //结束办理前输出了业务号
				L[i].OutQueue(e2,t);                                 //出队
				gotoxy(20,17);
				printf("已结束办理,办理业务消耗:%d秒,请迅速离开!",t);   //结束办理并打印办理时间 
				Lt[i].EnterQueue(t);
				gotoxy(20,18);
				system("pause");
			}
			break;
		} else {
			system("cls");
			gotoxy(20,16);
			color(12);
			printf("输入有误!请重新输入\n");
			color(1);
			gotoxy(20,17);
			system("pause");
			system("cls");
			menu1(L);
		}
	}
}

/**
*查看各个窗口处理业务的效率函数
*/
void xiaolv(Lqueue Lt[])
{
	int i;
	gotoxy(20,5);
	color(1);
	printf("--------------------------------------------");
	for (i = 6; i <= 14; i++)
	{
		gotoxy(32,i);
		printf("|");
		gotoxy(50,i);
		printf("|");
	}
	gotoxy(20,15);
	printf("--------------------------------------------");
	gotoxy(20,6);
	color(7);
	printf("(0号窗口)      (1号窗口)       (2号窗口)");
	color(1);
	gotoxy(20,8);
	printf("--------------------------------------------");
	gotoxy(20,12);  
	color(5);
	int L1 = Lt[0].Len();
	int L2 = Lt[1].Len();
	int L3 = Lt[2].Len();
	float x1, x2, x3;

	if (L1 == 0)
		x1 = 0;
	else
		x1 = (Lt[0].SumTime() * 0.1) / L1;

	if (L2 == 0)
		x2 = 0;
	else
		x2 = (Lt[1].SumTime() * 0.1)/L2;

	if (L3 == 0)
		x3 = 0;
	else
		x3 = (Lt[2].SumTime() * 0.1)/L3;

	printf("处理效率%0.2f   处理效率%0.2f     处理效率%0.2f",x1,x2,x3); //查看当前窗口正在办理业务的用户,实质上就是取出队首元素
	gotoxy(20,16);
	color(4);
	printf("【注:当窗口效率低于0.00时可认为该窗口空闲或该窗口业务处理效率高】");
	printf("测试:窗口3的人数是:%d 窗口1人数是:%d\n",L3,L1);
	color(1);
}

4、main函数:LineUp.c

# include <stdio.h>
# include <stdlib.h>
# include <windows.h>
# include <conio.h>
# include <time.h>
# define WINDOWSIZE 2               //定义窗口数
# include "WindowQueue.h"
# include "Function.h"

void main()
{
	char str;
	static Lqueue L[WINDOWSIZE];    //定义Lqueue类型的数组L用作排队窗口
	static Lqueue Lt[WINDOWSIZE];   //定义Lqueue类型的数组Lt用于统计各个窗口处理业务的时间,人数及其处理业务效率

	for (int j = 0; j <= WINDOWSIZE; j++)   //初始化队列
	{
		L[j].Init();
		Lt[j].Init();
	}
	while (1)                       //程序开始
	{
		gotoxy(20,2);
		color(1);
		printf("--------------------------------------------");
		gotoxy(35,3);
		printf("多窗口排队系统");
		menu1(L);                   //调用显示窗口菜单
		menu2();                    //调用选择菜单
		gotoxy(20,22);              //电泳该函数控制文字显示位置
		color(2);
		printf("请输入序号[ ]\b\b");
		color(6);
		str = getchar();
		color(1);
		switch (str)                   //选择开关
		{
		case '1':                       //调用用户取号入队模块
			system("cls");
			menu1(L);
			quhao(L);
			gotoxy(20,17);
			system("pause");
			system("cls");
			break;
		case '2':                       //调用窗口办理业务状态模块
			system("cls");
			gotoxy(30,4);
			printf("各窗口正在办理业务顾客情况");
			chakan(L);
			gotoxy(20,17);
			system("pause");
			system("cls");
			break;
		case '3':                        //调用查看窗口排队人数情况模块
			system("cls");
			gotoxy(35,4);
			printf("各窗口排队情况");
			menu1(L);
			gotoxy(20,16);
			system("pause");
			system("cls");
			break;
		case '4':                        //调用业务办理完毕出队模块
			system("cls");
			menu1(L);
			endWork(L,Lt);
			system("puase");
			system("cls");
			break;
		case '5':                        //退出
			gotoxy(20,20);
			printf("欢迎你下次继续使用,再按一次退出!\t\t");
			exit(0);
			break;
		case '6':
			system("cls");
			gotoxy(30,4);
			printf("各窗口正在办理业务效率(秒/人)");
			xiaolv(Lt);
			gotoxy(20,17);
			system("pause");
			system("cls");
			break;
		default:;                    //输入非法字符时直接回到主菜单
			break;
		}
	}
}

3、运行结果

运行结果

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值