Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define OK 0
- #define ERROR -1
- #define MAXSIZE 1000
- //定义队列
- struct Queue{
- int front,rear; //队列头,尾下表
- int Q[MAXSIZE];
- };
- typedef struct Queue QUEUE;
- //队列初始化
- int init(QUEUE *q)
- {
- q->front=q->rear=0;
- return OK;
- }
- //判断队列是否为空
- int empty(QUEUE *q)
- {
- if(q->rear==q->front)
- return OK;
- return ERROR;
- }
- //判断队列是否满
- int full(QUEUE *q)
- {
- return q->front==(q->rear+1)%MAXSIZE;
- }
- //入队操作
- int push(QUEUE *q,int t)
- {
- if(full(q))
- return ERROR;
- q->Q[q->rear]=t;
- q->rear=(q->rear+1)%MAXSIZE;
- return OK;
- }
- //出队操作
- int pop(QUEUE *q)
- {
- if(empty(q))
- return ERROR;
- q->front=(q->front+1)%MAXSIZE;
- return OK;
- }
- //返回队列的第一个元素
- int gettop(QUEUE *q)
- {
- return q->Q[q->front];
- }
- //计算队列排队需要的平均时间
- int average(QUEUE *q)
- {
- if(empty(q))
- return 0;
- else
- {
- int sum,cnt;
- sum=0;
- cnt=0;
- while(!empty(q))
- {
- int t=gettop(q);
- cnt++;
- sum+=t;
- pop(q);
- }
- return sum/cnt;
- }
- }
- int main()
- {
- int n,T;
- char X;
- QUEUE *qA,*qB,*qC;
- qA=(QUEUE*)malloc(sizeof(QUEUE));
- qB=(QUEUE*)malloc(sizeof(QUEUE));
- qC=(QUEUE*)malloc(sizeof(QUEUE));
- init(qA);
- init(qB);
- init(qC);
- scanf("%d",&n);
- while(n--)
- {
- getchar();
- scanf("%c%d",&X,&T);
- if(X=='A') push(qA,T);
- else if(X=='B') push(qB,T);
- else if(X=='C') push(qC,T);
- else printf("Do not serve such customers.");
- }
- printf("%d\n",average(qA));
- printf("%d\n",average(qB));
- printf("%d\n",average(qC));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement