Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define OK 0
  5. #define ERROR -1
  6. #define MAXSIZE 1000
  7.  
  8. //定义队列
  9. struct Queue{
  10. int front,rear; //队列头,尾下表
  11. int Q[MAXSIZE];
  12. };
  13. typedef struct Queue QUEUE;
  14.  
  15. //队列初始化
  16. int init(QUEUE *q)
  17. {
  18. q->front=q->rear=0;
  19. return OK;
  20. }
  21.  
  22. //判断队列是否为空
  23. int empty(QUEUE *q)
  24. {
  25. if(q->rear==q->front)
  26. return OK;
  27. return ERROR;
  28. }
  29.  
  30. //判断队列是否满
  31. int full(QUEUE *q)
  32. {
  33. return q->front==(q->rear+1)%MAXSIZE;
  34. }
  35.  
  36. //入队操作
  37. int push(QUEUE *q,int t)
  38. {
  39. if(full(q))
  40. return ERROR;
  41. q->Q[q->rear]=t;
  42. q->rear=(q->rear+1)%MAXSIZE;
  43. return OK;
  44. }
  45.  
  46. //出队操作
  47. int pop(QUEUE *q)
  48. {
  49. if(empty(q))
  50. return ERROR;
  51. q->front=(q->front+1)%MAXSIZE;
  52. return OK;
  53. }
  54.  
  55. //返回队列的第一个元素
  56. int gettop(QUEUE *q)
  57. {
  58. return q->Q[q->front];
  59. }
  60.  
  61. //计算队列排队需要的平均时间
  62. int average(QUEUE *q)
  63. {
  64. if(empty(q))
  65. return 0;
  66. else
  67. {
  68. int sum,cnt;
  69. sum=0;
  70. cnt=0;
  71. while(!empty(q))
  72. {
  73. int t=gettop(q);
  74. cnt++;
  75. sum+=t;
  76. pop(q);
  77. }
  78. return sum/cnt;
  79. }
  80. }
  81.  
  82.  
  83. int main()
  84. {
  85. int n,T;
  86. char X;
  87. QUEUE *qA,*qB,*qC;
  88.  
  89. qA=(QUEUE*)malloc(sizeof(QUEUE));
  90. qB=(QUEUE*)malloc(sizeof(QUEUE));
  91. qC=(QUEUE*)malloc(sizeof(QUEUE));
  92.  
  93. init(qA);
  94. init(qB);
  95. init(qC);
  96.  
  97. scanf("%d",&n);
  98. while(n--)
  99. {
  100. getchar();
  101. scanf("%c%d",&X,&T);
  102. if(X=='A') push(qA,T);
  103. else if(X=='B') push(qB,T);
  104. else if(X=='C') push(qC,T);
  105. else printf("Do not serve such customers.");
  106. }
  107. printf("%d\n",average(qA));
  108. printf("%d\n",average(qB));
  109. printf("%d\n",average(qC));
  110.  
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement