Forgive me if there are defects in the first writing
1, Question content
The simulator contains multiple queues, which can be simulated using queue arrays. Suppose that the grocery store has five cashier lines, and customers can pay at random. Customers will enter the shortest team. If the team is the same length, choose the nearest team. The time it takes to complete each transaction is also random.
Do some extra work and expand the grocery store queue so that customers can:
1. If you wait for more than a certain time, you can leave the team.
2. Check whether the other team is shorter within a given time interval. If the other team is shorter, switch teams.
3. If a friend is waiting in line, you can jump in line.
2, Detailed design
1. Grocery store queue structure
typedef struct
{int data[MaxSize]; / / save the elements in the queue
int front,rear; // Head and tail pointer
} People;
The structure People will be used to store customers.
2. Initialize pointer
Void initqueue (people & P) / / initialize circular queue p
The input queue needs to be initialized.
3. Number of elements in the output queue
int DisplayQueue(People p) / / returns the number of elements in the queue
The input queue is required, and the return value is the number of elements in the queue.
4. Customers join the team
Int enqueue (people & P) / / join the team
You need to enter the queue to queue the elements.
5. Customer departures (departures after payment)
Int dequeue (people & P) / / outgoing
You need to enter the queue and take the elements in the queue out of the queue.
6. Return to the shortest team
int shortline(People p,People p2,People p3,People p4,People p5)
You need to input five queues. Put the return value of each queue calling DisplayQueue() function into an array and return the array subscript with the smallest value. If the minimum value is repeated, return the array subscript with the smallest value.
7. Customer entry and departure
void enterandleave(People &p,People &p2,People &p3,People &p4,People &p5,int a)
Five queues and queues in or out of the queue need to be entered (out of the queue = in the queue + 5)
8. The shortest queue of customers
void customer(People &p,People &p2,People &p3,People &p4,People &p5,int customer)
You need to input five queues and the number of customers who need to enter the queue, put the return value of each queue calling DisplayQueue() function into an array, compare it with the array whose index is the return value of the shortline() function, count the number of times the minimum value is repeated, and put the index into a new array. If it is once, directly add 1 to the return value of the shortline() function, That is, the queue that the customer needs to enter, and then call the enter and leave () function. If it is multiple times, take a queue at random and call the enter and leave () function.
9. Customers leave the team (those who can't wait to leave the team)
int leave(People &p)
You need to input the queue, dequeue the elements in the queue, and return the number of dequeued elements.
10. Customers jump in line
void inserted(People &p,People &p2,People &p3,People &p4,People &p5)
You need to enter five queues. The queues of friends and people who need to jump in the queue are random. If the queues are different, call the enter and leave() function.
11. Customers change teams
int huan(People &p,People &p2,People &p3,People &p4,People &p5)
Five queues need to be entered, using the goto statement. The shortline() function is called to find the shortest queue a. Use the while statement to loop, and the condition is as short line (P, P2, P3, P4, P5) & & T.
*1. If a > 0 & & A < 4, put the return value of calling DisplayQueue() into a local variable array total []
*If the difference between the number of queues on both sides of a and queue a is greater than 1, take a queue at random and call the enter and leave() function.
*Else if total [a + 1] - Total [a] > 1, the enter and leave() function is called.
*Else if total [A-1] - Total [a] > 1, the enter and leave() function is called.
*Else, if total [a + 2] - Total [a] > 1 & & total [A-2] - Total [a] > 1 & & A2, take a queue randomly and call the enterandleave() function.
*Else, if total [a + 2] - Total [a] > 1 & & A= 3, then call the enter and leave() function.
*Else, if total [A-2] - Total [a] > 1 & & A= 1, then call the enterandleave() function.
*Else if total [a + 3] - Total [a] > 1&&a1, the enter and leave() function is called.
*Else if total [A-3] - Total [a] > 1&&a3, the enter and leave() function is called.
*Else t=0; End the while loop.
*2. If a0, put the return value of calling DisplayQueue() into a local variable array total []
*If total [a + 1] - Total [a] > 1, the enter and leave() function is called.
*Else if total [a + 2] - Total [a] > 1, the enter and leave() function is called.
*Else if total [a + 3] - Total [a] > 1, the enter and leave() function is called.
*Else if total [a + 4] - Total [a] > 1, the enter and leave() function is called.
*Else t=0; End the while loop.
*3. If a4, put the return value of calling DisplayQueue() into a local variable array total []
*If total [A-1] - Total [a] > 1, the enter and leave() function is called.
*Else if total [A-2] - Total [a] > 1, the enter and leave() function is called.
*Else if total [A-3] - Total [a] > 1, the enter and leave() function is called.
*Else if total [A-4] - Total [a] > 1, the enter and leave() function is called.
*Else t=0; End the while loop.
If t==0 after the loop ends, the loop ends. Otherwise, use the goto statement to go to the statement calling the shortline() function.
12. Current status of each team
void cus(People p,People p2,People p3,People p4,People p5)
You need to input five queues, put the return value of each queue calling DisplayQueue() function into an array, and then use the for loop printf to check the current status of each queue.
13. Main function
Initialize queue: call InitQueue() function
Enqueue: call EnQueue() function
Output queue status: call cus() function
Output the number of people who have completed the payment: call the cus() function
Output the number of people who have completed payment: DeQueue() function needs to be called
Queue automatically when the number of people in each queue is more than 2: you need to call the customer() function
The number of people in each team > 3 and the waiting time is a multiple of 20. Output the number of people who can't wait to leave: you need to call the leave() function
If the number of people in each queue is more than 3, the queue jumping status will be output automatically: the inserted() function needs to be called
3, The specific code implementation is as follows (I use DEV c + + compiler)
#include<stdio.h> #include<stdlib.h> #include<time.h> #define MaxSize 100 typedef struct { int data[MaxSize]; //Save elements in the queue int front,rear; //Head and tail pointer } People; void InitQueue(People &p) //Initialize circular queue p { p.rear=p.front=0; //Pointer initialization } int DisplayQueue(People p) //Returns the number of elements in the queue { int b=0; while(p.front!=p.rear) { p.front++; b++; } return b; } int EnQueue(People &p) //Join the team { int a,i; a=rand()%11+20; for(i=0;i<a;i++) { p.data[p.rear+1]=1; p.rear++; } return 1; } int DeQueue(People &p) //Out of the team { int i,a; if(DisplayQueue(p)==0) return 0; if(DisplayQueue(p)<=10) //When the number of people is too small, the maximum number of out of line elements is the total number of elements a=rand()%(DisplayQueue(p)+1); else a=rand()%11; for(i=0;i<a;i++) { p.front++; } return a; } int shortline(People p,People p2,People p3,People p4,People p5) // Return to shortest queue { int total[]={DisplayQueue(p),DisplayQueue(p2),DisplayQueue(p3),DisplayQueue(p4),DisplayQueue(p5)}; int b=total[0],c,i; for(i=0;i<5;i++) { if(b>=total[i]) { b=total[i]; c=i; } } return c; } void enterandleave(People &p,People &p2,People &p3,People &p4,People &p5,int a) { switch(a) { case 1: { p.data[p.rear+1]=1; p.rear++; break; } case 2:{ p2.data[p2.rear+1]=1; p2.rear++; break; } case 3:{ p3.data[p3.rear+1]=1; p3.rear++; break; } case 4:{ p4.data[p4.rear+1]=1; p4.rear++; break; } case 5:{ p5.data[p5.rear+1]=1; p5.rear++; break; } case 6: { p.front++; break; } case 7:{ p2.front++; break; } case 8:{ p3.front++; break; } case 9:{ p4.front++; break; } case 10:{ p5.front++; break; } } } void customer(People &p,People &p2,People &p3,People &p4,People &p5,int customer) { int i,count=0,c[5],a; int total[]={DisplayQueue(p),DisplayQueue(p2),DisplayQueue(p3),DisplayQueue(p4),DisplayQueue(p5)}; for(i=0;i<5;i++) { if(total[i]==total[shortline(p,p2,p3,p4,p5)]) { c[count]=i; count++; } } if(count!=1) { a=c[rand()%count]+1; enterandleave(p,p2,p3,p4,p5,a); printf("Due to%d The first team is as long as the second%d The customer selects the nearest team%d team\n",count,customer+1,a); } else { a=shortline(p,p2,p3,p4,p5)+1; enterandleave(p,p2,p3,p4,p5,shortline(p,p2,p3,p4,p5)+1); printf("The first%d Customers successfully enter the shortest%d team\n",customer+1,a); } } int leave(People &p) { int b=rand()%4; for(int i=0;i<b;i++) { p.front++; } return b; } void inserted(People &p,People &p2,People &p3,People &p4,People &p5) { int friends=rand()%5+1,others=rand()%5+1; if(friends!=others) { enterandleave(p,p2,p3,p4,p5,friends); enterandleave(p,p2,p3,p4,p5,others+5); printf("%d There are customers in the team%d There are friends in the team,And cut in line\n",others,friends); } printf("%d There are customers in the team%d There are friends in the team,And cut in line\n",others,friends); } int huan(People &p,People &p2,People &p3,People &p4,People &p5) { int a,b,t=1; jixv:a=shortline(p,p2,p3,p4,p5); if(a>0&&a<4) { while(a==shortline(p,p2,p3,p4,p5)&&t) { int total[]={DisplayQueue(p),DisplayQueue(p2),DisplayQueue(p3),DisplayQueue(p4),DisplayQueue(p5)}; if(total[a+1]-total[a]>1&&total[a-1]-total[a]>1) { b=rand()%2; if(b==0) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+7); } if(b==1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+5); } } else if(total[a+1]-total[a]>1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+7); } else if(total[a-1]-total[a]>1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+5); } else if(total[a+2]-total[a]>1&&total[a-2]-total[a]>1&&a==2) { b=rand()%2; if(b==0) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+8); } if(b==1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+4); } } else if(total[a+2]-total[a]>1&&a!=3) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+8); } else if(total[a-2]-total[a]>1&&a!=1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+4); } else if(total[a+3]-total[a]>1&&a==1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+9); } else if(total[a-3]-total[a]>1&&a==3) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+3); } else t=0; } if(t==0) return 0; else goto jixv; } else if(a==0) { while(a==shortline(p,p2,p3,p4,p5)&&t) { int total[]={DisplayQueue(p),DisplayQueue(p2),DisplayQueue(p3),DisplayQueue(p4),DisplayQueue(p5)}; if(total[a+1]-total[a]>1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+7); } else if(total[a+2]-total[a]>1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+8); } else if(total[a+3]-total[a]>1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+9); } else if(total[a+4]-total[a]>1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+10); } else t=0; } if(t==0) return 0; else goto jixv; } else if(a==4) { while(a==shortline(p,p2,p3,p4,p5)&&t) { int total[]={DisplayQueue(p),DisplayQueue(p2),DisplayQueue(p3),DisplayQueue(p4),DisplayQueue(p5)}; if(total[a-1]-total[a]>1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+5); } else if(total[a-2]-total[a]>1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+4); } else if(total[a-3]-total[a]>1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+3); } else if(total[a-4]-total[a]>1) { enterandleave(p,p2,p3,p4,p5,a+1); enterandleave(p,p2,p3,p4,p5,a+2); } else t=0; } if(t==0) return 0; else goto jixv; } } void cus(People p,People p2,People p3,People p4,People p5) { int total[]={DisplayQueue(p),DisplayQueue(p2),DisplayQueue(p3),DisplayQueue(p4),DisplayQueue(p5)}; for(int num=1;num<6;num++) printf("ranks%d have%d A customer is waiting\n",num,total[num-1]); } int main() { People p,p2,p3,p4,p5; int a=0,b,customers,n=0,j,flag=0,f=0; srand((unsigned)(time(NULL))); InitQueue(p); InitQueue(p2); InitQueue(p3); InitQueue(p4); InitQueue(p5); EnQueue(p); EnQueue(p2); EnQueue(p3); EnQueue(p4); EnQueue(p5); cus(p,p2,p3,p4,p5); while(DisplayQueue(p)!=0||DisplayQueue(p2)!=0||DisplayQueue(p3)!=0||DisplayQueue(p4)!=0||DisplayQueue(p5)!=0) { getchar(); n++; printf("Wait%d minute\n",5*n); int one=DeQueue(p);printf("The number of transactions completed by the first team is%d people\n",one); int two=DeQueue(p2);printf("The number of transaction completed by the second team is%d people\n",two); int three=DeQueue(p3);printf("The number of transaction completed by the third team is%d people\n",three); int four=DeQueue(p4);printf("The number of transaction completed by the fourth team is%d people\n",four); int five=DeQueue(p5);printf("The number of transaction completed by the fifth team is%d people\n",five); printf("The current status of the team is as follows:\n") ; cus(p,p2,p3,p4,p5); printf("---------------------------\n"); if(DisplayQueue(p)!=0||DisplayQueue(p2)!=0||DisplayQueue(p3)!=0||DisplayQueue(p4)!=0||DisplayQueue(p5)!=0) { printf("Customers at the back of the queue are automatically placed in a shorter queue\n") ; huan(p,p2,p3,p4,p5); printf("The current status of the team is as follows:\n") ; cus(p,p2,p3,p4,p5); printf("---------------------------\n"); } if(DisplayQueue(p)>2&&DisplayQueue(p2)>2&&DisplayQueue(p3)>2&&DisplayQueue(p4)>2&&DisplayQueue(p5)>2) { customers=rand()%6+3; printf("Again%d A customer is queued\n",customers); for(int i=0;i<customers;i++) { customer(p,p2,p3,p4,p5,i); } printf("The current status of the team is as follows:\n") ; cus(p,p2,p3,p4,p5); printf("---------------------------\n"); } if(n%4==0&&DisplayQueue(p)>3&&DisplayQueue(p2)>3&&DisplayQueue(p3)>3&&DisplayQueue(p4)>3&&DisplayQueue(p5)>3) { b=leave(p); if(b=!0) printf("Team 1 yes%d A customer left\n",b);; b=leave(p2); if(b=!0) printf("Team 2 yes%d A customer left\n",b);; b=leave(p3); if(b=!0) printf("Team 3 yes%d A customer left\n",b);; b=leave(p4); if(b=!0) printf("Team 4 yes%d A customer left\n",b);; b=leave(p5); if(b=!0) printf("Team 5 has%d A customer left\n",b);; printf("The current status of the team is as follows:\n") ; cus(p,p2,p3,p4,p5); printf("---------------------------\n"); }; if(DisplayQueue(p)>3&&DisplayQueue(p2)>3&&DisplayQueue(p3)>3&&DisplayQueue(p4)>3&&DisplayQueue(p5)>3) { int number=rand()%5; for(int i=0;i<number;i++) inserted(p,p2,p3,p4,p5); if(number!=0) { printf("The current status of the team is as follows:\n") ; cus(p,p2,p3,p4,p5); printf("---------------------------\n"); } } } printf("All transactions have been completed. Welcome to visit next time!"); }