1 #include <cstdlib>
2 #include <iostream>
3 #include <stack>
4 #include <queue>
5 #define stacksize 3
6
7 using namespace std;
8 typedef struct sqstack
9 {
10 int data[stacksize];
11 int top;
12 } SqStackTp;
13 typedef struct linked_queue
14 {
15 int data;
16 struct linked_queue * next;
17 }LqueueTp;
18 typedef struct
19 {
20 LqueueTp *front , *rear ;
21 } QueptrTp;
22
23
24 SqStackTp s1,s2;
25 QueptrTp q;
26
27
28
29 //stack<int> s1,s2;
30 //queue<int> q;
31 void in()//
32 {int n;
33 printf("%15s","");
34 cout<<"请输入车号: ";
35 cin>>n;
36 if(n<1)
37 exit(1);
38 system("cls");
39 if(s1.top==stacksize-1)
40 {printf("%15s","");cout<<"停车场已满,进入便道等候\n";system("PAUSE");
41 LqueueTp *p=(LqueueTp *)malloc(sizeof(LqueueTp));
42 p->data=n;
43 p->next=NULL;
44 if(q.rear==q.front)
45 {if(q.rear==NULL)
46 {q.rear=p;
47 q.front=p;
48 }
49 else
50 {q.rear->next=p;
51 q.rear=p;
52 }
53 }
54 else
55 {q.rear->next=p;
56 q.rear=p;
57 }
58
59
60 }
61 else
62 {printf("%15s","");cout<<n<<"车已停到停车场\n";system("PAUSE");
63 s1.data[s1.top]=n;
64 s1.top++;
65
66 }
67 }
68 void out()//
69 {int n;
70 printf("%15s","");
71 cout<<"请输入车号: ";
72 cin>>n;
73 if(n<1)
74 exit(1);
75 while((s1.top!=0)&&(s1.data[s1.top]!=n))
76 {s1.top--;
77 s2.data[s2.top]=s1.data[s1.top];
78 s2.top++;
79 }
80 s1.top--;
81 while(s2.top!=0)
82 {s2.top--;
83 s1.data[s1.top]=s2.data[s2.top];
84 s1.top++;
85 }
86 printf("%15s","");cout<<n<<"车已开出停车场\n";system("PAUSE");
87 while(s1.top!=stacksize-1)
88 {
89 if(q.rear==q.front)
90 {
91 if(q.front==NULL)
92 return ;
93 else
94 {
95 LqueueTp *p;
96 p=q.front;
97 q.front=q.rear=NULL;
98 s1.data[s1.top]=p->data;
99 s1.top++;
100
101 printf("%15s","");cout<<"便道上的"<<p->data<<"车进入停车场\n"; system("PAUSE");
102 free(p);
103 }
104 }
105 else
106 {LqueueTp *p;
107 p=q.front;
108 q.front=q.front->next;
109 s1.data[s1.top]=p->data;
110 s1.top++;
111 printf("%15s","");cout<<"便道上的"<<p->data<<"车进入停车场\n";system("PAUSE");
112 free(p);
113 }
114 }//while
115 }
116
117 int main(int argc, char *argv[])
118 {int n;
119 int order;
120 s1.top=0;
121 s2.top=0;
122 q.front=q.rear=NULL;
123 while(1)
124 {system("cls");
125 printf("%15s","");
126 cout<<"1为进停车场,2为出停车场\n";
127 printf("%15s","");
128 cout<<"请输入命令: ";
129 cin>>order;
130 while(order>2||order<1)
131 {printf("%15s","");cout<<"输入错误,重新输入";cin>>order;}
132
133
134 if(order==1)
135 {in();}
136 else
137 {out();}
138 }
139
140 system("PAUSE");
141 return EXIT_SUCCESS;
142 }
143
144