患者到医院看病模拟

标题:
患者到医院看病事件模拟
时 限:
1000 ms
内存限制:
10000 K
总时限:
3000 ms
描述:
患者到医院看病的顺序是:先排队等候,再看病治疗。要求设计一个算法,模拟病人等候就诊的过程。
其中:“病人到达”用命令“A”(或“a”)表示,“护士让下一位就诊”用“N”(或“n”)表示,“不再接收病人排队”用“S”(或“s”)表示。
输入:
A(或a)  病历号x
N(或n)
S(或s)
输出:
病历号为x的病人入队,无输出
若无病人,显示无病人就诊;否则输出队列中的第一个病人就诊信息
今天不再接收病人排队,输出队列中所有病人的病历号
若输入其它命令,则输出"输入命令不合法!"
输入样例:
A 123
n
n
a 124
a 125
x
s
输出样例:
病历号为123的病人就诊
无病人就诊
输入命令不合法!
今天不再接收病人排队,下列排队的病人依次就诊:124 125
提示:
 
来源:
 
1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef struct node
4 {
5 int data;
6 int d_index;
7 struct node *next;
8 }*List;
9 typedef struct nn
10 {
11 char ch;
12 int c_index;
13 struct nn *next;
14 }*Com;
15 List Init()
16 {
17 List head=malloc(sizeof(struct node));
18 head->next=NULL;
19 head->data=-1;
20 head->d_index=-1;
21 return head;
22 }
23 Com Ini()
24 {
25 Com head=malloc(sizeof(struct nn));
26 head->next=NULL;
27 head->ch='L';
28 head->c_index=-1;
29 return head;
30 }
31 int SeeDocter(List head,int index)
32 { int Person_num=-1;
33 int tem_index=1;
34 while(head->next!=NULL&&tem_index<index)
35 {
36 if(head->next->data>0)
37 {
38 Person_num=head->next->data;
39 head->next->data=0;
40 break;
41 }
42 head=head->next;
43 tem_index++;
44 }
45 return Person_num;
46 }
47 void Add_to_list(List head,int data,int index)
48 {
49
50 List newnode=malloc(sizeof(struct node));
51 if(newnode==NULL)
52 {
53 printf("memory out of use!\n");
54 exit(1);
55 }
56 while(head->next!=NULL)
57 {
58 head=head->next;
59 }
60 newnode->data=data;
61 newnode->d_index=index;
62 newnode->next=NULL;
63 head->next=newnode;
64
65 }
66 void Add_command(Com head, char c,int index)
67 {
68 Com newnode=malloc(sizeof(struct nn));
69 if(newnode==NULL)
70 {
71 printf("memory out of use!\n");
72 exit(1);
73 }
74 while(head->next!=NULL)
75 {
76 head=head->next;
77 }
78 newnode->ch=c;
79 newnode->c_index=index;
80 newnode->next=NULL;
81 head->next=newnode;
82 }
83 void main()
84 {char cm;
85 char select_char;
86 int select_index;
87 int Person=-1;
88 int data;
89 int index=0;
90 int chose=1;
91 int c_index=1;
92 int d_index=1;
93 List head=Init();
94 Com m_com=Ini();
95 while(1)
96 {
97 scanf("%c",&cm);
98
99 if(cm=='S'||cm=='s')
100 { Add_command(m_com,'s',c_index);
101 break;
102 }
103 switch (cm)
104 {
105 case 'A':
106 case 'a':Add_command(m_com,'a',c_index);
107 scanf("%d",&data);
108 Add_to_list(head,data,d_index);
109 d_index++;
110 c_index++;
111 break;
112 case 'N':
113 case 'n':Add_command(m_com,'n',c_index);
114 Add_to_list(head,0,d_index);
115 c_index++;
116 d_index++;
117 break;
118 case '\n':break;
119
120 default:Add_command(m_com,cm,c_index);
121 Add_to_list(head,-100,d_index);
122 c_index++;
123 d_index++;
124 break;
125
126
127 }
128 }
129 while(m_com->next->ch!='s')
130 {
131 select_char=m_com->next->ch;
132 select_index=m_com->next->c_index;
133 switch(select_char)
134 {
135 case 'a':break;
136 case 'n':Person=SeeDocter(head,select_index);
137 if(Person>0)
138 printf("病历号为%d的病人就诊\n",Person);
139 else
140 printf("无病人就诊\n");
141 break;
142 default: printf("输入命令不合法!\n");break;
143 }
144 m_com=m_com->next;
145 }
146 printf("今天不再接收病人排队,下列排队的病人依次就诊:");
147 while(head->next!=NULL)
148 {
149 if(head->next->data>0)
150 {
151 printf("%d \n",head->next->data);
152 head=head->next;
153 }
154 else
155 head=head->next;
156 }
157
158
159 }
posted @ 2011-06-09 22:30  又是一年夏天  阅读(1031)  评论(1编辑  收藏  举报