#2
crystall2012-12-14 16:48
程序代码: //在你的代码的基础上进行了修改,增加,有什么问题再讨论。 QQ:2495689642 #include "stdafx.h" #include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<string.h> #include<time.h> //窗口数量为3个 #define JobWindow 3 //最多排队顾客数为30 #define QUEUE_MAX 30 //忙碌 #define BeBusy 0 //空闲 #define Idle 1 //暂停 #define Pause 2 static int g_nStatic = 0; struct CWindow { int nState; //窗口状态 (忙碌,空闲, 暂停) }; struct CNode { CNode(int ndata = 0) { nData = ndata; pPrev = pNext = NULL; } int nData; CNode *pPrev; CNode *pNext; }; struct CQueue { CNode* pHead; CNode* pTail; int nCount; //已排队的人数 }; //初始化 void Init(CQueue* pBankQueue) { if(pBankQueue != NULL) { pBankQueue->pHead = pBankQueue->pTail = NULL; pBankQueue->nCount = 0; } } //入队列 尾部加入 void EnQueue(CQueue* pBankQueue) { if(pBankQueue == NULL) { return; } CNode* pNewNode = (CNode*)malloc(sizeof(CNode)); pNewNode->nData = g_nStatic; //首结点 if (pBankQueue->pHead == NULL) { pBankQueue->pHead = pNewNode; pBankQueue->pHead->pPrev = NULL; pBankQueue->pHead->pNext = NULL; } else if(pBankQueue->pTail != NULL) { pNewNode->pPrev = pBankQueue->pTail; pBankQueue->pTail->pNext = pNewNode; } //尾结点 pBankQueue->pTail = pNewNode; pBankQueue->nCount++; g_nStatic++; } //出队列 从头部出 void DeQueue(CQueue* pBankQueue) { if(pBankQueue == NULL) { return; } CNode* pNewNode = NULL; //当前只有一个结点 首位结点相等 if (pBankQueue->pHead == pBankQueue->pTail) { if (pBankQueue->pHead != NULL) { pNewNode = pBankQueue->pHead; pBankQueue->pHead = pBankQueue->pTail = NULL; pBankQueue->nCount--; delete pNewNode; pNewNode = NULL; } } if (pBankQueue->pHead != NULL) { pNewNode = pBankQueue->pHead; pBankQueue->pHead = pBankQueue->pHead->pNext; pBankQueue->nCount--; delete pNewNode; pNewNode = NULL; } } //取号 以及等待 办理业务 int TakeNumber(CQueue* pBankQueue, CWindow AryWindow[]) { char ch; int nIdle = -1; //空闲窗口ID //取号 printf("请输入“#”字符获取排队号,其他字符无效:\t"); fflush(stdin); scanf("%c", &ch); if(ch != '#') { printf("输入有误!\r\n"); return -1; } //来了一个客户 入队列 尾部加入 EnQueue(pBankQueue); printf("请输入“&”获取窗口号: "); fflush(stdin); scanf("%c", &ch); if(ch != '&') { printf("字符输入错误\r\n"); return -1; } //当有客户等待时 //如果有空闲的窗口, 安排客户去空闲窗口办理业务 //无空闲的窗口, 请客户耐心等待 for(int i = 0; i < JobWindow; i++) { //如果有空闲的窗口,安排客户去空闲窗口办理业务 if(AryWindow[i].nState == Idle) { nIdle = i+1; break; } } if(nIdle != -1) { printf("请到第%d个窗口办理业务.\r\n ", nIdle); //这里只是意思一下,没有设定每个客户办理业务的时间 //一个客户办理完成 DeQueue(pBankQueue); } else { printf("请客户耐心等待.\r\n"); } return 0; } //释放空间 void Clear(CQueue* pBankQueue) { CNode* pTempNode = pBankQueue->pHead; while(pTempNode) { CNode* pDelNode = pTempNode->pNext; if (pTempNode) { delete pTempNode; pTempNode = pDelNode; } } pBankQueue->pHead = pBankQueue->pTail = NULL; pBankQueue->nCount = 0; } int main(int argc, char* argv[]) { //初始化3个窗口均为空闲状态 CWindow AryWindow[3] = {Idle, Idle, Idle}; //建立队列 CQueue* pBankQueue = (CQueue*)malloc(sizeof(CQueue)); if(pBankQueue == NULL) { return 0; } //初始化队列 Init(pBankQueue); int nInput = 0; while(1) { printf("****************************\n"); printf(" 简易银行客户排队系统 \n"); printf(" 1.取号 \n"); printf(" 0.退出 \n"); printf("****************************\n"); fflush(stdin); scanf("%d", &nInput); if(nInput == 1) { //排队等待顾客人数在30人以内 if(pBankQueue->nCount >= QUEUE_MAX) { printf("排队等待顾客数已超过30, 系统禁止取票\r\n"); break; } //取号 以及等待 办理业务 int nRet = TakeNumber(pBankQueue, AryWindow); //返回值为-1表示 取号 以及等待 办理业务 过程出错 if(nRet == -1) { break; } } else if(nInput == 0) { printf("谢谢使用!\r\n"); break; } else { printf("输入有误!\r\n"); } } //释放空间 if(pBankQueue != NULL) { if(pBankQueue->nCount > 0) { Clear(pBankQueue); } free(pBankQueue); pBankQueue = NULL; } return 0; } |
问题描述】
目前银行、医院等单位通过“叫号机”模拟了人群排队的过程,用户取票进队。叫号排队是队列应用的一个典型例子,设计一个程序,演示叫号排队系统的实现过程。
【基本要求】
当客户到达银行时,先取号(显示当前的客户号,以及正在等待的客户人数)。当有窗口空闲时,若有等待的客户,则显示信息:请XXXX号客户到YY号窗口办理。设定窗口数量为3个,允许的最多排队等待顾客数为30,超过30则系统禁止取票。
我做不下去了,指导下我吧
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#define N 30 ///队列最大人数
#define w 3 ///银行窗口个数
typedef struct Node{
int data;
struct Node *next;
}node;
typedef struct {
node * front;
node * rear;
}queue;
void initqueue(queue *q)
{
q->front=q->rear=(node*)malloc(sizeof(node));
if(!q->front) exit(0);
q->front->next=NULL;
}
void destroyqueue(queue *q)
{
while(q->front){
q->rear=q->front->next;
free(q->front);
q->front=q->rear;
}
printf("队列销毁成功\n");
}
void enterqueue(queue *q,int a)
{
node *p;
p=(node*)malloc(sizeof(node));
if(!p) exit(0);
p->data=a;
p->next=NULL;
q->rear->next=p;
q->rear=p;
printf("\t您的号码为%d\t",a);
}
void deletequeue(queue *q,int a)
{
node *p;
p=(node*)malloc(sizeof(node));
if(q->front=q->rear) { printf("队列为空\n");exit(0);}
p=q->front->next;
a=p->data;
q->front->next=p->next;
if(q->rear==p) q->rear=q->front;
free(p);
printf("%d已离开\n",a);
}
void main()
{
queue *list;
int number=1,m;
char ch1,ch2;
list=(queue*)malloc(sizeof(queue));
initqueue(list);
while(1){
printf("请输入“#”字符获取排队号,其他字符无效:\t");
scanf("%c",&ch1);
getchar();
if(ch1!='#') {printf("字符输入错误\n"); exit(0);}
if(ch1=='#'){
enterqueue(list ,number);
number++;
printf("\t请输入“&”获取窗口号: ");
scanf("%c",&ch2);
getchar();
if(ch2!='&') {printf("字符输入错误\n"); exit(0);}
if(ch2=='&'){
srand(time(NULL));
m=rand()%3+1;
printf("请到第%d个窗口办理业务:\n ",m);
}
}
//if ( count>N ) printf("\n已超过%d人等候,稍后再取号:\n",N);
}
}