用c语言模拟简单的银行排队系统

 

此程序模拟了银行排队系统,用链表实现队列排队

 

List.h

 1 #ifndef  LIST_H
 2 #define LIST_H
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 
 6 #define OK      0
 7 #define ERROR   -1
 8 //define the structure node
 9 typedef struct queue
10 {
11         int value;
12         struct queue *next;
13 }Queue;
14 
15 // front point to head node
16 typedef struct linkqueue
17 {
18         Queue *front;
19         Queue *tail;
20 }LinkQueue;
21 
22 #define QUEUE_LEN  sizeof(Queue)
23 //init a queue
24 
25 int initQueue(LinkQueue *);
26 
27 //whether the queue is empty or not
28 int isEmptyQueue(LinkQueue *);
29 
30 
31 //enter a value  into queue
32 int enterQueue(LinkQueue *,int value);
33 
34 //pop a vaue from the queue
35 int deQueue(LinkQueue *);
36 
37 
38 //printf the Queue
39 int printQueue(LinkQueue *);
40 
41 int Query(LinkQueue *,int);
42 
43 
44 #endif

 

List.c  的实现:

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include"List.h"
 4 
 5 
 6 
 7 int initQueue(LinkQueue *Q)
 8 {
 9         printf("Init a queue ......\n");
10         if(!(Q->front = Q->tail = (Queue *)malloc(QUEUE_LEN)))
11         {
12                 printf("ERROR:Malloc Error !\n");
13                 return ERROR;
14         }
15         Q->front->next = NULL;
16         return OK;
17 }
18 
19 int enterQueue(LinkQueue *Q,int enterValue)
20 {
21         Queue *tmpNode = NULL;
22         if(!(tmpNode = (Queue*)malloc(QUEUE_LEN)))
23         {
24                 printf("ERROR:Malloc Error !\n");
25                 return ERROR;
26         }
27 
28         tmpNode->value = enterValue;
29         tmpNode->next = NULL;
30         Q->tail->next = tmpNode;
31         Q->tail = tmpNode;
32 
33 }
34 
35 int isEmptyQueue(LinkQueue *Q)
36 {
37         return  (Q->front->next == NULL);
38 }
39 
40 
41 
42 int printQueue(LinkQueue *Q)
43 {
44         Queue *tmpNode = NULL;
45         tmpNode = Q->front->next;
46         if(NULL == tmpNode)
47         {
48                 printf("ERROR:Queue is NULL\n");
49                 return ERROR;
50 
51         }
52 
53         while(tmpNode != Q->tail)
54         {
55                 printf("%d  ",tmpNode->value);
56                 tmpNode = tmpNode->next;
57         }
58         printf("%d\n",Q->tail->value);
59 
60 }
61 
62 int deQueue(LinkQueue *Q)
63 {
64         if(isEmptyQueue(Q))
65         {
66                 printf("ERROR:The Queue is empty !\n");
67                 return ERROR;
68         }
69         int value;
70         value = Q->front->next->value;
71         Q->front->next = Q->front->next->next;
72         return value;
73 }
74 
75 
76 int Query(LinkQueue *Q,int myNum)
77 {
78         int numOfMan = 0;
79         if(isEmptyQueue(Q))
80         {
81                 printf("The Queue is empty \n");
82                 return ERROR;
83         }
84         if( myNum>Q->tail->value || myNum < Q->front->next->value)
85         {
86                 printf("SORRY you have put into a wrong waiting number\n");
87                 return ERROR;
88         }
89 
90         numOfMan = myNum-Q->front->next->value;
91         return  numOfMan;
92 }

Bank.c

  1  *Author :zxJay
  2  *
  3  *
  4  */
  5 
  6 #include<stdio.h>
  7 #include<string.h>
  8 #include"List.h"
  9 
 10 static bankNumber = 0;
 11 
 12 static totalNumber = 0;
 13 
 14 int stopBank();
 15 int startBank();
 16 int flag = 1;
 17 
 18 int main()
 19 {
 20         int choice = 0;
 21         int num = 0;
 22         LinkQueue Q;
 23         initQueue(&Q);
 24         while(1)
 25         {
 26                 printf("################Bank Bank queuing system #############\n");
 27                 printf("###           1:Applying A number           \n");
 28                 printf("###           2:Query                       \n");
 29                 printf("###           3:Stop Applying A NUmber      \n");
 30                 printf("###           4:Start Applying A Number     \n");
 31                 printf("###           5:Leaving Bank     \n");
 32                 printf("###           6:Print list     \n");
 33                 printf("###           7:EXIT                        \n\n\n");
 34                 printf("   Please put into you chioce[1-5]:   ");
 35                 scanf("%d",&choice);
 36                 switch(choice)
 37                 {
 38                 case 1 :
 39                         if(flag)
 40                         {
 41                                 system("clear");
 42                                 bankNumber++;
 43                                 enterQueue(&Q,bankNumber);
 44                                 totalNumber++;
 45                                 printf("your waiting number id %d\n",bankNumber);
 46                                 printf("%d person(s) befor you,please wait for a while\n",Query(&Q,bankNumber));
 47                                 sleep(2);
 48                                 system("clear");
 49                                 break;
 50                         }
 51                         else
 52                         {
 53                                 system("clear");
 54                                 printf("         stop Applying A Bank number\n");
 55                                 break;
 56                         }
 57                 case 2 :
 58                         system("clear");
 59                         printf("Please put into your number:\n");
 60                         scanf("%d",&num);
 61                         printf("%d person(s) before,please wait for a while\n",Query(&Q,num));
 62                         sleep(2);
 63                         system("clear");
 64                         break;
 65                 case 3 :
 66                         stopBank();
 67                         system("clear");
 68                         printf("         stop Applying A Bank number\n");
 69                         break;
 70                 case 4 :
 71                         startBank();
 72                         system("clear");
 73                         break;
 74                 case 5 :
 75                         system("clear");
 76                         printf("you (%d) are leaving bank ....\n",Q.front->next->value);
 77                         deQueue(&Q);
 78                         totalNumber--;
 79                         sleep(2);
 80                         system("clear");
 81                         break;
 82                 case 6 :
 83                         system("clear");
 84                         printf("Waiting List :");
 85                         printQueue(&Q);
 86                         sleep(3);
 87                         system("clear");
 88                         break;
 89                 case 7 :
 90                         exit(0);
 91                         break;
 92                 default :
 93                         break;
 94 
 95                 }
 96 
 97         }
 98 
 99         return OK;
100 }
101 
102 
103 int stopBank()
104 {
105         flag = 0;
106 
107 }
108 
109 int startBank()
110 
111 {
112         flag = 1;
113 
114 }

 

 

编译环境:redhat  6.3 

 

posted @ 2013-11-23 17:02  zxjay  阅读(3118)  评论(0编辑  收藏  举报